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

Discovering custom validation settings

My goal is to enable "pre-submit" validation on the front end form (probably via jQuery or similar) which matches the validation specified in the Model/ORM.

I can look at the standard metadata for a given model, and can figure out the basics: field types, NULL/NOT NULL, etc. That's all good.

I'm interested to know whether I can access any information regarding any of the additional validation methods implemented via $this->validate() in the model.

I'm considering the creation of a custom metadata item to describe any additional validation, but I'd have to make sure my team makes updates in both places whenever they add/edit validation rules. Much nicer if this information was easily exposed and directly linked to the validation items.

Thoughts? Thanks!

edited Jun '14

Hi,

I found a way to run validation without run save method.

For example I have a Contacts model:

class Contacts extends \Phalcon\Mvc\Model
{
    public function validation()
    {
        $this->validate(new Email(array(
            'field' => 'email'
        )));
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }
}

And I run this code to check is object valid without saving them:

$contact = new Contacts();
$contact->fireEvent('validation');

// object is valid
if ($contact->validationHasFailed()) {
    // object is invalid
    $errors = $contact->getMessages(); // here are error fields
} else {
    // do something
}