Drupal 6验证表单回调函数 [英] Drupal 6 Validation for Form Callback Function

查看:157
本文介绍了Drupal 6验证表单回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在节点显示页面上有一个带有选择菜单的简单窗体。有没有一个简单的方法来验证我的回调函数中的表单?通过验证,我并不意味着任何进展,只是为了检查数值是否存在于表单数组中。例如,没有ajax,如果我的选择菜单有3个项目,并添加第4个项目并尝试提交表单,drupal将给出一个错误,说出类似非法选择,请联系管理员。 p>

使用ajax,您创建的第四个项目将被保存到数据库中。所以我必须编写如

 如果($ 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,看起来像没有数据。

解决方案

这不是特别容易,但是标准的方法是使用Drupal Form API用于回调提交,也就是说,您将获得与非js提交相同的验证。



查看使用AHAH添加动态表单元素。虽然它与您的场景不完全匹配(他们重建回调中的表单以添加新元素,而不是保存数据),处理工作流程的解释非常有帮助。



然后有几个模块试图以通用方式提供AJAX表单提交 - 你可以检查他们的代码如何做(或者可能只是使用它们)





最后,努力放更好地将此功能支持到Drupal 7中的核心 - 相关讨论也可能有帮助。


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.

解决方案

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.

这篇关于Drupal 6验证表单回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆