Drupal 表单验证对我不起作用
我正在尝试修改一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是表单更改,因此您不需要自己创建表单,因此您应该自己添加验证处理程序:
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:
Drupal will only by default use the validation that is defined as the
form_name
+_validate
. This is not the case since you are usinghook_form_alter
.编辑:基本上与 googletorps 相同的答案。发帖时没注意到他的。将其留在这里作为替代解释,但他的解释是正确的并且是第一个(+1)。
您的函数命名有些可疑:
与所有钩子实现一样,您的
hook_form_alter
函数应以您的自定义模块命名,而不是在表单之后命名(例如yourModule_form_alter
),在这种情况下,它将触发所有形式。如果(看起来)您只想挂钩到特定表单,请使用hook_form_FORM_ID_alter
,将“hook”替换为您的模块名称,并将“FORM_ID”替换为您要操作的表单的 id(名称)。表单的验证函数回调在
$form['#validate']
中以函数名称数组的形式列出。这只是您自己生成的表单的一个便捷快捷方式,您不必明确添加它,而是使用以表单命名的函数,并在末尾添加“_validate”。在您的情况下,您正在更改来自另一个模块的表单,因此您需要显式添加验证函数。所以你的代码应该是这样的:
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, usehook_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: