繁体   English   中英

CodeIgniter 2,DataMapper验证规则

[英]CodeIgniter 2, DataMapper Validation Rules

我正在将CodeIgniter 2与DataMapper ORM一起使用 对于用户,我有confirm_passwordconfirm_email字段(以及其他字段),它们都不是数据库中的字段(表users没有这些字段),但是可以在注册表单上显示这些字段:

我也有后端,这两个字段( confirm_passwordconfirm_email )在表单中不存在。

public $validation = array(
'first_name' => array(
    'label' => 'lang:common_first_name',
    'rules' => array('required', 'trim')
),
'last_name' => array(
    'label' => 'lang:common_last_name',
    'rules' => array('trim')
),
'email' => array(
    'label' => 'lang:common_email',
    'rules' => array('required', 'trim', 'unique', 'valid_email')
),
'confirm_email' => array(
    'label' => 'lang:common_confirm_email',
    'rules' => array('matches' => 'email')
),
'password' => array(
    'label' => 'lang:common_password',
    'rules' => array('required', 'min_length' => 6, 'encrypt')
),
'confirm_password' => array(
    'label' => 'lang:common_confirm_password',
    'rules' => array('matches' => 'password')
)

);

如果我不作confirm_emailconfirm_email 必填字段,验证不会触发比赛规则。 如果我将它们设置为必填项 ,那么不具有这些字段的后端将触发confirm_emailconfirm_password ,但不是。

  • 最好在应用程序中包括所有可能的验证规则(当然是在模型中)吗?
  • 在后端添加用户时,更改控制器中的这些规则(例如从$validation数组中删除confirm_email索引)是一个好主意吗?

我感激任何想法。 谢谢

您是否具有最新版本的ORM Mapper,根据文档可以添加非数据库字段。

http://stensi.com/datamapper/pages/validation.html

另外,您现在可以为非数据库表字段添加验证规则,例如“确认电子邮件地址”或“确认密码”。 例如:

并且您不需要确认字段的必需规则-因为它们指示在DB中需要归档,因此出现DB错误),- match属性将对匹配字段进行所需的验证(例如,如果它不匹配)换句话说,您只需要在'email'字段中输入必填项,如果归档的内容不匹配,confirmation_email将引发错误。 在空白字段中,您确实需要显示电子邮件是必需的。

最后-您可以删除索引,但是一般来说并不是一个好主意。 如果上述方法失败,我将改为-在控制器中添加表单验证规则。

暂无
暂无

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

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