We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



854
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;
    }
}