[html-formfu] Taking default value when not supplied in query (plaintext)

Wicked bits wickedbits at gmail.com
Sat Oct 9 18:39:27 GMT 2010


Thanks Carl,

That works.  When the plugin is active on a field, its default
propagates into $form->params

It seems to me that I'm not the only one who would want to have a
ready way to read the computed state of a form's fields' values as a
hash when working with forms that have defaults.  Basically a union of
supplied and default values in their post-processed state.

retain_default does supply this to a certain extent, but only in the
representation exposed by get_field(…)->value, creating a disjoint
between the state of the form's values as exposed in params and
get_field.  If reading from params is the right place to obtain the
computed field values (is it?), should retain_default propagate the
default into params without a plugin?

Alternatively, and with params being "a hash-ref of all valid input
for which there were no errors", is there cause for an interface that
exposes a hash of all the current state of the computed field values
as currently expressed by get_fields, leaving params to express
processed valid inputs, and not the final computed state of field
values?

Merging defaults into params has the side effect of losing the ability
to tell, post-process, from params what fields were supplied as
inputs.  query->params has pre-processed values that may be examined,
but this is not the same thing as post-processed valid field values.
This might just me taking the input in "… all valid *input* for …" too
literally, but I share the thought anyhow.


For the benefit of anyone else wanting to try it, here's Carl's the
plugin with strict added and $form defined:

	package HTML::FormFu::Plugin::PropagateDefault;
	
	use strict;
	use base 'HTML::FormFu::Plugin';
	
	sub post_process {
		my ($self) = @_;
		
		my $field = $self->parent;
		
		return if !$field->retain_default;
		
		my $form = $self->form;
		my $nested_name = $field->nested_name;
		
		return if $form->valid( $nested_name );
		
		$form->add_valid(
			$nested_name,
			$field->default
		);
		
		return;
	}
	
	1;

and the config has:

	default_args:
	  elements:
	    Select:
	      plugins: 'PropagateDefault'


On Sat, Oct 9, 2010 at 9:46 AM, Carl Franks <fireartist at gmail.com> wrote:
> On 9 October 2010 17:16, Wicked bits <wickedbits at gmail.com> wrote:
>> When FormFu processes a query that contains only some of the defined
>> fields, how can the fields that don't appear in the query have their
>> default value(s) in $form->params?
>
>
> I think this should work...
> You'll need a custom plugin that would be attached to the Select elements:
>
>    package MyCustomPlugin;
>    use base 'HTML::FormFu::Plugin';
>
>    sub post_process {
>        my ($self) = @_;
>
>        my $field = $self->parent;
>
>        return if !$field->retain_default;
>
>        my $nested_name = $field->nested_name;
>
>        return if $form->valid( $nested_name );
>
>        $self->form->add_valid(
>            $nested_name,
>            $field->default,
>        );
>
>        return;
>    }
>
>    1;
>
> Then to automatically attach this plugin to all Select elements, you could do:
>
> ---
> default_args:
>    elements:
>        Select:
>            plugins: '+MyCustomPlugin'
>
> And if you're using Catalyst::Controller::HTML::FormFu, you can use this:
> http://search.cpan.org/~cfranks/HTML-FormFu-0.08002/lib/HTML/FormFu/Manual/Cookbook.pod#Application-wide_default_values



More information about the HTML-FormFu mailing list