drupal javascript API 和表单设置错误

发布于 2024-10-20 04:58:05 字数 292 浏览 2 评论 0原文

1.有没有办法在客户端执行 form_set_error ,以便如果 javascript 验证中出现错误,它将设置错误并且不会让用户处理其他步骤? js 可以被禁用,所以我要格外小心...

  1. 哪里有像 Drupal.t 之类的 drupal javascript 函数参考的完整列表?

  2. 如何更改表单集错误的错误消息(默认错误如 { fieldname... field is required } ?

  3. 如何执行在字段下方/上方/内联显示的错误?

1.is there a way to do form_set_error in client side so if there is error in javascript validate it will set error and wont let user process the other steps? js can be disabled so i want to take extra caution...

  1. where is a complete list of drupal javascript function reference like Drupal.t and stuff?

  2. how can i change the error messages of form set error (the default errors like { fieldname... field is required } ?

  3. how can i do errors that will show below/above/inline the field ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-10-27 04:58:05

Drupal 中的 JavaScript,涵盖了 Drupal JavaScript API、AHAH 表单以及您正在寻找的内容。 快速入门指南非常好。

至于验证,你是对的,Javascript 可以关闭。 JavaScript 验证主要是为了可用性,因为用户不必等待 POST 表单来接收错误消息。 JavaScript 会在提交表单之前实时通知他,例如他的密码太弱或电子邮件无效。

然而 JavaScript 验证并不利于安全。这就是您需要进行服务器端验证的地方。 form_set_error 将负责服务器 -侧面验证。

因此,如果您有一个如下所示的表单:

function form_foo($form_state) {
    $form['foo'] = array(
      '#type' => 'textfield',
      '#title' => t('bar'),
      '#default_value' => $object['foo'],
      '#size' => 60,
      '#maxlength' => 64,
      '#description' => t('baz'),
    );
    return $form;
}

服务器端验证将如下所示:

function form_foo_validate($form, &$form_state) {
  if (empty($form_state['values']['foo'])) {
    form_set_error('foo', t('Foo cannot be empty.'));
  }
}

如果表单中的栏文本字段确实为空,则当用户提交表单时,栏将突出显示,“Foo 不能为空”错误消息将出现,并且不会调用表单 _submit 挂钩。

对于 JavaScript 功能,Drupal JavaScript API 概述文档包含您需要的大部分信息。

JavaScript in Drupal, Covers the Drupal JavaScript API, AHAH forms, and the kind of stuff your looking for. The Quick start Guide is pretty good.

As for validation, you're right, Javascript can be turned off. JavaScript validation is mostly done for usability, since the user doesn't have to wait to POST his form in order to receive an error message. JavaScript lets him know in real time if for example, his password is too weak, or email invalid, before submitting the form.

JavaScript validation however is not good for security. That's where you will need to do server-side validation. form_set_error will take care of the server-side validation.

So if you have a form that looks like:

function form_foo($form_state) {
    $form['foo'] = array(
      '#type' => 'textfield',
      '#title' => t('bar'),
      '#default_value' => $object['foo'],
      '#size' => 60,
      '#maxlength' => 64,
      '#description' => t('baz'),
    );
    return $form;
}

The server-side validation would look like:

function form_foo_validate($form, &$form_state) {
  if (empty($form_state['values']['foo'])) {
    form_set_error('foo', t('Foo cannot be empty.'));
  }
}

If the bar textfield in the form is indeed empty, when the user submits the form bar will be highlighted, the 'Foo cannot be empty' error message will appear, and the form _submit hook won't be called.

For JavaScript functionality, the Overview of Drupal JavaScript API document has most of the information you will need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文