[Fwd: [Html-widget] formfu validation proposals]

Mario Minati mario.minati at googlemail.com
Mon Mar 12 20:54:32 GMT 2007


Am Montag 12 März 2007 12:41 schrieben Sie:
> -------- Original-Nachricht --------
> Betreff: 	[Html-widget] formfu validation proposals
> Datum: 	Thu, 8 Mar 2007 10:12:11 +0000
> Von: 	Carl Franks <fireartist at gmail.com>
> Antwort an: 	html-widget at lists.rawmode.org
> An: 	html-widget list <html-widget at lists.rawmode.org>
>
>
>
> The old HTML-Widget process() simply ran all constraints, then all
> filters, and that was your lot.
>
> FormFu currently goes a step further with:
>  * constraints
>  * filters
>  * inflators
Already nice

> I propose making it a lot more powerful and flexible, with the following
> steps:
>  * filter 
>  * constrain
>  * inflate
>  * validate
>  * transform
>
> Any of these steps (except 'filter') will be able to throw an
> exception, which will be displayed to the user as a field error.
> Each step will only proceed if there are no errors from previous steps.
>
> The first filter for each field will be fed the raw input for that
> field only, and it's output will be piped to the next filter for that
> field.
> Likewise, with 'inflate', the output is piped from one inflate handler
> to the next.
> The same goes for 'transform', but the first transform handler will
> receive the inflated value.
>
> The purpose of the filter handlers would be to cleanup input before
> validation, so you might strip leading and trailing whitespace with
> the 'TrimEdges' filter. Or you might use the 'NonNumeric' filter to
> remove any spaces or hyphens entered into a credit card number field.
>
> The purpose of the 'constrain' handlers would be to check such
> low-level things such as "is this in range?", "does this contain valid
> characters?" or "is this an email address?".
>
> The purpose of the 'inflate' handlers would be to, for example, turn a
> date into a DateTime object, or turn a file upload into an Imager
> object.
>
> The purpose of the 'validate' handlers would be to check higher-level
> (business) rules, and they will have access to the inflated values of
> all fields. So you could check, for example, "is this date after that
> date?" or "is 'c' only filled in if 'a' and 'b' are?".
>
> Because these are likely to contain more-complex logic, it would be
> expected that the user would create a new subclass of
> HTML::Widget::Validate for each business rule, which would contain the
> programmatic logic. So in your form setup, you only need refer to the
> validation handler by name (otherwise, you'd end up trying to program
> in yaml - nasty!).

Wow, such a beauty :-))

May be can give a modul and / or function name or catalyst ation to call in 
the yaml. This way we could seperate the form showing function and the 
validation function and we could use the transform functions to store the data 
in the model. 
And keep the code seperated from the form with very little effort of 
connecting both pieces. Just a quick thought.

> And, of course, if you're using Catalyst::Controller::HTML::FormFu,
> your validation code will have access to the catalyst context via the
> form's stash.
>    $self->form->stash->{context}

Thanks Carl, I'll make havy use of it.

> The 'transform' handler is provided as a further hook to do anything
> else necessary after all validation is complete.
>
> You might notice each of these steps are a verb name. This would allow
> me to get rid of the 'plural' aliases filter/filters,
> constraint/contraints, which I don't like for some reason ;)
>
> So, to take the example of John Napiorkowski's HTML-Widget filters
> which use Imager to resize uploaded image files to a standard size...
>
> I would suggest there should be a HTML::FormFu::Inflate::Imager which
> simply reads an uploaded file and returns an Imager object.
> There would then be a HTML::FormFu::Transform::Imager which allows you
> to call methods on that Imager object.

Very good idea.

> The yaml config for a field might be something like:
>
> ---
> element:
>  - type: file
>    name: avatar
>    inflate:
>      - Imager
>    transform:
>      - type: Imager
>        scaleX: [pixels, 100]
>
> Any ideas / criticisms?
>
Greets,
Mario



More information about the Html-widget mailing list