[Catalyst] Where should constraints go

John Siracusa siracusa at gmail.com
Fri Nov 3 19:46:49 GMT 2006


On 11/3/06, Ian Docherty <ian at iandocherty.com> wrote:
> 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>

Warning!  Localization alert! :)  Like I said before, I don't think
you really want to be passing things to the view at that granularity.
It makes for some vary hairy form templates--one for each locale, to
boot.

If you whip up all this stuff *before* passing it off to the template,
you get templates like this (shown using Mason with a
"designer-friendly" syntax for the form/fields which just translates
deterministically into the corresponding method calls at runtime):

    <% '/messages.mc', %ARGS %>

    <% 'START_FORM' |fm %>

    <table>

    <tr>
    <td class="label"><% 'USERNAME:LABEL' |f %></td>
    <td class="field"><% 'USERNAME:FIELD' |f %></td>
    </tr>

    <tr>
    <td class="label"><% 'PASSWORD:LABEL' |f %></td>
    <td class="field"><% 'PASSWORD:FIELD' |f %></td>
    </tr>

    <tr>
    <td colspan="2"><% 'LOGIN_BUTTON:FIELD' |f %></td>
    </tr>

    </table>

    <% 'END_FORM' |fm %>

That's one form template for all supported languages, complete with
extremely granular, localized error messages.  (e.g., "The username
'whatever' is already taken.  Please choose another.")  The form-wide
messages go in the message box produced (or not, if no messages) at
the top by messages.mc.  The per-field errors are returned by the
"USERNAME:FIELD" calls and go under each field.

Even ignoring localization, you will save much sanity by constructing
all this stuff "perl-side" before passing it off to a template.

-John



More information about the Catalyst mailing list