If you add a validator to an input, Phalcon won't add relevant HTML attributes. Most validation can be done client-side without need to send the form to server. Of course, you can specify HTML attributes manually 2 ways but why can't it be done automatically? No need to repeat yourself (DRY).
Example form class:
class UserForm extends Form
{
public function initialize()
{
//Prepare login field
$login = new Text('login', array(
'maxlength' => 25, //why do we need to write it manually?
'required' => '' //Phalcon cannot make this attribute empty (not empty string)
));
//If you add this validator, Phalcon should add relevant attributes automatically
//In this case - maxlength or pattern
$login->addValidator(new StringLength(array(
'min' => 2,
'max' => 25,
'messageMinimum' => 'Message minimum',
'messageMaximum' => 'Message maximum'
)));
}
}
Look at all built-in validators:
- Between - in case of number inputs - min/max attributes
- Confirmation - probably impossibe without JavaScript
- PresenceOf - add
required
attribute (in HTML5 it can be empty) - Regex - probably
pattern
attribute - StringLength - upper value -
maxlength
, lower -pattern
if Regex not present
PS. I wonder whether it's really worth to use Form class. It's very uncomfortable. You can also write all HTML code manually and do simple validation in controller or model. In case of forms with dynamic fields (we don't know how much) we need additional loops etc. There is one advantage - using form.render() for <select> saves a bunch of Volt code with conditions.
Validation in controller or model?