Like CI Form Validation is there any easy way to have multiple (Groups) validation in DataMapper Model Extension? |
[eluser]dmyers[/eluser]
I have really been digging into datamapper and was wondering if like CodeIgniter's Form Validation which has validation "groups" is there an easy way to have "groups" on a datamapper model? http://ellislab.com/codeigniter/user-gui...ation.html For example on 1 form I have every field BUT email (or whatever) if I try to validate the form (via the datamodel) it fails because I haven't set the "email" field. Perhaps a "not check" value of some kind??? What I ended up doing is this in which I change the validation via a function call. Any body else have any ideas? Code: $user = new User(); Code: class User extends DataMapper {
[eluser]dmyers[/eluser]
Ok, this is what I came up with I think it's the easiest. Adding this Code: function only_validate($inp=null) { to your class and/or datamapper you can use it like this. Code: $u->only_validate('first'); Unit Test against each Code: $u = new user(); Then you can leave you validation in place and turn off what you need depending on the form requirements. What do you think?
[eluser]OverZealous[/eluser]
Sorry this is so much later, but I just read your post and wanted to make a suggestion: When saving an existing object, you should load that object in, first. This allows DM to determine which fields have changed, and only update those fields. Code: $u = new User(); If it is a new object, every required field should be filled out (I mean, it's required, right?). If the field is sometimes required, and sometimes not, I have a simple trick that works really well. I add a _required method to the model, which I can then always return TRUE on. This prevents errors for empty fields. Then I have a custom validation routine that determines if that field is necessary or not: Code: $validation = array( You will need to have a language key called "check_names" with a message like: "You must include either a first name or a last name." |
Welcome Guest, Not a member yet? Register Sign In |