老葛的Drupal培训班 Think in Drupal
通过将表单的属性#validate设置为一个数组,其中函数名为键,一个数组作为值,从而为表单分配一个验证函数。在调用验证函数时,后面的数组中的任何数据都将被传递给验证函数。可以使用下面的方式来定义多个验证器:
// We want foo_validate() and bar_validate() to be called during form validation.
$form['#validate'][] = 'foo_validate';
$form['#validate'][] = 'bar_validate';
// Optionally stash a value in the form that the validator will need
// by creating a unique key in the form.
$form['#value_for_foo_validate'] = 'baz';
如果表单中没有定义属性#validate,那么接下来就要寻找名为“表单ID”+“_validate”的函数。所以,如果表单ID为user_register,那么表单的#validate属性将被设置为user_register_validate。
评论
勘误:http://www.drupalbook.com/
勘误:http://www.drupalbook.com/errata2?page=5 Page 225
"其中函数名为键,一个数组作为值," 应该为:“其中函数名作为值,”