0

I using Phalcon framework, i have a Collection Model, in validate() function of this model, i validate my fields like below:

class Users extends Collection
{


    public function validation()
    {

        $this->validate(
            new EmailValidator(
                array(
                    "field"   => "email",
                    "message" => "email is not valid"
                )
            )
        );

        $this->validate(
            new NumericalityValidator(
                array(
                    "field"   => "phone",
                    "message" => "phone is not valid"
                )
            )
        );


        return $this->validationHasFailed() != true;
    }

}

How can i tel to this model one of these two fields are mandatory ? for example if tel is not empty, email can be empty, or if email is not empty, tel can empty, and when both fields are empty, validation should be failed.

Ali
  • 443
  • 5
  • 22

2 Answers2

0

Something like this ?

public function validation()
{
    if(empty($this->phone)) {
        $this->validate(
            new EmailValidator(
                array(
                    "field"   => "email",
                    "message" => "email is not valid"
                )
            )
        );
    }

    if(empty($this->email)) {
        $this->validate(
            new NumericalityValidator(
                array(
                    "field"   => "phone",
                    "message" => "phone is not valid"
                )
            )
        );
    }


    return $this->validationHasFailed() != true;
}
Juri
  • 1,369
  • 10
  • 16
0

You should be able to append a message. https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Message.html

<?php

use Phalcon\Mvc\Model\Message as Message;

public function validation()
{
    if(empty($this->phone) && empty($this->email)) {
        $message = new Message('Phone or email must be provided',
                               array('phone','email'), 
                               'PresenceOf');
    $this->appendMessage($message);
    return $this->validationHasFailed() != true;
}
Nicolas
  • 300
  • 2
  • 5