简体   繁体   English

drupal javascript API和表单设置错误

[英]drupal javascript API & form set error

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? 1.有没有一种方法可以在客户端执行form_set_error,所以如果javascript验证有错误,它将设置错误,并且不会让用户处理其他步骤? js can be disabled so i want to take extra caution... 可以禁用js,所以我要格外小心...

  1. where is a complete list of drupal javascript function reference like Drupal.t and stuff? drupal javascript函数参考(如Drupal.t和东西)的完整列表在哪里?

  2. how can i change the error messages of form set error (the default errors like { fieldname... field is required } ? 如何更改表单设置错误的错误消息(默认错误如{fieldname ... field是必填项}?

  3. how can i do errors that will show below/above/inline the field ? 我该怎么办将显示在字段下方/上方/行内的错误?

JavaScript in Drupal , Covers the Drupal JavaScript API, AHAH forms, and the kind of stuff your looking for. JavaScript的在Drupal ,涵盖了Drupal的的JavaScript API,AHAH形式,和你找的那种东西。 The Quick start Guide is pretty good. 快速入门指南非常好。

As for validation, you're right, Javascript can be turned off. 至于验证,您是对的,可以关闭Javascript。 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验证主要是为了提高可用性,因为用户不必等待POST表单即可收到错误消息。 JavaScript lets him know in real time if for example, his password is too weak, or email invalid, before submitting the form. 例如,JavaScript会在提交表单之前实时告知他密码是否太弱或电子邮件无效。

JavaScript validation however is not good for security. 但是,JavaScript验证不利于安全性。 That's where you will need to do server-side validation. 那是您需要进行服务器端验证的地方。 form_set_error will take care of the server-side validation. form_set_error将负责服务器端验证。

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. 如果在表格栏文本框确实是空的,当用户提交表单栏将被强调的,“富不能为空”会出现错误消息,并且形式_submit钩子将不会被调用。

For JavaScript functionality, the Overview of Drupal JavaScript API document has most of the information you will need. 对于JavaScript功能, Drupal JavaScript API概述文档包含您将需要的大多数信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM