Search code examples
perlformscatalyst

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;

Solution

  • 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.