I am using the CodeIgniter DataMapper ORM, but there is something that I don't quite understand.
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.
Inside the login
function, you can also see that it calls $this->validate()->get()
. When the validation
function is run, it should check against all rules from $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 us开发者_开发知识库e 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.
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?
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)
Datamapper's validation method ignores rules for fields not part of the object. So the confirm_password
rule won't trigged unless the object has a property by that field name.
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.
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.
Calling an object's validate() function is all that's needed to have the validation rules applied. Note that validate is automatically run whenever you perform a save() call without parameters. You can also run or validate()->get() on an object to get a matching record using the objects current field values.
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".
For login use form_validation library
and only verify username/password
, for registration
you can use DataMapper
and in you model add a rule confirm_password
must match password
but do not add required
rule in confirm_password
.. that should do it
精彩评论