Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validation emebed objects

I have a company class and that has a phone class on property $phone. I will validate the phone property based on my PhoneValidation class.

my validation class

class Company extends Validation
{
    public function initialize()
    {
        $phoneValidation = new Phone();
        $validators = $phoneValidation->getValidators();
        foreach ($validators as $validator) {
            $this->add('phone['.$validator[0].']', $validator[1]);
        }

this doesn't work.



306
Accepted
answer
edited Aug '16

I found a solution

class Company extends Validation
{
    protected $phoneValidation;

    public function __construct(array $validators = array())
    {
        parent::__construct($validators);

        $this->phoneValidation = new PhoneValidation();
    }

    public function afterValidation($data, $entity, $messages)
    {
        if (!empty($data['phone'])) {
            $messages->appendMessages($this->phoneValidation->validate($data['phone']));
        }

        return true;
    }
}