[html-formfu] Example of a simple repeatable field and button

Florent Angly florent.angly at gmail.com
Wed Mar 18 18:37:48 GMT 2009


Thanks for your reply Carl!

Do I need to involve Ajax in that problem? I would like to keep it as 
simple and light as possible and have no experience in Ajax.

Attached is what I tried in the config file to get my field to repeat on 
clicking on a button:
> <elements>
>     type  Hidden
>     name  nof_samples
>     constraints Required
>     value 1
> </elements>
>
> <elements>
>     type  Repeatable
>     increment_field_names 1
>     counter_name nof_samples
>     name samples
>     <elements>
>         name  sample_files
>         label Sample file *
>         type  File
>         constraints Required
>         constraints File
>         <attributes>
>             title   Input a file here...
>         </attributes>
>     </elements>
> </elements>
>
> <elements>
>     value  Add a sample
>     name   add_sample
>     type   Button
>     <attributes>
>     
>         #onclick "document.forms[0].nof_samples.value++;"
>         # does properly increment nof_samples, but field is not repeated
>         
>         onclick "document.forms[0].nof_samples.value++; 
> window.location.reload();"
>         # does properly increment nof_samples, but field is not repeated
>         
>     </attributes>
> </elements>
>
> <elements>
>     value   Submit
>     name   submit
>     type   Submit
> </elements>

As you can see, on cliking on the button, I increment a hidden variable. 
My field looks at that variable to know how many times it should be 
repeated.
In practice, clicking on the button correctly increment the variable, 
but the field does not get repeated several times.
What am i missing here?

Thanks,

Florent




Carl Franks wrote:
> 2009/3/14 Florent Angly <florent.angly at gmail.com>:
>   
>> Hi FormFu list,
>>
>> I have created a small Catalyst webapp that uses a form that is based on a
>> FormFu YML configuration file. I would like to have some repeatable fields,
>> along with a button to click in order generate more of these fields on the
>> fly.
>>
>> I have read the Catalyst tutorial, the FormFu documentation, and I have some
>> basic repeatable fields, but I seem to have problem with the logic of how to
>> generate more repetitions of these fields from the webinterface.
>>
>> Can I even do that using only my FormFu config file, or would I need some
>> extra logic in my Catalyst code? I asked the Catalyst list without
>> success... Maybe the asking the FormFu experts on this list is a better
>> place to ask.
>>
>> If someone could post a simple example of a simple FormFu config file with a
>> simple repeatable field and button to add more of this field, it would be
>> great!
>>     
>
> Hi,
>
> You will need code to piece it together, but you shouldn't need an
> extra config file.
> You need to decide whether you want this to work with AJAX, or not.
> If AJAX, I'd probably suggest having a separate controller handle to handle it.
> Use JS to keep the 'counter_name' [1] hidden field updated, and pass
> that value in the AJAX call.
> Then in your controller, do something like:
>
>     $form->load_config_file('$file');
>     $count = $c->request->param->{count};
>     my $rep = $form->get_all_elements({ type => 'Repeatable' });
>     my $new = $rep->repeat($count);
>     my $html = join '', @$new;
>
> [1] http://search.cpan.org/~cfranks/HTML-FormFu-0.03007/lib/HTML/FormFu/Element/Repeatable.pm#counter_name
>
> _______________________________________________
> HTML-FormFu mailing list
> HTML-FormFu at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>
>   




More information about the HTML-FormFu mailing list