Drupal 表单验证对我不起作用

发布于 2024-08-23 16:44:53 字数 717 浏览 15 评论 0原文

我正在尝试修改一些 Drupal 6 表单代码并合并一些本机表单验证。代码看起来像这样,但验证不起作用。我什至从未进入过 thisFormName_form_validate 函数。 Drupalians 有什么好主意吗?

function thisFormName_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );


function thisFormName_form_validate($node, &$form) {
  if ($form_state['values']['email_address'] == '') 
  {
    form_set_error('', t('Email must be valid format if entered.'));

  }
}

I am trying to modify some Drupal 6 form code and incorporate some native form validation. Code looks like this, but validation does not work. I never even get into function thisFormName_form_validate. Any Drupalians have some good ideas?

function thisFormName_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );


function thisFormName_form_validate($node, &$form) {
  if ($form_state['values']['email_address'] == '') 
  {
    form_set_error('', t('Email must be valid format if entered.'));

  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2024-08-30 16:44:53

由于您使用的是表单更改,因此您不需要自己创建表单,因此您应该自己添加验证处理程序:

function myModule_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );
  $form['#validate'][] = 'my_validation_function';
}


function my_validation_function(&$form, &$form_state) {
  if ($form_state['values']['email_address'] == '') {
    form_set_error('', t('Email must be valid format if entered.'));
  }
}

Drupal 默认情况下只会使用定义为 form_name + 的验证_验证。事实并非如此,因为您使用的是 hook_form_alter

Since you are using form alter, so you don't create the form yourself, you should add the validation handler yourself:

function myModule_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );
  $form['#validate'][] = 'my_validation_function';
}


function my_validation_function(&$form, &$form_state) {
  if ($form_state['values']['email_address'] == '') {
    form_set_error('', t('Email must be valid format if entered.'));
  }
}

Drupal will only by default use the validation that is defined as the form_name + _validate. This is not the case since you are using hook_form_alter.

梦回梦里 2024-08-30 16:44:53

编辑:基本上与 googletorps 相同的答案。发帖时没注意到他的。将其留在这里作为替代解释,但他的解释是正确的并且是第一个(+1)。


您的函数命名有些可疑:

  • 与所有钩子实现一样,您的 hook_form_alter 函数应以您的自定义模块命名,而不是在表单之后命名(例如 yourModule_form_alter),在这种情况下,它将触发所有形式。如果(看起来)您只想挂钩到特定表单,请使用 hook_form_FORM_ID_alter,将“hook”替换为您的模块名称,并将“FORM_ID”替换为您要操作的表单的 id(名称)。

  • 表单的验证函数回调在 $form['#validate'] 中以函数名称数组的形式列出。这只是您自己生成的表单的一个便捷快捷方式,您不必明确添加它,而是使用以表单命名的函数,并在末尾添加“_validate”。在您的情况下,您正在更改来自另一个模块的表单,因此您需要显式添加验证函数。

所以你的代码应该是这样的:

function yourModuleName_theFormID_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );
  // Add custom validation callback
  $form['#validate'][] = 'yourModuleName_theFormID_validate';


function yourModuleName_theFormID_validate(&$form, &$form_state) {
  if ($form_state['values']['email_address'] == '') {
    form_set_error('', t('Email must be valid format if entered.'));
  }
}

EDIT: Basically the same answer as googletorps. Didn't notice his while posting mine. Leaving it in here for the alternate explanation, but his is correct and was first (+1).


There is something fishy about your function naming:

  • As with all hook implementations, your hook_form_alter function should be named after your custom module, not after the form (e.g. yourModule_form_alter), in which case it would trigger for all forms. If (as it looks) you only want to hook into a specific form, use hook_form_FORM_ID_alter, replacing 'hook' with with your module name, and 'FORM_ID' with the id (name) of the form you want to manipulate.

  • The validation function callbacks for a form are listed as an array of function names in $form['#validate']. It is only a convenience shortcut for forms generated by yourself that you do not explicitly have to add that, but use a function named after the form, adding a '_validate' at the end. In your case, you are altering a form coming from another module, so you need to add the validation function explicitly.

So your code should look something like this:

function yourModuleName_theFormID_form_alter(&$form, $form_state, $form_id) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your email address (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 4,
    '#size' => 60,
    '#maxlength' => 128,  
    '#description' => t('Enter email address.'),
  );
  // Add custom validation callback
  $form['#validate'][] = 'yourModuleName_theFormID_validate';


function yourModuleName_theFormID_validate(&$form, &$form_state) {
  if ($form_state['values']['email_address'] == '') {
    form_set_error('', t('Email must be valid format if entered.'));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文