如何在 Drupal 7 中的字段小部件提交按钮上使用 ajax,而不验证表单的其余部分
我正在为分类术语参考制作一个新的小部件,其中提交按钮使 ajax 回调到 drupal 以更改表单。这就是我所拥有的:
$element['my_module_wrapper']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#ajax' => array(
'callback' => 'my_module_ajax',
'wrapper' => $field_name . '_my_module_container',
),
);
我的 ajax 调用工作正常,但它会验证整个表单,并在未填写所需的不相关字段时引发错误。我怎样才能阻止这个?
也可能相关,当调用 ajax 时,它似乎没有调用我的 hook_field_widget_form() 函数...这是否源于同一问题?
I'm making a new widget for taxonomy term references where a submit button makes an ajax call back to drupal to alter the form. Here's what I have:
$element['my_module_wrapper']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#ajax' => array(
'callback' => 'my_module_ajax',
'wrapper' => $field_name . '_my_module_container',
),
);
I have the ajax call working properly, but it validates the whole form, and throws errors when unrelated fields that are required aren't filled out. How can I stop this?
Also possibly related, it doesn't seem to call my hook_field_widget_form() function when ajax is called... is this stemming from the same problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将字段设置为 #type = 'button' 而不是 'submit'?据我所知,提交按钮将始终通过 for validate 数组运行,无论它是否是 ajax 表单项。
Have you tried making your field #type = 'button' instead of 'submit'? As far as I know, a submit button will always run through the for validate array whether it's an ajax form item or not.
查看此处给出的轮询模块的示例: http ://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#ajax
完整函数显示了如何使用“#limit_validation_errors”完成此操作。
Look at the example from Poll module given here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#ajax
The full function shows how it's done with '#limit_validation_errors'.