1

I am developing a Perl/Catalyst web app using HTML::FormHandler for forms. I am trying to do a conditional validation on a field if a checkbox is checked.

The code below is from a Role I created for my form to use.

has_field 'autosys_jobs.schedule.has_run_day_of_month' => ( type=>'Boolean');
has_field 'autosys_jobs.schedule.run_day_of_month' => ( type => 'PosInteger');

#Validation function for 'autosys_jobs.schedule.run_day_of_month'
sub validate_autosys_jobs_schedule_run_day_of_month {
    my ( $self, $field ) = @_;
    if( $self->field('autosys_jobs.schedule.has_run_day_of_month')->value ) {
       #validate 'autosys_jobs.schedule.run_day_of_month'
    }
}

The problem I'm having is that $self->field('autosys_jobs.schedule.has_run_day_of_month')->value for the boolean field always returns 0 even if it's checked.

Any ideas on what I'm doing wrong?

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    is the validate function being called? Because field ( type => 'boolean' ) does exactly what you want for me, I am currently using that without problems - i.e. for me, checkbox checked => field->value is true. I am asking because HTML::Formhandler will automatically use field_$fieldname subs for validation, but it is called 'validate_autosys_jobs_schedule_run_day_of_month', whereas your field is 'autosys_jobs.schedule.has_run_day_of_month' ( note the dots ) – bytepusher Oct 05 '15 at 23:48
  • Yes the validate function is being called. I confirmed this also the documentation directs to rename any dots with an underscore. – Tony Okusanya Oct 07 '15 at 15:28
  • did not know that. Seemed like the only thing that could have been wrong, but I am glad to see it wasn't and you have figured it out yourself. Perhaps you should mention all relevant info next time ;) – bytepusher Oct 07 '15 at 17:22

1 Answers1

0

After digging deeper into the HTML::FormHandler documentation I was able to figure out my mistake. The field autosys_jobs.schedule is a compound field(forgot to mention that)

has_field 'autosys_jobs.schedule' => ( type => 'Compound');

in order to access the field of interest I had to use the following in my validate sub:

$self->field('autosys_jobs.0.schedule.has_run_day_of_month')->value

Thanks all