Thursday, July 14, 2011

Validation Tips and Tricks

When using validation, as mentioned previously, there are some tricks to automatically adding the class required to your form fields. Any validation rule that has minlength works. So a short, simple rule to just ensure the required class is added to the field label could look something like this:
    var $validate = array(
        'field_name' => array(
            'rule' => array('minlength', 1)
        )
    );
If you want to require user input but don't care about the required class for your labels, you can actually use something as simple as this:
    var $validate = array(
        'field_name' => 'notEmpty'
    );
If you expand that to the following code, however, you will also get the required class for your field label.
    var $validate = array(
        'field_name' => array(
            'rule' => 'notEmpty'
        )
    );
This is very useful when you want the required class for select fields.

No comments:

Post a Comment