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

Phalcon 3.0 Custom Model Validator

Hi All, I upgrade to Phalcon 3.0.1 and I have problems with my custom validator. I will show the code below. I know for the changes in the model for validation and I had changed but maybe there is errors. Please tell me how to fix this. Thank you :)

Custom Validation:


use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\ValidatorInterface;
use Phalcon\Mvc\EntityInterface;

class ExpenseValidator extends Validator implements ValidatorInterface
{
    public function validate(EntityInterface $model)
    {
        $params = $this->getOption('params');
        $message = $this->getOption('message');
        if (!$message) {
            $message = 'Duplicate';
        }

        $count = $model::count(
            array("conditions" => "carID=" . (int)$params['carID'] . " AND rnameHash='" . $params['rnameHash'] . "' AND rID != '" . $params['rID'] . "' AND rcost=" . (float)$params['rcost'] . " AND dateAdd=" . (int)$params['dateAdd']

            )
        );
        //Tools::viewarray($params);exit;
        //echo $count;exit;
        if ($count) {
            $this->appendMessage($message, '', 'ExpenseValidator');
            // Tools::viewarray($this);exit;
            return false;
        }

        return true;
    }
}

In model: (It maybe wrong):

 /**
     * @return bool
     */
    public function validation()
    {
        $validator = new Validation();
        $validator->add(new ExpenseValidator(
                array(
                    'params' => array('rID' => $this->rID, 'carID' => $this->carID, 'rnameHash' => $this->rnameHash, 'rcost' => $this->rcost, 'dateAdd' => $this->dateAdd),
                    'message' => 'Duplicate - ' . $this->rname
                )
            )
        );

        return $this->validate($validator);
    }

Your validator should extend Phalcon\Validation\Validator not Phalcon\Mvc\Model\Validator. There is sample from my email validator:

use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;

/**
 * Class Email
 * @package Agregatix\App\Validators
 */
class Email extends Validator
{
    /**
     * @param Validation $validation
     * @param string $attribute
     *
     * @return bool
     */
    public function validate(Validation $validation, $attribute)
    {
        $value = $validation->getValue($attribute);
        $emailArray = explode("@", $value);
        if (!filter_var($value, FILTER_VALIDATE_EMAIL) || !checkdnsrr(array_pop($emailArray), "MX")) {
            $label = $this->getOption('label');
            if (empty($label)) {
                $label = $validation->getLabel($attribute);
            }
            $message = $this->getOption('message');
            $replacePairs = [":field" => $label];
            if (empty($message)) {
                $message = $validation->getDefaultMessage("Email");
            }
            $validation->appendMessage(
                new Message(
                    strtr($message, $replacePairs), $attribute, "Email",
                    $this->getOption('code')
                )
            );

            return false;
        }

        return true;
    }
}


1.7k

Thank You. I have success with your code example. Now It's working as expected.