Welcome Guest, Not a member yet? Register   Sign In
basic but....
#13

[eluser]nmweb[/eluser]
Akelos
Code:
//controller
if($this->Post->save()){
      $this->redirectToAction("show", array('id' => $Post->getId()));
    }else{
      $this->Post->errors = $this->Post->getErrors();
    }
//Model
class Post extends ActiveRecord
{
    function validate(){
      $this->validatesPresenceOf( array('title', 'body'), "Missing required field");
      $this->get('title') > 10 ? $this->addError("title", "must be at most 10 characters") : null;
    }
}
Cakephp uses a similar approach. The save() method first tests the data for being valid before saving it into the database. Errors can then be retrieved. Validation and the rules are in a central place in the application and all data must pass.

When you have a table with a lot of fields and multiple forms to fill them, I don't see why the rules should therefore by stored in the controller. Each form field that is inserted into the db should be validated when an attempt to insertion is being made. The current CI_Validation falls short on that but that's no excuse to not do it. Sure you might have forms which are so complex that validation in the controller is easier, at this point you might want to retrieve the rules for the models concerned from the model.

No I don't have methods for each view, if I would need to do that, my model would be designed wrongly. If data is required in one form and not in another it'll lead to inconsistencies in your data. If you have properly designed your model and its validation you shouldn't run into any problems and you'll find that it is much easier.

You could always implement a save($validate=true) for exotic cases. I wrote a Form generation class which also does validation. I'm now writing something so I can pass my model (with rules) to that class and it'll generate a form of the fields I desire. The form generation class will also write my Javascript validation for usability purposes. To have this work properly I'll have to call the Form_generation->validate() method and may avoid calling the models validation methods. The validate() will check each form field against the rules retrieved from the model.

This way I have to write my rules once and it'll validate my forms everywhere with nice error messages and even javascript validation. All the way from the model through the controller to the view I have perfect validating forms with only a few lines of code. It's proper OOP so I can always for example add an extra javascript rule for usability purposes such as an ajax-username-exists rule or if my model doesn't exactly map to my form I can add or remove fields from the form. I can override rules, messages, anything.

Not validating in the model at all times, sure, you might want to do something like the above and forms don't always map to models. Not having rules in the model I find strange, try adding 300 characters to a varchar field and it'll trigger a crappy mysql error. Yet your controllers or views should have no knowledge of this limitation of varchar, it's the job of the model and using rules you can prevent 300chars from being added and trigger an error that is comprehensible for the user.


Messages In This Thread
basic but.... - by El Forum - 03-11-2008, 11:46 AM
basic but.... - by El Forum - 03-11-2008, 11:57 AM
basic but.... - by El Forum - 03-11-2008, 04:24 PM
basic but.... - by El Forum - 03-12-2008, 03:30 AM
basic but.... - by El Forum - 03-12-2008, 03:41 AM
basic but.... - by El Forum - 03-12-2008, 04:36 AM
basic but.... - by El Forum - 03-12-2008, 04:39 AM
basic but.... - by El Forum - 03-12-2008, 05:00 AM
basic but.... - by El Forum - 03-12-2008, 06:30 AM
basic but.... - by El Forum - 03-12-2008, 06:55 AM
basic but.... - by El Forum - 03-13-2008, 02:25 AM
basic but.... - by El Forum - 03-13-2008, 02:58 AM
basic but.... - by El Forum - 03-13-2008, 04:11 AM
basic but.... - by El Forum - 03-13-2008, 05:04 AM
basic but.... - by El Forum - 03-13-2008, 06:13 AM
basic but.... - by El Forum - 03-13-2008, 06:41 AM
basic but.... - by El Forum - 03-13-2008, 07:12 AM
basic but.... - by El Forum - 03-13-2008, 02:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB