Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created June 6, 2012 20:28
Show Gist options
  • Select an option

  • Save jshirley/2884542 to your computer and use it in GitHub Desktop.

Select an option

Save jshirley/2884542 to your computer and use it in GitHub Desktop.
[% MACRO form_block BLOCK %]
[% context.scope = 'guide' %]
[% text_field({ label => 'Your Name', name => 'name',
value => 'This is focused', placeholder => 'Placeholder Text',
required => 1 }) %]
[% text_field({ label => 'Prepended', name => 'prepended_name', prepend => '←',
value => 'This is focused', placeholder => 'Placeholder Text',
required => 1,
#support => 'Support text', warning => 'And a warning'
}) %]
[% text_field({ label => 'Appended', name => 'appended_name',
append => '→',
value => 'Trailing', placeholder => 'Placeholder Text',
required => 1,
support => 'Support text', warning => 'And a warning'
}) %]
[% text_field({ label => 'Going both ways', name => 'app_pre_name',
classes => [ 'span2' ],
prepend => '←', append => '→',
value => 'Neat', placeholder => 'Placeholder Text',
required => 1,
support => 'Support text', error => 'Pick a direction'
}) %]
[% select_field({ label => 'A Select', name => 'select',
value => 1,
array => [ [ 0, 'Zero' ], [ 1, 'One' ], [ 2, 'Two' ] ],
value_method => '0', label_method => '1',
required => 1 }) %]
[% readonly_field({ label => 'Readonly', name => 'readonly_name',
value => 'This is read-only' }) %]
[% password_field({ label => 'A Password', name => 'password' }) %]
[% textarea_field({ label => 'Block of Text', name => 'textarea',
value => 'This is a textbox. You should have noticed.',
success => 'With a message',
support => 'field_support' }) %]
[% checkboxes({ label => 'And checkboxes', name => 'checkboxes',
array => [ [ 0, 'Zero' ], [ 1, 'One' ], [ 2, 'Two' ] ],
value_method => '0', label_method => '1',
error => 'Oh no. Failed.'
}) %]
[% text_field({ label => 'Disabled', name => 'disabled', value => 'This is disabled', disabled => 1 }) %]
[% text_field({ label => 'Warning', name => 'has_warning',
value => 'This has a warning', warning => 'Something may be wrong', }) %]
[% text_field({ label => 'Error', name => 'has_error',
value => 'This has an error', error => 'Something did go wrong', }) %]
[% text_field({ label => 'Success', name => 'has_success',
value => 'This is successful', success => 'Celebrate good times', }) %]
[% form_actions([
{ label => 'Save changes' }, # Defaults to submit
{ label => 'Cancel', type => 'cancel' }
]) %]
[% END %]
<form method="post" class="form-vertical">
<fieldset>
<legend>Vertical Form</legend>
[% form_block %]
</fieldset>
</form>
<form class="form-horizontal" method="post">
<fieldset>
<legend>Horizontal Form</legend>
[% form_block %]
</fieldset>
</form>

Make sure you:

  1. Have configured Catalyst::Plugin::MessageStack to point to your Data::Manager model.

  2. Create a guide action, mine is very basic:

     'guide' => Data::Verifier->new(
         profile => {
             'name' => {
                 type     => Email,
                 required => 1,
             },
         }
     ),
    
  3. Are using Post-Redirect-Get.

package MyApp::Controller::Root;
# ... Normal Controller bits, just add this action:
sub guide : Local {
my ( $self, $c ) = @_;
unless ( $c->debug ) {
$c->detach('/error/not_found');
}
my $template = 'index.tt';
if ( @{ $c->req->args } ) {
$template = $c->req->args->[0];
if ( $template eq 'die' ) {
die "This is an unhandled error to test error catching.";
}
if ( $template =~ /^\d{3}/ ) {
$c->detach('/error/_error', [ $template ]);
}
$template .= '.tt';
}
unless ( -f $c->path_to('root', 'guide', $template) ) {
$template = 'index.tt';
}
if ( $c->req->method eq 'POST' ) {
my $result = $c->model('DataManager')->verify('guide', $c->req->params);
if ( $result->success ) {
$c->message(q{Victory!});
} else {
$c->message({ type => 'error', message => $c->loc('FORM VALIDATION ERROR') });
}
$c->res->redirect( $c->req->uri );
$c->detach;
}
$c->stash->{template} = 'guide/' . $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment