http://www.perlmonks.org?node_id=1086648

GertMT has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,

I found node Re: Form validation - server-side (Dancer) and client-side (javascript) and I'm trying to use the mentioned module CGI::Ex::Validate. I'm trying to get it to work and the code below results in a HTML-Form included with Javascript for client-side. Happy with that, except that I can't seem to find out how to add code that gives me a simple set of radio-buttons. Is there anyone who has experience with that and cares to share information on that?

Thanks, Gert
#!/usr/bin/perl # use CGI::Ex::Validate; my $val_hash = { 'group order' => [qw(date fname lname email email2 telephone choice comments)], date => { name => 'Date(s) of Workshop *', # field is not optional in + this case required => 1, match => 'm|^\d\d\d\d/\d\d/\d\d$|', match_error => 'Please enter date in YYYY/MM/DD format', custom_js => "function (args) { var t = new Date(); var y = t.getYear()+1900; var m = t.getMonth() + 1; var d = t.getDate();) if (m < 10) m = '0'+m; if (d < 10) d = '0'+d; (args.value > ''+y+'/'+m+'/'+d) ? 1 : 0; }", custom_js_error => 'The date was not greater than today.', }, fname => { name => 'First name *', required => 1, min_len => 2, max_len => 50, }, lname => { name => 'Last name *', required => 1, min_len => 2, max_len => 75, }, email => { name => 'Email *', required => 1, max_len => 100, type => 'email', type_error => '$name must be a valid email address.', }, email2 => { validate_if => 'email was_valid', vif_disable => 1, name => 'Verify email *', required => 1, equals => 'email', }, telephone => { name => 'Telephone *', required => 1, max_values => 1, max_len => 14, }, # # attempt to create radio-buttons. # 'group order'=>[qw(yes no)], 'yes' =>{ max_in_set=> '1 of yes, no', }, 'no'=>{ max_in_set => '2 of yes, no', }, comments => { name => 'Additional Info', max_len => 3000, }, }; # my $vob = CGI::Ex::Validate->new; open( OUT, ">form2.html" ) or die "can't open out $!"; my $js_uri_path="/CGI-Ex-2.38/lib/CGI/Ex/validate.js"; my $form_name = "the_form"; # name of the form to attach javascript + to my $form = $vob->generate_form( $val_hash, { form_name => $form_name, # will use a random name if not + passed js_uri_path => $js_uri_path, } ); print OUT $form;
  • Comment on validation built form with cgi::ex::validate that has radio buttons
  • Download Code

Replies are listed 'Best First'.
Re: validation built form with cgi::ex::validate that has radio buttons
by Anonymous Monk on May 19, 2014 at 11:55 UTC
    See https://metacpan.org/pod/CGI::Ex::Validate#GROUP-OPTIONS "group" isn't radio

    If you view the source to see sub generate_form { you'll see it takes a htype option , so specify htype somehow ... check the dist tree for an example of usage (grep "radio|check|htype" https://metacpan.org/release/CGI-Ex )

    if that fails study the code

    } elsif ($type eq 'radio' || $type eq 'checkbox') { my $e = $field->{'enum'} || []; my $l = $field->{'label'} || $e; my $I = @$e > @$l ? $#$e : $#$l; for (my $i = 0; $i <= $I; $i++) { my $_e = $e->[$i]; $_e =~ s/\"/&quot;/g; $input .= "<div class=\"option\"><input type=\"$type\" + name=\"$field->{'field'}\" id=\"$field->{'field'}_$i\" value=\"$_e\" +>" .(defined($l->[$i]) ? $l->[$i] : '')."</div>\n"; }