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

How shoul I validate form input

Hi

Just started to explore this framework, so this might be a simple question.

But how should I validate input from form? For example a string length or a password strength.

I can see there are some validation classes but cant figure out on how to use them in my application or how to register my own validator rules?

Best Regards // Johannes



98.9k

As @Phillipp said you can add validations to your models/collections:

https://docs.phalcon.io/en/latest/reference/odm.html#validating-data-integrity https://docs.phalcon.io/en/latest/reference/models.html#validating-data-integrity

Version 1.0.0 also introduces a validation component which is independent of the models, its documentation is not ready yet, you can see how it works in the following unit tests:

https://github.com/phalcon/cphalcon/blob/1.0.0/unit-tests/ValidationTest.php

Hi

Thank you for reply, I now have a working validator in my model like this.

What I still don't understand is how I get the "message" from my view template?

public function validation() { $this->validate(new EmailValidator(array( 'field' => 'email', 'message' => 'Please provide a valid email' ))); return $this->validationHasFailed() != true; }

regards // johannes



98.9k

In case that validation fails a message is appended to the messages list:

if ($robot->save()==false) {
    foreach ($robot->getMessages() as $message) {
        echo $message;
    }
}


17.0k

The sam thing bothered me for some time :)

'Please provide a valid email' message is activatet with javascript file utils.js in public folder.
I believe you started working from invo example :)