Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

Одно правило на одно поле

This definition technique allows for better control of how the validation rules work. But before we discuss that, let’s see the general usage pattern adding a rule for a single field:

Простой текст
  1. var $validate = array(
  2. 'fieldName1' => array(
  3. 'rule' => 'ruleName', // or: array('ruleName', 'param1', 'param2' ...)
  4. 'required' => true,
  5. 'allowEmpty' => false,
  6. 'on' => 'create', // or: 'update'
  7. 'message' => 'Your Error Message'
  8. )
  9. );

The 'rule' key is required. If you only set 'required' => true, the form validation will not function correctly. This is because 'required' is not actually a rule.

As you can see here, each field (only one field shown above) is associated with an array that contains five keys: ‘rule’, ‘required’, ‘allowEmpty’, ‘on’ and ‘message’. Let’s have a closer look at these keys.