[Catalyst] Where should constraints go
Ian Docherty
ian at iandocherty.com
Fri Nov 3 19:16:17 GMT 2006
Matt S Trout wrote:
> Ian Docherty wrote:
>> My problem is finding a clean way of getting these constraints out of
>> the Model and into the View so that I can generate meaningful error
>> messages without hard-coding them in the templates. By 'clean' I mean
>> not having to code the Controller to act as the middle-man and just
>> pass them from the Model to the View.
>>
>> Perhaps I am expecting too much.
>
> No, I understand entirely, and you aren't expecting too much at all -
> except perhaps any expectation that this would already be fully
> implemented :)
>
> We're doing this in Reaction using Moose to provide an introspectable
> metamodel so the update action class reflects its constraints off the
> model and then the form reflects its field types off the update action
> and the fields just have the constraints "already there" when they're
> doing validation. It's been hard work and there's a lot of hard work
> still to come but so far it works bloody well.
>
How the hell do you find the time to work on Catalyst and on Reaction at
the same time!!!
One thing occurs to me. What I want to do is to get parameters from the
Model (or the Business layer) into the View via the stash. So for a User
object I want be able to do something like.
<td align="left" class="error">Username must be between [%
schema.user.username.min %] and [% schema.user.username.max %]
characters</td>
The only problem now is how to get this information from the DBIC data
in a DRY manner.
My model namespace is not in Catalyst as in:-
MyApp::Model::DBIC::User
but is in
MyApp::Schema::User
so that I can use it outside my Catalyst application (for example in
cron jobs) so I don't have a mechanism to do something like.
$c->forward->('MyApp::Model::DBIC::User/constraints')
Where 'constraints' would populate the stash with all the constraints
for the User table.
The only option I can see is to do a call everywhere that I need
constraints as follows.
$c->stash->{schema}{user} = MyApp::Model::DBIC::User::Constraints();
And in there do the following.
sub Constraints {
return {
username => {
min => 0,
max => 16,
},
firstname => {
min => 0,
max => 32,
},
};
}
Carl Franks has proposed a method using 'columns_info_for' which I will
have to give some more thought to and this may remove my worry that by
doing the above I am not doing it in a DRY manner.
More information about the Catalyst
mailing list