We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Custom Validation


In model i write a validation and custom validation, rule if i going to add a state its want to check with a function checkStates() if the data is present then its validation want to fail, how to do this please help me.

public function validation() {

     $this->validate(new PresenceOf(array(
            "field"   => "State",
            'message' => 'The State is required',
            'with' => $this->checkStates($this)

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


function checkStates($data){

    $parameters = $this->persistent->parameters;
    if (!is_array($parameters)) {
        $parameters = array();
    $parameters["conditions"] = 'CountryId = '.$countryid;
    $parameters["columns"] = 'id';
   $states = States::findFirst($parameters);

        return false; 
          return true;



as stated in doc here : https://docs.phalcon.io/en/latest/reference/validation.html you may create your custom validator


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

class CheckState extends Validator implements ValidatorInterface

     * Executes the validation
     * @param Phalcon\Validation $validator
     * @param string $attribute
     * @return boolean
    public function validate(Validation $validator, $attribute)
        // If the attribute value is name we must stop the chain
        if ($attribute == 'name') {
            $validator->setOption('cancelOnFail', true);

edited Oct '16

I think my question would fit nicely here.

I have done the custom validator, following the rules explained here, I have problem using it, singularly.

I have a form, so when I call form->isValid($data), my custom validator gets correctly called, but before getting to that part, because I have an Ajax component on my application, I would like to be able to use this custom Validator but only with one element of the form, so I was thinking to call the validate method from the custom validator directly, but always find problems with the $validator parameter.

Is there any example or something in the documentation that I have missed?

If I have a data that I want to validate with my custom validator? what should I pass as a first parameter?

If maybe somebody can point me on the right direction, it would be greatly appreciated.

https://docs.phalcon.io/en/latest/reference/validation.html On this examel of the IPValidator, there's no example of use case, so it's hard to understand how to aply the newly created validator.




As example with this Validator, if I wanted to try it


and call directly the method validate, how would I do it? as a First parameter I should use FileValidator, and as a second parameter use 'file' (being the field i want to validate)? I would come like this:

$validation->validate(FIleValidator, 'file');

$validation is not an instance of FileValidator?

So i would have to give the object itself to the function validate?

Really I'm confused...thanks to anyone.

New Edit:

Sometimes I find the validate method public validate (Phalcon\Validation $validation, mixed $field) and other times I find:

public function validate($data = null, $entity = null) , which want the data to Validate so I dont know how to use this.