Permalink
Browse files

add info_message to form

  • Loading branch information...
1 parent f7eb42b commit 244fdc883986c9f642deacfd500531ce32eacc70 @gshank committed Oct 11, 2012
View
@@ -922,6 +922,7 @@ has 'http_method' => ( isa => 'Str', is => 'ro', default => 'post' );
has 'enctype' => ( is => 'rw', isa => 'Str' );
has 'error_message' => ( is => 'rw', predicate => 'has_error_message', clearer => 'clear_error_message' );
has 'success_message' => ( is => 'rw', predicate => 'has_success_message', clearer => 'clear_success_message' );
+has 'info_message' => ( is => 'rw', predicate => 'has_info_message', clearer => 'clear_info_message' );
# deprecated
has 'css_class' => ( isa => 'Str', is => 'ro' );
has 'style' => ( isa => 'Str', is => 'rw' );
@@ -1253,6 +1254,7 @@ sub clear {
$self->clear_use_defaults_over_obj;
$self->clear_use_init_obj_over_item;
$self->clear_no_update;
+ $self->clear_info_message;
}
sub values { shift->value }
@@ -225,6 +225,28 @@ Hashref of arbitrary HTML attributes to be included in the form wrapper
=back
+=head2 Form messages
+
+Some messages are rendered at the top of the form (inside the form tag)
+by the 'render_form_messages' method, which is implemented in
+L<HTML::FormHandler::Widget::Form::Simple> and
+L<HTML::FormHandler::Widget::Theme::BootstrapFormMessages> (which is
+included by the Bootstrap theme).
+
+There are three types of form messages: 'error_message', 'success_message',
+and 'info_message'. The 'error_message' and 'success_message' are set inside
+the form:
+
+ has '+success_message' => ( default => 'Form successfully submitted' );
+ has '+error_message' => ( default => 'There were errors in your form.' );
+
+And then are displayed after the form is validated.
+
+The 'info_message' is cleared out when a form is re-processed, and so would
+normally be set on the process call, or between new & process.
+
+ $form->process( params => {}, info_message => 'Fill in the form' );
+
=head2 Field settings
has_field 'foo' => ( widget => 'MyWidget', widget_wrapper => 'SpecialWrapper',
@@ -140,6 +140,12 @@ sub render_form_messages {
my $success_class = $self->get_tag('success_class') || 'success_message';
$output .= qq{\n<span class="$success_class">$msg</span>};
}
+ if( $self->has_info_message && $self->info_message ) {
+ my $msg = $self->info_message;
+ $msg = $self->_localize($msg);
+ my $info_class = $self->get_tag('info_class') || 'info_message';
+ $output .= qq{\n<span class="$info_class">$msg</span>};
+ }
$output .= "\n</div>";
return $output;
}
@@ -20,6 +20,7 @@ Form error messages:
=cut
use Moose::Role;
+with 'HTML::FormHandler::Widget::Theme::BootstrapFormMessages';
after 'before_build' => sub {
my $self = shift;
@@ -52,6 +53,13 @@ sub render_form_messages {
$output .= qq{\n<span>$msg</span>};
$output .= "\n</div>";
}
+ if ( $self->has_info_message && $self->info_message ) {
+ my $msg = $self->info_message;
+ $msg = $self->_localize($msg);
+ $output = qq{\n<div class="alert alert-info">};
+ $output .= qq{\n<span>$msg</span>};
+ $output .= "\n</div>";
+ }
return $output;
}
@@ -0,0 +1,49 @@
+package HTML::FormHandler::Widget::Theme::BootstrapFormMessages;
+# ABSTRACT: role to render form messages using Bootstrap styling
+
+use Moose::Role;
+
+=head1 NAME
+
+HTML::FormHandler::Widget::Theme::BootstrapFormMessages
+
+=head1 DESCRIPTION
+
+Role to render form messages using Bootstrap styling.
+
+=cut
+
+sub render_form_messages {
+ my ( $self, $result ) = @_;
+
+ $result ||= $self->result;
+ my $output = '';
+ if ( $result->has_form_errors || $result->has_errors ) {
+ $output = qq{\n<div class="alert alert-error">};
+ my $msg = $self->error_message;
+ $msg ||= 'There were errors in your form';
+ $msg = $self->_localize($msg);
+ $output .= qq{\n<span class="error_message">$msg</span>};
+ $output .= qq{\n<span class="error_message">$_</span>}
+ for $result->all_form_errors;
+ $output .= "\n</div>";
+ }
+ elsif ( $result->validated ) {
+ my $msg = $self->success_message;
+ $msg ||= "Your form was successfully submitted";
+ $msg = $self->_localize($msg);
+ $output = qq{\n<div class="alert alert-success">};
+ $output .= qq{\n<span>$msg</span>};
+ $output .= "\n</div>";
+ }
+ if ( $self->has_info_message && $self->info_message ) {
+ my $msg = $self->info_message;
+ $msg = $self->_localize($msg);
+ $output = qq{\n<div class="alert alert-info">};
+ $output .= qq{\n<span>$msg</span>};
+ $output .= "\n</div>";
+ }
+ return $output;
+}
+
+1;
View
@@ -44,6 +44,7 @@ use HTML::FormHandler::Test;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
with 'MyApp::Form::Basic::Theme';
+ with 'HTML::FormHandler::Widget::Theme::BootstrapFormMessages';
has '+name' => ( default => 'basic_form' );
has_field 'foo' => ( type => 'Text' );
@@ -62,7 +63,6 @@ my $expected = '<div class="row">
</div>
<div class="span9">
<form class="well" method="post" id="basic_form">
- <div class="form_messages"></div>
<label for="foo">Foo</label>
<input type="text" class="span3" placeholder="Type something…" name="foo" id="foo" value="">
<span class="help-inline">Associated help text!</span>
@@ -75,9 +75,6 @@ my $expected = '<div class="row">
is_html($rendered, $expected, 'rendered correctly');
-done_testing;
-exit;
-
# check foo
$rendered = $form->field('foo')->render;
$expected =
@@ -90,7 +87,13 @@ is_html($rendered, $expected, 'foo field is correct' );
$rendered = $form->field('bar')->render;
$expected =
'<label class="checkbox" for="bar">
- <input type="checkbox" name="bar" id="bar" value="1">Check me out</label>';
+ <input type="checkbox" name="bar" id="bar" value="1"> Check me out </label>';
is_html($rendered, $expected, 'bar field is correct' );
+$form->process( params => {}, info_message => 'Fill in the form!' );
+$rendered = $form->render_form_messages;
+$expected =
+'<div class="alert alert-info"><span>Fill in the form!</span>';
+is_html($rendered, $expected, 'info message rendered ok' );
+
done_testing;
View
@@ -44,4 +44,11 @@ $form->process( params => {} );
$rendered = $form->render;
unlike( $rendered, qr/class="success_message"/, 'no success message when not validated' );
+$form->process( params => {}, info_message => 'Please fill out the form completely' );
+$rendered = $form->render;
+like( $rendered, qr/class="info_message"/, 'info message is there' );
+$form->process( params => {} );
+$rendered = $form->render;
+unlike( $rendered, qr/class="info_message"/, 'info message has been cleared' );
+
done_testing;

0 comments on commit 244fdc8

Please sign in to comment.