On Mon, 14 May 2007, mla wrote:
There are two kinds of validation here. One is model-level validation, and
yes, it's in my model code. My model throws exceptions, which I trap in the
controller and "mess with" to make it work for the web UI.
The controller might also do some validation, but all it's doing is
validating things specific to the controller's environment (in this case,
the web UI).
Putting your model validation in the controller is a horrible violation of
the layering that makes MVC work. What do you do when you want to
insert/update/delete outside of the web UI?
The controller is the bridge between a specific environment (web, CLI, REST
API) and the model. That means _most_ of your code for an app should
probably be in the model. The controller is basically a simple translation
layer between the stuff the client provides and the model API.
That's makes total sense to me.
Could you give an example of how you munge the exceptions into error
messages for the user?
eval
{
$user->update( %bunch_of_stuff );
};
if ( my $e = Exception::Class->caught( 'My::App::Exception::DataValidation') )
{
# $e->errors contains multiple data validation error messages
# stuff them in the session
# save the user's form submission in the session
# redirect back to form
}
elsif ( my $e = $@ )
{
die $e;
}
Then on the display side, I check the session for error messages and saved
form arguments, and do something useful with them.
For example, say we have a User model with an email field. And we want
to ensure the e-mail format is somewhat sane. So you have something in
your DBIx::Class User model that checks the e-mail value whenever an
attempt is made to modify it. If it's invalid, you raise an exception
of what sort? An object exception or just a string?
Yes. An object (see above).
And then you need to map that back to a user message. The html form
could be asking for something very specific -- say, your mother's e-mail
address. So you need to convert the generic "e-mail invalid" error into
something like "Your mother's e-mail address is invalid". Something of
that sort?
I've never had to do that. If I did, I'd probably make my exception give
back more info, like a field name _plus_ an error message. Presumably, I
have a map from form field name to model object field name, so I can
reverse that mapping if needed.
The real point here is that you have all the information you need in your
controller to map from environment to model, so you also have the
information to do the reverse mapping as needed.
Obviously, you'll quickly find common patterns that can be abstracted into
base classes or traits or
whatever-you-like-for-handling-this-sort-of-abstraction.
-dave
/*===================================================
VegGuide.Org www.BookIRead.com
Your guide to all that's veg. My book blog
===================================================*/
_______________________________________________
List: [email protected]
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/