Is it possible to avoid repetitive validation code?
42 views
Skip to first unread message
Michael Fung
unread,
Feb 26, 2018, 11:08:23 PM2/26/18
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Mojolicious
Hi All,
I am trying out Mojolicious::Validator. It is very elegant. But, if many actions take a optional "page" parameter, then I need to include something like the following for all these actions.
sub action1 { ...
$validation->optional('page')->num(1,100);
Is there a way to avoid this repetition while using Mojolicious::Validator?
Thanks,
Michael
Jan Henning Thorsen
unread,
Feb 27, 2018, 1:48:36 PM2/27/18
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Mojolicious
Yes. Put it in a helper:
$app->helper("validate.page" => sub { shift->validaton->optional('page')->num(1, 100) })
and use it as:
$c->validate->page;
Or you could also route condition or a "under route" where you add the validation before your action is called: