Drupal Web 表单验证 (webform_form_alter)
我正在使用 webform_form_alter 进行一些网络表单验证。我使用 webform_form_alter 因为我在“选择”字段上切换某些内容。
在我的 webform-form-317.tpl.php 中,我定义了新的字段集,我将字段设置到这个新的字段集中,并从网络表单中取消设置原始字段集。
$form['submitted']['ContactInfo'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="ContactInfo">',
'#suffix' => '</div>',
'#weight' => -10,
'#title' => 'Contact Information'
);
$form['submitted']['ContactInfo']['phone_home'] = $form['submitted']['phone_home'];
unset($form['submitted']['phone_home']);
在我的 webform_form_alter 中,我有以下代码:
switch ($form_id)
{
case 'webform_client_form_317':
{
$form['#validate'][] = 'validate_form';
}
}
我的 Validate_form 函数如下所示:
function validate_form($form_id, $form_values)
{
if ($form_values['submitted_tree']['ContactInfo']['phone_home'] == "")
{
form_set_error('phone_error', t('Please enter a home phone number.'));
}
}
问题是 $form_values['subscribed_tree']['ContactInfo']['phone_home'] 没有返回,即使用户在其中输入了某些内容文本字段。
关于我做错了什么有什么建议吗?
作为第二个问题,如果有人也给出了答案,我如何修改这些文本字段以将类设置为“表单文本必需错误”,以便它们与其余必填字段一起显示为红色。
谢谢
I'm doing some webform validation using webform_form_alter. I'm using webform_form_alter because I switch certain content on a "select" field.
In my webform-form-317.tpl.php I defined new fieldsets I set my fields into this new fieldset and unset the original from the webform.
$form['submitted']['ContactInfo'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="ContactInfo">',
'#suffix' => '</div>',
'#weight' => -10,
'#title' => 'Contact Information'
);
$form['submitted']['ContactInfo']['phone_home'] = $form['submitted']['phone_home'];
unset($form['submitted']['phone_home']);
in my webform_form_alter I have the following code:
switch ($form_id)
{
case 'webform_client_form_317':
{
$form['#validate'][] = 'validate_form';
}
}
My Validate_form function looks like:
function validate_form($form_id, $form_values)
{
if ($form_values['submitted_tree']['ContactInfo']['phone_home'] == "")
{
form_set_error('phone_error', t('Please enter a home phone number.'));
}
}
The issue is that the $form_values['submitted_tree']['ContactInfo']['phone_home'] comes back as nothing even is the user has inputted something into the textfield.
Any suggestions on what i'm doing wrong?
As a second question in case somebody also the answers, how do I modify the of these textfields to set the class for "form-text required error" so they show up in red with the rest of the mandatory fields.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望您不要在 webform 模块中编写此代码,而是为其创建了一个自定义模块。
在第一种情况下,您的函数应该是
如果您正在谈论错误类,Drupal 将其添加到所有具有错误集的字段,就像上面的代码所做的那样。您需要将表单字段的名称作为第一个参数传递给
form_set_error
函数。I hope you don't write this code in the webform module, but have made your a custom module for it.
In the first case, your function should be
If you are talking about the error class, Drupal add it to all fields that has an error set like is done on the above code. You need to pass in the name of the form field as first param to the
form_set_error
function.