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 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.
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.
Datamapper’s validation method ignores rules for fields not part of the object. So the
confirm_passwordrule 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.