HTML::FormHandler access to form field - perl

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.

Related

How can I sort a views field created in hook_views_data_alter?

In a custom drupal 9 module I define a new field for a view within hook_views_data_alter(&$data).
$data['node']['node_views_mydata'] = array(
'title' => t('Node Views Mydata'),
'field' => array(
'title' => t('Node views mydata'),
'help' => t('Shows some data in views'),
'id' => 'node_views_mydata',
'sort' => [
'node_views_mydata' => 'default',
],
)
);
I have also defined a field plugin processing text data for this field, and can insert and output the field in views.
Now I would like to make the field sortable. But I can't do that. I always get the error "unknown column". In fact, node_views_data is not a "real" node field, but only created on the fly via the hook.
Is there nevertheless a way to sort by this column?

HTML::FormHandler sort_column for HTML::FormHandler::Field::Select

The documentation for HTML::FormHandler::Select says that the sort_column option Sets or returns the column or arrayref of columns used in the foreign class for sorting the options labels. I've tried setting it, but it is not setting my options:
has_field 'my_field' => (
type => 'Select',
sort_column => 'label',
required => 1,
);
I've also tried not setting sort_column, since the default is to sort by the label column and that's what I want, but it doesn't seem to work still. Does anyone know how to have HTML::FormHandler sort the values of my select field? Currently the values are being set with an options function:
sub options_my_field {
return [
{
value => 1,
label => 'One',
},
{
value => 2,
label => 'Two',
},
];
}

How do I override compare_fields when using Rose::HTML::Form

So I'm trying use Rose::HTML::Form and I want my fields to appear based on 'rank' rather than by name (the default) .
I've written a comparator subroutine:
sub _order_by_rank {
my ($self, $one, $two) = #_;
return $one->rank <=> $two->rank;
};
and referenced it in my form constructor:
Rose::HTML::Form->new(method => 'post', compare_fields => \&_order_by_rank);
But I am then left with:
Can't call method "name" on unblessed reference at /usr/lib/perl5/site_perl/5.8.8/Rose/HTML/Form/Field/Collection.pm line 405.
It seems to call the comparator before I've added anything.
After constructing the form object, I add some fields and then call init_fields:
$form->add_fields(
id => { type => 'hidden', value => "", rank => 0 },
number => { type => 'int', size => 4, required => 1, label => 'Plant Number', rank => 1 },
name => { type => 'text', size => 25, required => 1, label => 'Plant Name', rank => 2 },
...
);
$form->init_fields;
According to the documentation this is something people usually do. What it doesn't explain is how to do it.
Hopefully someone can explain this to me before I have to buy a new keyboard :)
From the documentation it looks as though, rather than passing in a subroutine reference, you need to subclass Rose::HTML::Form and override the compare_fields method.
The default comparison method is Rose::HTML::Form::compare_fields. You have to create subclasses if you want different sorting methods for different forms.
It would help me to explain further if you showed your full code.

HTML::FormHandler dynamic fields added client side

I'm using HTML::FormHandler and I would like to have a form that has a dynamic number of form elements. Essentially, I have some inputs that are always present, such as first_name, last_name, and email, but then I have an input, pracitce_area, that I can have many of dynamically (so practice_area1, pracitce_area2, etc). So on the client side I will be using jQuery to dynamically add more practice_area inputs, and I would like for my HTML::FormHandler form to be able to process a dynamic number of these inputs and validate them and put them in the database. The practice_area inputs will be stored in a separate table that will be related with a foreign key to the element of this form, so I would like for HTML::FormHandler to know that these are related and pull out a dynamic number when editing, but also be able to save a dynamic number into the database when saving. Is there a way to handle something like this with HTML::FormHandler? Here's the definition of my form:
package test::Form::Base;
use namespace::autoclean;
use HTML::FormHandler::Moose;
with 'HTML::FormHandler::TraitFor::Model::DBIC';
has title => ( is => 'ro', default => 'Client Information Form');
has '+item_class' => ( default => 'ClientInformationForm' );
has_field 'first_name' => (
type => 'Text',
label => 'First Name',
required => 1,
);
has_field 'last_name' => (
type => 'Text',
label => 'Last Name',
required => 1,
);
has_field 'email' => (
type => 'Email',
label => 'Email',
required => 1,
);
#would like to have this be dynamic in number, and have HTML::FormHandler know
#that it's related with a foreign key when pulling them out of the database
has_field 'practice_area' => (
type => 'TextArea',
);
no HTML::FormHandler::Moose;
__PACKAGE__->meta->make_immutable;
1;
Have you looked at HTML::Formhandler::Repeatable.
You should just be able to use practice_area in your form and have multiple entries. These will just be pulled in an an array(ref) in form processing.

Duplicate Validation on Combined Fields in zend form

Hi there I have a table in which combination of three fields is unique. I want to put the check of duplication on this combination. Table looks like
I know how to validate single field, But how to validate the combination is not know. To validate one field I use the following function
public function isValid($data) {
// Options for name field validation
$options = array(
'adapter' => Zend_Db_Table::getDefaultAdapter(),
'table' => 'currencies',
'field' => 'name',
'message'=> ('this currency name already exists in our DB'),
);
// Exclude if a id is given (edit action)
if (isset($data['id'])) {
$options['exclude'] = array('field' => 'id', 'value' => $data['id']);
}
// Validate that name is not already in use
$this->getElement('name')
->addValidator('Db_NoRecordExists', false, $options
);
return parent::isValid($data);
}
Will any body guide me how can I validate duplication on combined fields?
There is no ready to use validator for this, as far as I know. You have either to write your own, or do a check with SQL-query with three conditions (one for each field).
you have to Apply a validation on name element of zend form.
Here is code for add validation on name field.
$name->addValidator(
'Db_NoRecordExists',
true,
array(
'table' => 'currencies',
'field' => 'name',
'messages' => array( "recordFound" => "This Currency Name already exists in our DB") ,
)
);
And you must set required true.