简体   繁体   English

CodeIgniter DataMapper ORM如何知道要应用哪组验证规则?

[英]How does the CodeIgniter DataMapper ORM know which set of validation rules to apply?

I am using the CodeIgniter DataMapper ORM, but there is something that I don't quite understand. 我正在使用CodeIgniter DataMapper ORM,但是有些事情我不太了解。

In this example, http://datamapper.wanwizard.eu/pages/examples/login.html , you can see that there are some $validation rules defined on the User model class. 在此示例http://datamapper.wanwizard.eu/pages/examples/login.html中 ,您可以看到在User模型类上定义了一些$validation规则。

Inside the login function, you can also see that it calls $this->validate()->get() . login函数内部,您还可以看到它调用$this->validate()->get() When the validation function is run, it should check against all rules from $validation . 运行validation功能时,应检查$validation中的所有规则。

What I don't understand is, for the login use case, only username and password need to be validated but you can see there are other validation rules unrelated to this use case in the example. 我不了解的是,对于登录用例,仅需要验证用户名和密码,但是在示例中可以看到与该用例无关的其他验证规则。 Specifically, there is a confirm_password rule defined on $validation and this rule obviously is only for the update use case, rather than the login use case. 具体来说,在$validation上定义了一个confirm_password规则,该规则显然仅用于更新用例,而不是登录用例。

Since I don't see any codes that bypass these unrelated rules in the example, how does the DataMapper ORM actually know these unrelated rules can be bypassed in the login function? 由于在示例中没有看到绕过这些不相关规则的代码,因此DataMapper ORM如何真正知道可以在login功能中绕过这些不相关的规则?

Many thanks to you all. 非常感谢你们。

Maybe the solution is to make 2 models: one is "login"(for table user), and other one is "register"(also for table user). 也许解决方案是制作两种模型:一种是“登录”(针对表用户),另一种是“注册”(也针对表用户)。 Then, when you want to login, just use login model of user. 然后,当您要登录时,只需使用用户的登录模型即可。 I think that this is true purpose of models. 我认为这是模型的真正目的。 (now you have 2 sets of validation in 2 models) (现在您在2个模型中有2套验证)

Calling an object's validate() function is all that's needed to have the validation rules applied. 要应用验证规则,只需调用对象的validate()函数。 Note that validate is automatically run whenever you perform a save() call without parameters. 请注意,只要执行不带参数的save()调用,validate就会自动运行。 You can also run or validate()->get() on an object to get a matching record using the objects current field values. 您还可以使用对象的当前字段值在对象上运行或validate()-> get()以获得匹配的记录。

http://datamapper.wanwizard.eu/pages/validation.html http://datamapper.wanwizard.eu/pages/validation.html

I think simply because validation will run using the objects current fields, and the "confirm_password" field is a "non-database table field". 我认为仅仅是因为验证将使用对象的当前字段运行,而“ confirm_password”字段是“非数据库表字段”。

对于登录用form_validation library ,只有验证username/password ,为registration就可以使用DataMapper ,并在你的模型中添加规则confirm_password必须匹配password ,但不添加required在规则confirm_password ..应该这样做

Datamapper's validation method ignores rules for fields not part of the object. Datamapper的验证方法将忽略不属于对象的字段的规则。 So the confirm_password rule won't trigged unless the object has a property by that field name. 因此,除非对象具有该字段名称的属性,否则confirm_password规则将不会触发。

Data validation rules should be in the model, not in the controller, as it is the only entry point to your data, and it ensures all data going into the database is validation. 数据验证规则应该在模型中,而不是在控制器中,因为它是数据的唯一入口点,并且可以确保进入数据库的所有数据都是验证数据。 It also answers to DRY, you don't want to define validation rules in every controller that uses the model. 它还回答了DRY,您不想在每个使用该模型的控制器中定义验证规则。

Given this fact, it is simple to define the rules for extra fields that might be on your CRUD forms as well and keep it all in one place. 鉴于这一事实,很容易为CRUD表单上可能存在的其他字段定义规则,并将其全部放在一个位置。

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

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