Feature Request #1864
ORM Validate() is backwards
| Status: | Closed | Start date: | 07/23/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | Libraries:ORM | |||
| Target version: | 2.4 | |||
| Resolution: | fixed | Points: |
Description
the purpose of validation in the model is that it validates the model. The way ORM validate() currently works, is an array of savable data(generally through $_POST) is passed to it, which is then set to the model, and then checked for validation.
These should be two separate steps, though it probably couldn't hurt to have a ORM helper function to set all array key/value pairs to a model, something like a set_fields();
so ideally, a person wishing to validate $_POST through his ORM model would:
either loop through post and do $model->$key = $value; or something like $model->set_fields($_POST);
then do $model->validate(true);//passing true to save automatically if post validates.
i'd love to get opinions on this.
Related issues
History
Updated by Jeremy Bush almost 4 years ago
- Status changed from New to Review
This is how it works in my Auto Modeler, and it make much more sense.
Updated by Isaiah DeRose-Wilson almost 4 years ago
We can already use ORM::load_values() for setting all the fields key/value pairs to a model. What if we made the first argument in the validate function optional, so you could still set the values, validate and save all in one easy function call if you wanted?
Updated by John Heathco almost 4 years ago
I like Isaiah's idea. Also, I think the validation object should be more tightly integrated with ORM, perhaps a property of each ORM. It already sounds like this is what you guys are thinking.
It would be nice if the validate() method in ORM would use the validate object that each ORM would possess, or an optional argument could be passed to override and specify your own validate object.
Updated by Ben Rogers almost 4 years ago
i'm not opposed to the optional validate() parameter which would be passed to load_values(by the way thanks for finding that isaiah, i was sure something existed like that, just couldnt find it), however i think that it doesnt make sense for the scope of the validate() function. If were changing this so validation occurs to the model, and not the posted data, it makes less readable sense to the programmer that this is whats happening if we are still passing $_POST or a similar array to the validate() function.
besides, it's not that hard to do a $model->load_values($_POST); $model->validate();
Updated by Jeremy Bush almost 4 years ago
John Heathco wrote:
I like Isaiah's idea. Also, I think the validation object should be more tightly integrated with ORM, perhaps a property of each ORM. It already sounds like this is what you guys are thinking.
I'd suggest looking at Auto_Modeler validation, as this is exactly how it works.
http://dev.kohanaphp.com/projects/automodeler/repository/entry/trunk/libraries/Auto_Modeler.php#L106
Updated by John Heathco almost 4 years ago
On an unrelated note, I think saved and loaded should become methods so that these can be used as database field names, no?
Updated by Chris Bandy almost 4 years ago
Another feature/functionality to keep in mind is filtering unwanted $_POST/input data. ORM::validate depends on defined rules and safe_array() while AM hardcodes column names.
Updated by Ben Rogers almost 4 years ago
@jheathco: agreed on the loaded/saved becoming methods
@cbandy: this is already solved if you use $model->load_values(), which is the point of this ticket, as that will ignore $_POST fields not found in the model.
Updated by Chris Bandy almost 4 years ago
That is not currently the case, which is why I bring it up.
Updated by Martin Rampersad almost 4 years ago
load_values doesn't set the $changed member in ORM, which I think save looks at to decide how to form the update query. perhaps that's a bug?
Updated by Ben Rogers over 3 years ago
- Assignee changed from John Heathco to Ben Rogers
Updated by Ben Rogers over 3 years ago
- Status changed from Review to Assigned
Updated by Ben Rogers over 3 years ago
just confirming the changes to be made in this ticket, much discussion occured in #kohana-dev between myself and zombor on the topic:
- loaded/saved to become functions(allowing db fields to be named loaded/saved
- validate() to have one optional parameter, a validation object to be passed instead of creating its own
- load_values() needs to be fixed so it updates ORM::$changed
- the appropriate way to pass $_POST to validate() is via load_values(). the appropriate way to save after validate() is save()
- save will call validate if the unchanged record was not yet validated.
- validate() will throw an exception(Database_Validation_Exception) if the model doesnt pass validation
- user will no longer overload validate() but fill in validation rules to an array in their models
Updated by Bruno Heridet - Delapouite over 3 years ago
Really good news to port the Auto_Modeler validation philosophy to ORM.
Updated by Ben Rogers over 3 years ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
Applied in changeset r4536.
Updated by Ben Rogers over 3 years ago
- Resolution set to fixed