简体   繁体   English

CodeIgniter 2,DataMapper验证规则

[英]CodeIgniter 2, DataMapper Validation Rules

I'm using CodeIgniter 2 with DataMapper ORM . 我正在将CodeIgniter 2与DataMapper ORM一起使用 For Users, I have confirm_password and confirm_email fields (plus others) which both aren't fields in the database (table users does not have these fields), but it's just there to show on the sign-up form: 对于用户,我有confirm_passwordconfirm_email字段(以及其他字段),它们都不是数据库中的字段(表users没有这些字段),但是可以在注册表单上显示这些字段:

I also have back-end where these 2 fields ( confirm_password and confirm_email ) do not exist in the form. 我也有后端,这两个字段( 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')
)

); );

If I don't make the confirm_email or confirm_email fields required , the validator won't trigger the matches rule. 如果我不作confirm_emailconfirm_email 必填字段,验证不会触发比赛规则。 If I make them required , then the back-end that does not have these fields, triggers the confirm_email and confirm_password , but it shouldn't. 如果我将它们设置为必填项 ,那么不具有这些字段的后端将触发confirm_emailconfirm_password ,但不是。

  • Is it best to include ALL possible validation rules (in the model of course) that we may have in the application? 最好在应用程序中包括所有可能的验证规则(当然是在模型中)吗?
  • Is it a good idea to alter these rules in the controller (say remove confirm_email index from $validation array) when adding user on back-end? 在后端添加用户时,更改控制器中的这些规则(例如从$validation数组中删除confirm_email索引)是一个好主意吗?

I appreciate any thoughts. 我感激任何想法。 Thanks 谢谢

Do you have the latest version of ORM Mapper, according to documentation you can add non database fields. 您是否具有最新版本的ORM Mapper,根据文档可以添加非数据库字段。

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

Also, you can now add validation rules for non-Database Table fields, such as 'Confirm Email Address' or 'Confirm Password'. 另外,您现在可以为非数据库表字段添加验证规则,例如“确认电子邮件地址”或“确认密码”。 For example: 例如:

and you don't need required rule for the confirmation fields - as they indicate that filed is required in DB hence the DB error) , - the matches property will do the required validation against the matched field (eg if it dosen't match it will throw the error.) In other words you only need required on 'email' field, confirmation_email will throw the error if filed don't match. 并且您不需要确认字段的必需规则-因为它们指示在DB中需要归档,因此出现DB错误),- match属性将对匹配字段进行所需的验证(例如,如果它不匹配)换句话说,您只需要在'email'字段中输入必填项,如果归档的内容不匹配,confirmation_email将引发错误。 On empty field you really need to show that email is required. 在空白字段中,您确实需要显示电子邮件是必需的。

Finally - you can remove the index, but generally thats not good idea. 最后-您可以删除索引,但是一般来说并不是一个好主意。 I would , instead, if above fails - add the form validation rule in controller. 如果上述方法失败,我将改为-在控制器中添加表单验证规则。

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

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