[Catalyst] Form validation in insert() and update()?

mla maurice.aubrey at gmail.com
Tue May 15 21:04:42 GMT 2007


Matt S Trout wrote:
> On Tue, May 15, 2007 at 02:51:18PM -0400, Christopher H. Laco wrote:
>>> where do you handle the validation? Only in the controller or in
>>> both the model and controller?
>> Fail Early. Fail Often.
>>
>> Some will say redundancy sucks. I agree, except for where validation is
>> concerned. If you're writing data from the web into a model, check the
>> data at the page level, and at the model level.
>>
>> If you're writing to a model from a command line utility...check data at
>> the command line level, and the model level...
> 
> The controller should validate.
> 
> The model should also validate.
> 
> The model should provide a way for the controller to get the constraints.
> 
> The controller should check the constraints and return errors to the user,
> and if there are errors NOT TRY AND UPDATE THE MODEL.
> 
> The model's validation should die screaming if it doesn't pass - if bad data
> gets that far, either the model's changed under you or there's a bug in
> your code.

Okay, thanks very much for this. So in terms of the model constraints,
you will validate everything twice. Once at the controller layer (where
it leverages info from the model), and once in the model itself.

So you can interrogate the model to find out, for example, that the
email column can't be longer than 100 characters and should match the
pattern /@/ (just to keep it simple here).

Do you expose something like a Model->valid_email() method that can be
used both within the model and from the controller?

Maurice



More information about the Catalyst mailing list