Hi, Horsey!
I don't sure that is the best practic, but I do it like this:

In controller I have:

sub some_route {
  my $s = shift;
  my $userid = $s->session('userid'); # for example
  my $valid = $s->_validate_it('order_form');
  if ($valid->has_error) {
# render json or somethink that you want, 
     $s->render(json => {message => "No data: " . @{$valid->failed}[0], 
type => 'alert-info'});# Error!
   } else {
# 
   my $vars = $valid->output;
   $s->render(); # OK!
   }

}

I have method _validate_it() , that is a helper and it look like this:

 $app->helper( _validate_it => sub {
                  my ($self, $form) = @_;
                  my $v = $self->validation;
                  my $out = $VALIDATORS{$form}($v);
                  return $out;
                }
               );  

Hash %VALIDATORS look like this:

my %VALIDATORS = (
                  order_form=> \&_validation_order_form,
                  other_form=> \&_validation_other_form,

);

In this hash I have a link to validation subs (per one sub on form)

where _validation_order_form is a sub like this:

sub _validation_order_form {
  my $v = shift;
  $v->optional('order_id')->like(qr/^[0-9]+/);
  $v->optional('userid')->like(qr/^[0-9]+/);# 

  $v->required('delivery')->in('EXW','CIF','FOB');
  $v->required('temp_id')->like(qr/^[0-9]+/);
  $v->optional('comment');  
  say for @{$v->failed};
  return $v;
}

Helper _validate_it is useful for validating many forms by name, and 
linking validation sub with nedded form

понедельник, 23 мая 2016 г., 14:33:48 UTC+3 пользователь Bad Horsey написал:
>
> Hi,
>
> I've just started using Mojolicious and I'm new to quite a few of the 
> concepts.  I'm building a form based app that has templates that extend 
> from a basic from, for example:
>
> *basic form ep template*
>
> name
> email
> tel
>
> *second form extends basic*
>
> accommodation choice
> event choice
>
> *third form extends basic*
>
> quantity
>
> Can I create a form validation object to check required fields that I can 
> extend based on the form submitted? Sorry if that's a stupid question but 
> like I said I’m new to a lot of the concepts and trying to figure out best 
> practices.  I’ve looked at validator docs but I’m a little unsure how to 
> proceed. So if I have:
>
> my $validatorBasic  = Mojolicious::Validator->new;
> my $validation = $validatorBasic->validation;
> …
> $validation->required(‘name’)->size(50);
> $validation->required(‘email’)->size(50);
> $validation->required(‘tel’)->size(11);
>
> How could I extend this for second form, third form?  Also is there a 
> helper to check for types like number, email or should I just use 
> like(regex)?  
>
> Really liking Mojolicious btw.  Thanks in advance.
>
> Horsey
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to