package MyApplication::Controller::User; use strict; use MyApplication::Form::User; sub edit : Local { my ( $self, $c, $id ) = @_; # Create the form object my $form = MyApplication::Form::User->new( $id ); # Update or create the user record if form posted # and form validates $form->update_from_from( $c->request->parameters ) if $c->form_posted; $c->stash->{form} = $form; } #### use HTML::FormFu; my $form = HTML::FormFu->new; $form->load_config_file('form.yml'); $form->process( $cgi_query ); if ( $form->submitted_and_valid ) { # do something with $form->params } else { # display the form $template->param( form => $form ); } #### # This is the run mode that will be validated. Notice that it accepts # some errors to be passed in, and on to the template system. sub form_display { my $self = shift; my $errs = shift; my $t = $self->load_tmpl('page.html'); $t->param($errs) if $errs; return $t->output; } sub form_process { my $self = shift; use CGI::Application::Plugin::ValidateRM (qw/check_rm/); my ($results, $err_page) = $self->check_rm('form_display','_form_profile'); return $err_page if $err_page; #.. do something with DFV $results object now my $t = $self->load_tmpl('success.html'); return $t->output; } sub _form_profile { return { required => 'email', msgs => { any_errors => 'some_errors', prefix => 'err_', }, }; } #### use CGI::FormBuilder; # Assume we did a DBI query to get existing values my $dbval = $sth->fetchrow_hashref; # First create our form my $form = CGI::FormBuilder->new( name => 'acctinfo', method => 'post', stylesheet => '/path/to/style.css', values => $dbval, # defaults ); # Now create form fields, in order # FormBuilder will automatically determine the type for you $form->field(name => 'fname', label => 'First Name'); $form->field(name => 'lname', label => 'Last Name'); # Setup gender field to have options $form->field(name => 'gender', options => [qw(Male Female)] ); # Include validation for the email field $form->field(name => 'email', size => 60, validate => 'EMAIL', required => 1); # And the (optional) phone field $form->field(name => 'phone', size => 10, validate => '/^1?-?\d{3}-?\d{3}-?\d{4}$/', comment => 'optional'); # Check to see if we're submitted and valid if ($form->submitted && $form->validate) { # Get form fields as hashref my $field = $form->fields; # Do something to update your data (you would write this) do_data_update($field->{lname}, $field->{fname}, $field->{email}, $field->{phone}, $field->{gender}); # Show confirmation screen print $form->confirm(header => 1); } else { # Print out the form print $form->render(header => 1); }