开发者

How to do form validation in drupal?

How I valid开发者_高级运维ate form fields? For example, I want to ensure that two text boxes have the same value.


Implements the hook_form_alter() to add a validation callback (throught the #validate) argument.
In this callback you will have at your disposal the two values of the fields, you will simply have to had a statement to check the values and display an error message if the statement is not good.

Example:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'myform') {
    $form['#validate'][] = 'myvalidation_function';
  }
}

function myvalidation_function($form, &$form_state) {
  if ($form_state['values']['field_a'] != $form_state['values']['field_b']) {
    form_error('field_a', t('Field A and B must have the same values'));
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜