Skip to content

Commit

Permalink
added tag_with_error helper to Mojolicious::Plugin::TagHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 28, 2013
1 parent fb65a8a commit 9f32bd3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.51 2013-10-28
- Added tag_with_error helper to Mojolicious::Plugin::TagHelpers.
- Improved .ep template performance significantly, the number of helpers no
longer has any effect. (jberger, sri)
- Improved form_for performance.
Expand Down
30 changes: 22 additions & 8 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -37,7 +37,8 @@ sub register {
# "t" is just a shortcut for the "tag" helper
$app->helper($_ => sub { shift; _tag(@_) }) for qw(t tag);

$app->helper(text_area => \&_text_area);
$app->helper(tag_with_error => \&_tag_with_error);
$app->helper(text_area => \&_text_area);
}

sub _form_for {
Expand Down Expand Up @@ -210,6 +211,13 @@ sub _tag {
return Mojo::ByteStream->new($tag);
}

sub _tag_with_error {
my ($self, $tag) = (shift, shift);
my ($content, %attrs) = (@_ % 2 ? pop : undef, @_);
$attrs{class} .= $attrs{class} ? ' field-with-error' : 'field-with-error';
return _tag($tag, %attrs, defined $content ? $content : ());
}

sub _text_area {
my ($self, $name) = (shift, shift);

Expand All @@ -223,11 +231,9 @@ sub _text_area {
}

sub _validation {
my ($self, $name, $tag) = (shift, shift, shift);
my ($content, %attrs) = (@_ % 2 ? pop : undef, @_);
$attrs{class} .= $attrs{class} ? ' field-with-error' : 'field-with-error'
if $self->validation->has_error($name);
return _tag($tag, %attrs, defined $content ? $content : ());
my ($self, $name) = (shift, shift);
return _tag(@_) unless $self->validation->has_error($name);
return $self->tag_with_error(@_);
}

1;
Expand Down Expand Up @@ -262,8 +268,8 @@ necessary attributes always be generated automatically.
<%= radio_button country => 'uk' %> UK
For fields that failed validation with L<Mojolicious::Controller/"validation">
the C<field-with-error> class will be automatically added to make styling with
CSS easier.
the C<field-with-error> class will be automatically added through the
C<tag_with_error> helper, to make styling with CSS easier.
<input class="field-with-error" name="age" type="text" value="250" />
Expand Down Expand Up @@ -652,6 +658,14 @@ Very useful for reuse in more specific tag helpers.
Results are automatically wrapped in L<Mojo::ByteStream> objects to prevent
accidental double escaping.
=head2 tag_with_error
%= tag_with_error 'input', class => 'foo'
Same as C<tag>, but adds the class C<field-with-error>.
<input class="foo field-with-error" />
=head2 tel_field
%= tel_field 'work'
Expand Down
18 changes: 18 additions & 0 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -15,6 +15,8 @@ patch 'more_tags';

get 'small_tags';

get 'tags_with_error';

any [qw(GET POST)] => 'links';

get 'script';
Expand Down Expand Up @@ -60,6 +62,15 @@ $t->get_ok('/small_tags')->status_is(200)->content_is(<<EOF);
<div>works</div>
EOF

# Tags with error
$t->get_ok('/tags_with_error')->status_is(200)->content_is(<<EOF);
<bar class="field-with-error">0</bar>
<bar class="test field-with-error">0</bar>
<bar class="test field-with-error">
0
</bar>
EOF

# Links
$t->get_ok('/links')->status_is(200)->content_is(<<'EOF');
<a href="/path">Pa&lt;th</a>
Expand Down Expand Up @@ -442,6 +453,13 @@ __DATA__
%= end
%=t div => 'works'
@@ tags_with_error.html.ep
%= tag_with_error bar => 0
%= tag_with_error 'bar', class => 'test', 0
%= tag_with_error 'bar', (class => 'test') => begin
0
%= end
@@ links.html.ep
<%= link_to 'Pa<th' => '/path' %>
<%= link_to 'http://example.com/', title => 'Foo', sub { 'Foo' } %>
Expand Down

0 comments on commit 9f32bd3

Please sign in to comment.