[Catalyst] Design of a new form validator

Sebastian Riedel sri at oook.de
Thu Dec 1 08:05:09 CET 2005


Here are some first preview releases. :)

     http://files.oook.de/HTML-Widget-1.0.tar.gz
     http://files.oook.de/Catalyst-Plugin-HTML-Widget-1.0.tar.gz

All the basics are implemented, missing are just JavaScript  
validators, inline error messages and more Elements/Constraints.
Constraints should also get a bit more clever in the future, so they  
can auto-select/generate the right Element for you. ;)

I've picked lots of features from DFV, DFV::Simple,  
CGI::FormBuilder...yada yada.
FillInForm is also built-in and is much faster because we don't have  
to use a HTML parser!

Examples:

     use HTML::Widget;

     # Create a widget
     my $w = HTML::Widget->new( { method => 'GET', action => '/foo/ 
action' } );

     # Add some Elements
     $w->element( 'Textfield', 'age' )->label('Age')->size(3);
     $w->element( 'Textfield', 'name' )->label('Name')->size(60);
     $w->element( 'Submit', 'ok' )->value('OK');

     # Add some Constraints
     $w->constraint( 'Integer', 'age' )->message('No integer.');
     $w->constraint( 'Required', 'age', 'name' )->message('Missing  
value.');

     # Process it to a form
     my $form = $w->process;
     my $form = $w->process($query);

     my $xml = "$form";   # Yes, this really generates all the xml!

     [% form %]

     [% FOREACH element = form.errors %]
         <p>
         [% element %]:<br/>
         <ul>
         [% FOREACH message = form.messages(element) %]
             <li>[% element %]:</li>
         [% END %]
         </ul>
         </p>
     [% END %]

     <p><ul>
     [% FOREACH element = form.errors %]
         [% IF result.error( element, 'Integer' ) %]
             <li>[% element %] has to be an integer.</li>
         [% END %]
     [% END %]
     </ul></p>


     # And the Catalyst version ;)
     use Catalyst qw/HTML::Widget/;

     $c->widget( { method => 'GET', action => '/foo/action' } );

     $c->widget->element( 'Textfield', 'age' )->label('Age')->size(3);
     $c->widget->element( 'Textfield', 'name' )->label('Name')->size 
(60);
     $c->widget->element( 'Submit', 'ok' )->value('OK');

     $c->widget->constraint( 'Integer', 'age' )->message('No integer.');
     $c->widget->constraint( 'Required', 'age', 'name' )
         ->message('Missing value.');

     my $form = $c->widget->process;
     my $form = $c->widget->process($query);


Not bad for 6 hours of work, eh? Now I better try to sleep a bit...


--
sebastian




More information about the Catalyst mailing list