Drupal表单验证函数
无论如何,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在 Drupal 中验证数字字段,请使用:
无需创建自定义验证函数。
http://api.drupal.org/api/drupal/includes%21form。 inc/function/element_validate_number/7
To validate a numeric field in Drupal use:
No need to create a custom validation function.
http://api.drupal.org/api/drupal/includes%21form.inc/function/element_validate_number/7
里米安的观点既正确又错误。
Rimian 指出的好处是,您可以使用
#element_validate
将任何验证函数附加到表单字段。但是,我不知道您可以调用一组表单 api 验证函数来测试最常见的事情,例如,值是否为:
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:
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.表单 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).
是的!
虽然我还没有进行太多尝试。
http://api.drupal.org/api /drupal/developer--topics--forms_api_reference.html/6#element_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