Drupal表单验证函数

发布于 2024-09-28 10:10:53 字数 285 浏览 5 评论 0原文

无论如何,Drupal 是否可以验证表单元素,例如电子邮件字段、密码、数字字段,自动验证让我们绑定一个系统验证器

$form['email] = array(
   '#title' => t('Email'),
   '#type' => 'textfield',
   '#validate_as' => array('email', ...),
   ...
);

Is there anyway say Drupal to validate form elements like email fields, passwords, numeric fields validate automatically lets say bind a system validator

$form['email] = array(
   '#title' => t('Email'),
   '#type' => 'textfield',
   '#validate_as' => array('email', ...),
   ...
);

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

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

发布评论

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

评论(4

瞳孔里扚悲伤 2024-10-05 10:10:53

要在 Drupal 中验证数字字段,请使用:

'#element_validate' => array('element_validate_number')

无需创建自定义验证函数。
http://api.drupal.org/api/drupal/includes%21form。 inc/function/element_validate_number/7

To validate a numeric field in Drupal use:

'#element_validate' => array('element_validate_number')

No need to create a custom validation function.
http://api.drupal.org/api/drupal/includes%21form.inc/function/element_validate_number/7

離殇 2024-10-05 10:10:53

里米安的观点既正确又错误。

Rimian 指出的好处是,您可以使用 #element_validate 将任何验证函数附加到表单字段。

但是,我不知道您可以调用一组表单 api 验证函数来测试最常见的事情,例如,值是否为:

  • int
  • 正 int
  • 有效日期(尽管日期模块中存在这样的函数)
  • 电子邮件-address(您可以使用 valid_email_address 来检查电子邮件,但您需要一个函数来引发验证错误)

因此,虽然您可以做到这一点,但这比您希望的要多一些工作,因为您需要自己创建这些验证函数。但完成此操作后,您可以通过 #element_validate 重用它们。

#element_validate 的使用主要集中在复杂的验证、日期验证、位置验证等方面,因为创建这些验证函数需要一些工作。大多数时候,您不需要验证那么多数字等(您可以在使用循环的正常验证函数中轻松完成此操作)。所以我不确定这对你有多大帮助,但这绝对是有可能的。

Rimian is both correct and wrong.

The good thing as Rimian points out, is that you can attach any validation function to your form fields using the #element_validate.

However I'm not aware of a set of form api validation functions you can call to test most common things like, if the value is:

  • a int
  • a positive int
  • a valid date (such a function exists in the date module though)
  • a email-address (you can use valid_email_address to check the email, but you need a function to raise validation error)

So while you can do this, it's a bit more work than you were hoping for, as you will need to create these validation functions yourself. But once you have done this, you can reuse them with #element_validate.

The use of #element_validate is mostly centered around complex validation fx date validation, location validation and such, as it requires some work to create these validation functions. Most of the time, you don't need to validate that many numbers etc (which you quite easily could do within a normal validation function using a loop). So I'm not sure how much this will be of help to you, but it's definitely a possibility.

恋竹姑娘 2024-10-05 10:10:53

表单 API 验证模块完全符合您的要求:http://drupal.org/project/fapi_validation

对于客户端验证还有 http://drupal.org/project/clientside_validation (可以使用规则由表单 API 验证提供)。

The Form API Validation module does exactly what you request: http://drupal.org/project/fapi_validation

For client-side validation there is also http://drupal.org/project/clientside_validation (which can use rules provided by Form API Validation).

万劫不复 2024-10-05 10:10:53

是的!

虽然我还没有进行太多尝试。

http://api.drupal.org/api /drupal/developer--topics--forms_api_reference.html/6#element_validate

$form = array(  
  '#type' => 'fieldset',  
  '#title' => t('Input format'),  
  '#collapsible' => TRUE,  
  '#collapsed' => TRUE,  
  '#weight' => $weight,  
  '#element_validate' => array('filter_form_validate'),  
);  

Yep!

Though I have not experimented with it much.

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#element_validate

$form = array(  
  '#type' => 'fieldset',  
  '#title' => t('Input format'),  
  '#collapsible' => TRUE,  
  '#collapsed' => TRUE,  
  '#weight' => $weight,  
  '#element_validate' => array('filter_form_validate'),  
);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文