Remote form validation

| | Comments (0) | TrackBacks (0)
Catalyst and ExtJS are my favourite frameworks for developing web applications. ExtJS provides a lot features to validate form data before the form is being submitted to the server. Everybody knows that you never should trust those values even if it was validated in the browser by ExtJS. Here is a comfortable way to validate form values remotely using Catalyst::Plugin::FormValidator::Simple.

The full example can be found on github:
git clone git://github.com/plu/rfv-example.git


  • Create an ExtJS FormPanel:
        var win = new Ext.Window({
            .....
            ,items:[
                new Ext.FormPanel({
                    ,url:'[% Catalyst.uri_for('/save') %]'
                    ,id: 'fp'
                    .....
                    ,items: [{
                            fieldLabel: 'First name',
                            name: 'first',
                            anchor:'100%'
                        },{
                            fieldLabel: 'Last name',
                            name: 'last',
                            anchor:'100%'
                        },{
                            fieldLabel: 'Company',
                            name: 'company',
                            anchor:'100%'
                        },{
                            fieldLabel: 'eMail',
                            name: 'email',
                            anchor:'100%'
                        }
                    ]
    
                    ,buttons: [{
                        text: 'Save',
                        handler: function() {
                            win.findById('fp').getForm().submit();
                        }
                    }]
                })
            ]
        });
    
  • Read validation results and modify form fields:
        var fp = win.findById('fp');
        fp.on('actioncomplete', function(a, fp) {
            var r = fp.result;
            for (var field in r.error) {
                fp.form.findField(field).markInvalid(
                    r.error[field].join("<br/>")
                );
            }
        });
    
  • Validate form values:
    sub save : Global {
        my ( $self, $c ) = @_;
    
        $c->form(
            first   => [ qw/NOT_BLANK ASCII/ => [qw/LENGTH 2 20/] ],
            last    => [ qw/NOT_BLANK ASCII/ => [qw/LENGTH 2 20/] ],
            company => [ qw/NOT_BLANK ASCII/ => [qw/LENGTH 2 20/] ],
            email   => [qw/NOT_BLANK EMAIL_LOOSE/],
        );
    
        my $json : Stashed = {
            error   => $c->form->field_messages('save'),
        };
    }
    

0 TrackBacks

Listed below are links to blogs that reference this entry: Remote form validation.

TrackBack URL for this entry: http://www.pqpq.de/mt-tb.cgi/3

Leave a comment

About this Entry

This page contains a single entry by Johannes Plunien published on July 26, 2008 11:17 AM.

MojoMojo - make it private was the previous entry in this blog.

Firebug debugging fallback is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.