-
-
Save anonymous/6a187567036efd222b76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm | |
index 44c8bc2..d85a482 100644 | |
--- a/lib/Mojolicious/Controller.pm | |
+++ b/lib/Mojolicious/Controller.pm | |
@@ -341,6 +341,7 @@ sub validation { | |
my $header = $req->headers->header('X-CSRF-Token'); | |
my $hash = $req->params->to_hash; | |
$hash->{csrf_token} //= $header if $token && $header; | |
+ $hash->{$_->name} = $_ for @{$req->uploads}; | |
my $validation = $self->app->validator->validation->input($hash); | |
return $stash->{'mojo.validation'} = $validation->csrf_token($token); | |
} | |
diff --git a/lib/Mojolicious/Plugin/TagHelpers.pm b/lib/Mojolicious/Plugin/TagHelpers.pm | |
index 0e4b435..c2f449d 100644 | |
--- a/lib/Mojolicious/Plugin/TagHelpers.pm | |
+++ b/lib/Mojolicious/Plugin/TagHelpers.pm | |
@@ -16,17 +16,16 @@ sub register { | |
my @helpers = ( | |
qw(csrf_field form_for hidden_field javascript label_for link_to), | |
- qw(password_field select_field stylesheet submit_button tag_with_error), | |
- qw(text_area) | |
+ qw(select_field stylesheet submit_button tag_with_error text_area) | |
); | |
$app->helper($_ => __PACKAGE__->can("_$_")) for @helpers; | |
$app->helper(check_box => | |
sub { _input(shift, shift, value => shift, @_, type => 'checkbox') }); | |
- $app->helper(file_field => | |
- sub { shift; _tag('input', name => shift, @_, type => 'file') }); | |
+ $app->helper(file_field => sub { _empty_field('file', @_) }); | |
$app->helper(image => sub { _tag('img', src => shift->url_for(shift), @_) }); | |
$app->helper(input_tag => sub { _input(@_) }); | |
+ $app->helper(password_field => sub { _empty_field('password', @_) }); | |
$app->helper(radio_button => | |
sub { _input(shift, shift, value => shift, @_, type => 'radio') }); | |
@@ -39,6 +38,11 @@ sub _csrf_field { | |
return _hidden_field($c, csrf_token => $c->helpers->csrf_token, @_); | |
} | |
+sub _empty_field { | |
+ my ($type, $c, $name) = (shift, shift, shift); | |
+ return _validation($c, $name, 'input', name => $name, @_, type => $type); | |
+} | |
+ | |
sub _form_for { | |
my ($c, @url) = (shift, shift); | |
push @url, shift if ref $_[0] eq 'HASH'; | |
@@ -119,12 +123,6 @@ sub _option { | |
return _tag('option', %attrs, @$pair[2 .. $#$pair], $pair->[0]); | |
} | |
-sub _password_field { | |
- my ($c, $name) = (shift, shift); | |
- return _validation($c, $name, 'input', name => $name, @_, | |
- type => 'password'); | |
-} | |
- | |
sub _select_field { | |
my ($c, $name, $options, %attrs) = (shift, shift, shift, @_); | |
diff --git a/lib/Mojolicious/Validator.pm b/lib/Mojolicious/Validator.pm | |
index 124eae7..62e7a0d 100644 | |
--- a/lib/Mojolicious/Validator.pm | |
+++ b/lib/Mojolicious/Validator.pm | |
@@ -4,7 +4,13 @@ use Mojo::Base -base; | |
use Mojolicious::Validator::Validation; | |
has checks => sub { | |
- {equal_to => \&_equal_to, in => \&_in, like => \&_like, size => \&_size}; | |
+ { | |
+ equal_to => \&_equal_to, | |
+ file => sub { ref $_[2] && $_[2]->isa('Mojo::Upload') }, | |
+ in => \&_in, | |
+ like => sub { $_[2] !~ $_[3] }, | |
+ size => \&_size | |
+ }; | |
}; | |
sub add_check { $_[0]->checks->{$_[1]} = $_[2] and return $_[0] } | |
@@ -25,8 +31,6 @@ sub _in { | |
return 1; | |
} | |
-sub _like { $_[2] !~ $_[3] } | |
- | |
sub _size { | |
my ($validation, $name, $value, $min, $max) = @_; | |
my $len = length $value; | |
@@ -65,6 +69,12 @@ These validation checks are available by default. | |
Value needs to be equal to the value of another field. | |
+=head2 file | |
+ | |
+ $validation->file; | |
+ | |
+Value needs to be a L<Mojo::Upload> object. | |
+ | |
=head2 in | |
$validation->in(qw(foo bar baz)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment