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 2.0 - Own Validator does not work anymore

Hi, I've created my own validator for some models - see below. After updating to Phalcon 2 the framework throws always an error, that the validator is not defined as expected: Fatal error: Declaration of SlipBOX\Libraries\Models\ValidatorMin::validate() must be compatible with Phalcon\Mvc\Model\ValidatorInterface::validate(Phalcon\Mvc\ModelInterface $record) in

I hope you can help me. The code for the validator is almost copied from the documentations. I delevop now without that validator, but would be nice to have it back. :) Thanks a lot (again and again and again) Aljoscha

<?php

namespace SlipBOX\Libraries\Models;

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

class ValidatorMin extends Validator implements ValidatorInterface
{
/**
 * Executes the validation
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
public function validate ($record)
{
    $field  = $this->getOption('field');
    $min    = $this->getOption('min');
    $value  = $record->$field;

    if ($value < $min) {
        $message = $this->getOption('message');
        if (!$message) {
            $message = "The field's value is smaller than allowed.";
        }
        $this->appendMessage(
            $message,
            $field,
            "ValidatorMin"
        );
        return false;
    }
    return true;
}
}

Read about type hinting validate method must be public function validate(\Phalcon\Mvc\ModelInterface $record)



7.7k

Wow > "public function validate(\Phalcon\Mvc\ModelInterface $record)" This will make horrible ugly function names =((



6.4k
Accepted
answer

Wow > "public function validate(\Phalcon\Mvc\ModelInterface $record)" This will make horrible ugly function names =((

use is your friend

use Phalcon\Mvc\Model\Validator,
    Phalcon\Mvc\Model\ValidatorInterface,
    Phalcon\Mvc\ModelInterface as MI;

class ValidatorMin extends Validator implements ValidatorInterface {
    public function validate(MI $record) {}
}