Drupal 6 表单回调函数验证
我有一个简单的表单,在节点显示页面上有一个选择菜单。有没有一种简单的方法可以在我的回调函数中验证表单?通过验证,我并不是指任何高级的东西,只是为了检查表单数组中是否实际存在这些值。例如,如果没有 ajax,如果我的选择菜单有 3 个项目,而我添加了第 4 个项目并尝试提交表单,drupal 将给出类似“做出了非法选择,请联系管理员”的错误。
使用 ajax,您创建的第四个项目将保存到数据库中。那么我是否必须编写验证,
if ($select_item > 0 && $select_item <= 3) {
//insert into db
}
或者是否有一种更简单的方法来检查该项目是否确实存在于表单数组中?我希望没有ajax,如果表单被操纵,drupal将不会提交表单。谢谢。
编辑: 所以我基本上在我的回调函数中需要这个?
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
为了获取 $_POST['form_build_id'],我将其作为数据参数发送,对吗?我使用form_get_cache的地方,看起来没有数据。现在有点迷失了。
I have a simple form with a select menu on the node display page. Is there an easy way to validate the form in my callback function? By validation I don't mean anything advanced, just to check that the values actually existed in the form array. For example, without ajax, if my select menu has 3 items and I add a 4th item and try to submit the form, drupal will give an error saying something similar to "an illegal choice was made, please contact the admin."
With ajax this 4th item you created would get saved into the database. So do I have to write validation like
if ($select_item > 0 && $select_item <= 3) {
//insert into db
}
Or is there an easier way that will check that the item actually existed in the form array? I'm hoping there is since without ajax, drupal will not submit the form if it was manipulated. Thanks.
EDIT:
So I basically need this in my callback function?
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
To get $_POST['form_build_id'], I sent it as a data param, is that right? Where I use form_get_cache, looks like there is no data. Kind of lost now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您已经在使用 AJAX,为什么不编写一些 jQuery 来仅在选项位于合法选项列表内时才允许表单提交呢?这可以在您正在处理的自定义模块中完成(使用 drupal_add_js())。
Since you're already using AJAX, why not just write a bit of jQuery to only allow form submission if the choice is within the list of legal choices? This can be done within the custom module it already looks like you're working on (using drupal_add_js()).
这并不是特别“简单”,但标准方法是使用 Drupals Forms API 进行回调提交 - 这样,您将获得与非 js 提交相同的验证。
查看使用 AHAH 添加动态表单元素。虽然它与您的场景不完全匹配(它们在回调上重建表单以添加新元素,而不是保存数据),但处理工作流程的解释非常有帮助。
然后有几个模块尝试以通用方式提供 AJAX 表单提交 - 您可以检查它们的代码以了解如何执行此操作(或者可能只是使用它们;)
最后,努力将此功能更好地支持到 Drupal 的核心中7 - 相关讨论也可能有帮助。
It is not especially 'easy', but the standard way to do it would be to use Drupals Forms API for the callback submission as well - that way, you'll get the same validation that would happen on a non js submit.
Take a look at Adding dynamic form elements using AHAH. While it does not match your scenario exactly (they rebuild the form on the callback to add new elements, not to save data), the explanation of the processing workflow is pretty helpful.
Then there are several modules that try to offer AJAX form submission in a generic way - you could check their code on how to do it (or maybe just use them ;)
Finally, there are efforts to put better support this functionality into core in Drupal 7 - the related discussions might also help.