HTML::FormHandler field validation based on other field's check'd state - perl

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?

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

Related

display radio buttons using FormHelper

I am using CakePHP3
I am unable to decipher the documentation on using radio buttons to create for a list of results.
There is create radio button.
Which I tried this way:
foreach ($userGroups as $group_id => $group) {
echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
}
There is using select pickers.
Which says all you need to do is set the type to radio. But I checked the api and it looks like it will unset the type.
Anyway, I tried
echo $this->Form->select("user_circle_id", $userGroups, ['type' => 'radio']);
Nothing works.
Please advise.
UPDATE:
$userGroups = [1 => 'Group 1', 2 => 'Group 2']; // basically primary key is keys and the display fields are the values.
Use following syntax :
$selectedStatus = 1;
$attributes = array('legend'=>false,'value'=>$selectedStatus,'class'=>'sortByStatusCourse','name'=>'sortByStatusCourse');
$options = array("1"=>"Active","0"=>"Inactive");
echo $this->Form->radio('is_active', $options,$attributes);
Code explanation :
We are passing parameters to cakePHP core so that it will output Radio button in HTML.
echo $this->Form->radio :
Here 1st param is Name on field in Database. 2nd param is What are the options for radio buttons , its value and text .3rd param is what are its attributes like what should be its default value here I explicitely set it to 1 so it will already tick radio with Activetext.
According to your code : echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
I think order of param is wrong as you pass VALUE in 2nd param which should be 3rd param.Check my code.

HTML::FormHandler access to form field

Is there any way to access the value of form field 'wklloc_id' in the form field options method of field 'prg_id'?
My Form contains (amongst others) these fields:
has_field 'wklloc_id' => ( type => 'Select', label => 'Winkel(locatie)' );
has_field 'prg_id' => ( type => 'Select', empty_select => 'Kies eerst een Winkel(locatie)', label => 'Productgroep' );
At this point my options method for field 'prg_id' contains:
sub options_prg_id
{
my ($self) = shift;
my (#prg_select_list,$productgroep,$productgroepen);
return unless ($self->schema);
$productgroepen = $self->schema->resultset( 'WinkelLocatieProductgroepen' )->search( {}, { bind => [ 2 ] } );
Is is possible to set the value of the bind variable (i.e. 2) to the value of field 'wklloc_id' and how does one do that? Mind you this is needed before any submit.
The value of a select field is set the same way as other fields, i.e. it comes from an init_object, from parameter values, or from a default. For your case, if you want this field to start with the 'value' 2, then just put: default => 2 in your field definition.

Using HTML::FormFu, how do you change a field value *after* processing so that it appears modified in Template Toolkit?

For example, if I process a form:
my $form_input = { input_data => '123' };
$form->process($form_input);
Then I want to alter the value of 'input_data':
my $clearme = $form->get_field('input_data');
$clearme->value("546"); # doesn't seem to work
..Before pushing the form object to TT:
template 'index' => { form => $form }; # using Dancer
'input_data' seems to retain it's original value (123). Any hints on what I'm doing wrong, or what I should be doing?
Thanks
After looking at the documentation and doing some testing, I think you want
$form->add_valid(input_data => '546');

HTML::FormHandler render field without label

With a HTML::FormHandler formular I want to render only the field part of an form field.
<div>
my label [% form.field('name').render %]
</div>
This renders the field and a label.
I see in the code that the renderer checks for:
$self->has_flag('no_render_label')
so you need to set this flag.
How it is done? I can only make a guess.
you did not specify which widget you are using, and if I understand it right, you should extend him and create your own widget.
package HTML::FormHandler::Field::Checkbox::NoLabel;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Checkbox';
our $VERSION = '0.01';
has '+no_render_label' => ( default => 1 );
For future searchers:
You can set the do_label attribute when you define your field in the form class.
has_field 'name' => (
type => 'Text'
do_label => 0,
);
Source: HTML::FormHandler field attribute reference
I've found an another way in the manual of HFH.
package Test::Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has '+name' => ( default => 'testform' );
has '+widget_wrapper' => ( default => 'None' );
has '+auto_fieldset' => ( default => 0 );
has_field 'foo';

Populate date in MultiOptions element of Zend Form

Hello I have an array like this :
Array (
[id] => 1
[code] => Dep98
[description] => Hello World
[facility] => Array (
[0] => FacName1
[1] => FacName2
)
)
But when I populate this array to Zend_Form it only show data in textboxes elements having same id as defined in array index not in multiselect dropdown element. for example:
'code' id is also define in form's first textbox element,
'description' id is also define in form's second textbox element,
'facility' id is also define in form's third MultiOptions element
But in MultiOptions it does not show any record.
I agree with Travis, you should pass an array with following values to populate:
$vals = array('code'=>5,
'description' => 'testing',
'facility' => array(1=>'FacName2'));
$form->populate($vals);
But note this - options must be filled in the facility form element before attempting to populate or validate, dont expect facility value to be set if there is an empty list of options in the facility element.
What exactly do you want in the drop down box?
The array you pass to multiOptions must be in the form of value => title.
You may want to loop through your results and generate an options array.
For example
$options = array();
foreach ( $data as $value ) {
$options[$value['id']] = $value['description'];
}
$select = Zend_From_Element_Select("select_field");
$select->multiOptions($options);
Try this:
Array (
[id] => 1
[code] => Dep98
[description] => Hello World
[facility] => Array (
FacName1 => [0]
FacName2 => [1]
)
)