Rose-HTML-Objects
view release on metacpan or search on metacpan
lib/Rose/HTML/Form.pm view on Meta::CPAN
=item B<init_fields [PARAMS]>
Initialize the fields based on L<params()|/params>. In general, this works as you'd expect, but the details are a bit complicated.
The intention of L<init_fields()|/init_fields> is to set field values based solely and entirely on L<params()|/params>. That means that default values for fields should not be considered unless they are explicitly part of L<params()|/params>.
In general, default values for fields exist for the purpose of displaying the HTML form with certain items pre-selected or filled in. In a typical usage scenario, those default values will end up in the web browser form submission and, eventually, a...
But to preserve the intended functionality of L<init_fields()|/init_fields>, the first thing this method does is L<clear()|/clear> the form. (This is the default. See the C<no_clear> parameter below.)
If a parameter name exactly matches a field's name (note: the field's L<name|Rose::HTML::Form::Field/name>, which is not necessarily the the same as the name that the field is stored under in the form), then the (list context) value of that parameter...
If a field "isa" L<Rose::HTML::Form::Field::Compound>, and if no parameter exactly matches the L<name|Rose::HTML::Form::Field/name> of the compound field, then each subfield may be initialized by a parameter name that matches the subfield's L<name|Ro...
If a field is an "on/off" type of field (e.g., a radio button or checkbox), then the field is turned "on" only if the value of the parameter that matches the field's L<name|Rose::HTML::Form::Field/name> exactly matches (string comparison) the "value"...
PARAMS are name/value pairs. Valid parameters are:
=over 4
=item B<no_clear BOOL>
If true, the form is not L<clear()ed|/clear> before it is initialized.
=item B<recursive BOOL>
If true, this method is called recursively on any L<nested sub-forms|/"NESTED FORMS">. If false, the fields in all nested sub-forms are still initialized as expected, but this is done by iterating over the "flattened" L<fields|/fields> list rather t...
If this parameter is not passed, its value defaults to the value of the L<recursive_init_fields|/recursive_init_fields> object attribute.
=back
Examples:
package RegistrationForm;
...
sub build_form
{
my($self) = shift;
$self->add_fields
(
name => { type => 'text', size => 25 },
gender =>
{
type => 'radio group',
choices => { 'm' => 'Male', 'f' => 'Female' },
default => 'm'
},
hobbies =>
{
type => 'checkbox group',
name => 'hobbies',
choices => [ 'Chess', 'Checkers', 'Knitting' ],
default => 'Chess'
},
bday = => { type => 'date split mdy' }
);
}
...
$form = RegistrationForm->new();
$form->params(name => 'John',
gender => 'm',
hobbies => undef,
bday => '1/24/1984');
# John, Male, no hobbies, 1/24/1984
$form->init_fields;
$form->reset;
$form->params(name => 'John',
bday => '1/24/1984');
# No name, Male, Chess, 1/24/1984
$form->init_fields(no_clear => 1);
$form->reset;
# Set using subfield names for "bday" compound field
$form->params('name' => 'John',
'bday.month' => 1,
'bday.day' => 24,
'bday.year' => 1984);
# John, Male, no hobbies, 1/24/1984
$form->init_fields();
$form->reset;
$form->params('bday' => '1/24/1984',
'bday.month' => 12,
'bday.day' => 25,
'bday.year' => 1975);
# No name, no gender, no hobbies, but 1/24/1984 because
# the "bday" param trumps any and all subfield params.
$form->init_fields();
$form->reset;
# Explicitly set hobbies field to Knitting...
$form->field('hobbies')->input_value('Knitting');
# ...but then provide a hobbies param with no value
$form->params('hobbies' => undef);
# Fields are not cleared, but the existence of the hobbies
# param with an empty value causes the hobbies list to be
# empty, instead of the default Chess. Thus:
#
# No name, Male, no hobbies, no birthday
$form->init_fields(no_clear => 1);
=item B<init_fields_with_cgi CGI [, ARGS]>
This method is a shortcut for initializing the form's L<params|/params> with a CGI object and then calling L<init_fields|/init_fields>. The CGI argument is passed to the L<params_from_cgi|/params_from_cgi> method and ARGS are passed to the L<init_fi...
lib/Rose/HTML/Form.pm view on Meta::CPAN
The L<local_name|Rose::HTML::Form::Field/local_name> for all of these fields is C<name>, but L<object_from_form|/object_from_form> will pass the value of the C<name> field to the C<name()> method of the object. See the L<nested forms|/"NESTED FORMS"...
In order to return an object based on a form, L<object_from_form|/object_from_form> needs an object. If passed an OBJECT argument, then that's the object that's used. If passed a CLASS name, then a new object is constructed by calling L<new()|/new...
Use this method as a "helper" when writing your own methods such as C<person_from_form()>, as described in the example in the L<overview|/OVERVIEW>. L<object_from_form()|/object_from_form> should be called in the code for subclasses of L<Rose::HTML::...
The convention for naming such methods is "foo_from_form", where "foo" is a (lowercase, underscore-separated, please) description of the object constructed based on the values in the form's fields.
The field names may not match up exactly with the object method names. In such cases, you can use L<object_from_form()|/object_from_form> to handle all the fields that do match up with method names, and then handle the others manually. Example:
sub person_from_form
{
my($self) = shift;
my $person = $self->object_from_form(class => 'Person');
$person->alt_phone($self->field('phone2')->internal_value);
...
return $person;
}
It is the caller's responsibility to ensure that the object class (C<Person> in the example above) is loaded prior to calling this method.
=item B<param NAME [, VALUE]>
Get or set the value of a named parameter. If just NAME is passed, then the value of the parameter of that name is returned. If VALUE is also passed, then the parameter value is set and then returned.
If a parameter has multiple values, the values are returned as a reference to an array in scalar context, or as a list in list context. Multiple values are set by passing a VALUE that is a reference to an array of scalars.
Failure to pass at least a NAME argument results in a fatal error.
=item B<params [PARAMS]>
Get or set all parameters at once.
PARAMS can be a reference to a hash or a list of name/value pairs. If a parameter has multiple values, those values should be provided in the form of a reference to an array of scalar values. If the list of name/value pairs has an odd number of ite...
If PARAMS is a reference to a hash, then it is accepted as-is. That is, no copying of values is done; the actual hash references is stored. If PARAMS is a list of name/value pairs, then a deep copy is made during assignment.
Regardless of the arguments, this method returns the complete set of parameters in the form of a hash (in list context) or a reference to a hash (in scalar context).
In scalar context, the hash reference returned is a reference to the actual hash used to store parameter names and values in the object. It should be treated as read-only.
The hash returned in list context is a deep copy of the actual hash used to store parameter names and values in the object. It may be treated as read/write.
=item B<params_exist>
Returns true if any parameters exist, false otherwise.
=item B<param_exists NAME>
Returns true if a parameter named NAME exists, false otherwise.
=item B<param_exists_for_field [ NAME | FIELD ]>
Returns true if a L<param|/param> exists that addresses the field named NAME or the L<Rose::HTML::Form::Field>-derived object FIELD, false otherwise.
This method is useful for determining if any query parameters exist that address a compound field. For example, a compound field named C<a.b.c.d> could be addressed by any one of the following query parameters: C<a>, C<a.b>, C<a.b.c>, or C<a.b.c.d>....
$form = Rose::HTML::Form->new;
$form->add_field(when => { type => 'datetime split mdyhms' });
$form->params({ 'when.date' => '2004-01-02' });
$form->param_exists_for_field('when'); # true
$form->param_exists_for_field('when.date'); # true
$form->param_exists_for_field('when.date.month'); # true
$form->param_exists_for_field('when.time.hour'); # false
$subform = Rose::HTML::Form->new;
$subform->add_field(subwhen => { type => 'datetime split mdyhms' });
$form->add_form(subform => $subform);
$form->params({ 'subform.subwhen.date' => '2004-01-02' });
$form->param_exists_for_field('subform.subwhen'); # true
$form->param_exists_for_field('subform.subwhen.date'); # true
$form->param_exists_for_field('subform.subwhen.date.month'); # true
$form->param_exists_for_field('subform.subwhen.time.hour'); # false
$form->param_exists_for_field('when'); # false
$form->param_exists_for_field('when.date'); # false
$form->param_exists_for_field('when.date.month'); # false
$form->param_exists_for_field('when.time.hour'); # false
=item B<params_from_apache APR>
Set L<params|/params> by extracting parameter names and values from an apache request object. Calling this method entirely replaces the previous L<params|/params>.
If running under L<mod_perl> 1.x, the APR argument may be:
=over 4
=item * An L<Apache> object. In this case, the L<Apache::Request> module must also be installed.
=item * An L<Apache::Request> object.
=back
If running under L<mod_perl> 2.x, the APR may be:
=over 4
=item * An L<Apache2::RequestRec> object. In this case, the L<Apache2::Request> module must also be installed.
=item * An L<Apache2::Request> object.
=back
In all cases, APR may be an object that has a C<param()> method that behaves in the following way:
=over 4
=item * When called in list context with no arguments, it returns a list of parameter names.
=item * When called in list context with a single parameter name argument, it returns a list of values for that parameter.
=back
=item B<params_from_cgi CGI>
Set L<params|/params> by extracting parameter names and values from a L<CGI> object. Calling this method entirely replaces the previous L<params|/params>. The CGI argument must be either a L<CGI> object or must have a C<param()> method that behaves...
=over 4
=item * When called in list context with no arguments, it returns a list of parameter names.
=item * When called in list context with a single parameter name argument, it returns a list of values for that parameter.
=back
( run in 0.776 second using v1.01-cache-2.11-cpan-71847e10f99 )