4 CXGN::Form.pm -- classes to deal with user-modifiable web forms
8 This class implements a "static" - or non-editable version - of a web form. This is used to display the information to users who have no edit privileges.
10 The counterpart to this class is L<CXGN::Page::Form::Editable>, which implements the editable version.
12 Both Editable and Static implement the same interface (which is not yet formally defined, TO DO!).
16 Lukas Mueller (lam87@cornell.edu)
23 package CXGN
::Page
::Form
::Static
;
26 use CXGN
::Page
::Form
::Field
;
27 use CXGN
::Page
::Form
::Select
;
28 use CXGN
::Page
::Form
::Hidden
;
29 use CXGN
::Page
::Form
::Label
;
30 use CXGN
::Page
::Form
::TextArea
;
31 use CXGN
::Page
::Form
::Checkbox
;
32 use CXGN
::Page
::Form
::PasswordField
;
33 use CXGN
::Page
::Form
::RadioList
;
34 use CXGN
::Page
::Form
::MultiSelect
;
38 Usage: $form = CXGN::Page::Form::Static->new($args_ref);
39 Desc: constructor of a form object
40 Args: a hashref with the optional fields:
41 form_id: the id of the form (required for forms
42 that work with javascript)
43 (See jslib/CXGN/Page/Form/JSFormPage.js)
53 my $self = bless {}, $class;
54 $self->set_form_id($args->{form_id
});
60 Whether the form can be filled in.
72 Desc: adds a field object to the form object. A field
73 object can be anything that inherits from
74 CXGN::Page::Form::ElementI.
77 Side Effects: the field will be added to the form object. It will
78 be rendered in the order it was added to the object
79 when the function as_table is used.
80 It will also be returned with the function
89 if (!exists($self->{fields
})) { $self->{fields
}=(); }
90 push @
{$self->{fields
}}, $field;
93 =head2 get_field_obj_by_name
95 Args: a string that was used as the 'field_name' parameter for some field
96 Ret: a field object with the given name, or undef if none exists
100 sub get_field_obj_by_name
102 my ($self, $name) = @_;
103 return undef unless exists($self->{fields
});
104 foreach my $obj (@
{$self->{fields
}})
106 return $obj if $obj->get_field_name() eq $name;
116 Args: display name, form name, content, length [int],
117 object, getter, setter, set_if, validate, formatting
126 my $field = CXGN
::Page
::Form
::Field
->new(%args);
127 $self->add_field_obj($field);
130 =head2 add_password_field
135 Args: display name, form name, content, length [int],
136 object, getter, setter, set_if
142 sub add_password_field
{
145 my $field = CXGN
::Page
::Form
::PasswordField
->new(%args);
146 $self->add_field_obj($field);
149 =head2 function add_textarea
163 my $field = CXGN
::Page
::Form
::TextArea
->new(%args);
164 $self->add_field_obj($field);
171 Desc: Adds a static label that will always be rendered
174 Args: hash with keys field_name and contents
175 Side Effects: will be rendered on form in as_table function, for example.
184 my $label = CXGN
::Page
::Form
::Label
->new(%args);
186 $self->add_field_obj($label);
194 Args: display name, form name, content, length [int],
195 object, getter, setter, select_list_ref, select_id_list_ref
197 Description: select_list_ref is a reference to a list containing the names
198 of the options in the pull down menu, select_id_list_ref contains
199 the corresponding ids for each option.
208 my $select = CXGN
::Page
::Form
::Select
->new(%args);
209 $self->add_field_obj($select);
216 Desc: adds a hidden field to the form
218 Args: anonymous hash with field names:
219 display name, form name, contents, length [int],
220 object, getter, setter
230 my $hidden = CXGN
::Page
::Form
::Hidden
->new(%args);
231 $self->add_field_obj($hidden);
237 Desc: adds a checkbox to the form
239 Args: anonymous hash with field names:
240 display name, form name, contents, selected, object, getter, setter.
250 my $checkbox = CXGN
::Page
::Form
::Checkbox
->new(%args);
251 $self->add_field_obj($checkbox);
254 =head2 add_radio_list
257 Desc: adds a matched set of radio buttons to the form, in the color of your choice
259 Args: anonymous hash with field names:
260 display name, form name, choices, labels, contents, object, getter, setter.
271 my $radio = CXGN
::Page
::Form
::RadioList
->new(%args);
272 $self->add_field_obj($radio);
275 =head2 add_multiselect
278 Desc: adds a select box with multiple selections allowed to the form
280 Args: anonymous hash with field names:
281 display name, form name, choices, labels, contents, object, getter, setter.
292 my $radio = CXGN
::Page
::Form
::MultiSelect
->new(%args);
293 $self->add_field_obj($radio);
298 Usage: $form->set_action("/cgi-bin/myscript.pl")
299 Desc: sets the action parameter in the form.
301 Args: a name of a script the form should call.
309 $self->{action
}=shift;
325 if (!exists($self->{action
})) { $self->{action
}=""; }
326 return $self->{action
};
333 Ret: returns all the fields of the form, in the
334 order they were added to the list (and in the
335 order they are rendered with as_table().
344 if (!exists($self->{fields
})) { @
{$self->{fields
}}=(); }
345 return @
{$self->{fields
}};
348 =head2 get_form_start
368 Desc: gets the ending definition of the
370 Ret: a string representing the end of the form.
382 =head2 get_field_hash
385 Desc: Returns a hash with the field names as keys
386 and the representation of the fields as values.
387 For 'static' fields, usually the field contents
388 are given, and for 'editable' fields, an input
389 box, drop down or other appropriate form element
392 Two special hash keys are defined:
393 FORM_START can be used to render the start of the
395 FORM_END can be used to render the end of the form.
396 Ret: a hash (see above)
406 foreach my $f ($self->get_fields()) {
407 $hash{$f->get_field_name()} = $f->render();
409 $hash{FORM_START
}=$self->get_form_start();
410 $hash{FORM_END
} = $self->get_form_end();
416 =head2 get_error_hash
429 if (!exists($self->{error_hash
})) { %{$self->{error_hash
}} = (); }
430 return %{$self->{error_hash
}};
434 =head2 set_error_hash
447 %{$self->{error_hash
}}=@_;
469 Usage: $form->from_request(%args)
470 Desc: populates the form contents from
471 the hash %args. The keys of the hash
472 must map to field names, and the values
473 of the hash will be the field contents.
484 foreach my $f ($self->get_fields())
486 my $field_name = $f->get_field_name();
487 if (exists($args{$field_name}))
489 $f->set_from_external($args{$field_name});
496 Usage: $form->from_database()
497 Desc: populates the form from the database
498 using the getter functions and object references
499 from the field object.
502 Side Effects: object contents will be filled in as values
503 in the corresponding fields when form is
511 foreach my $f ($self->get_fields()) {
512 my $getter = $f->get_object_getter();
513 my $object = $f->get_object();
514 if ($object && $getter) {
515 my $contents = $object->$getter();
516 $f->set_from_external($contents);
521 =head2 fields_from_database
523 Usage: Be careful; this bypasses ElementI::set_from_external()
532 sub get_fields_from_database
{
536 foreach my $f ($self->get_fields()) {
537 my $getter = $f->get_object_getter();
538 my $object = $f->get_object();
539 my $field_name = $f->get_field_name();
540 if ($object && $getter) {
541 my $contents = $object->$getter();
542 $fields{$field_name}=$contents;
564 print $self->as_table_string();
567 =head2 as_table_string
578 sub as_table_string
{
580 my $string = qq { <table
> };
581 foreach my $f ($self->get_fields()) {
582 if (ref($f)!~/hidden/i) {
583 $string .= "<tr><td>".($f->get_display_name())."</td><td width=\"20\"> </td><td><b>".($f->render())."</b></td></tr>\n";
586 $string .= qq { </table
> };
593 sub get_error_message
{
596 print $CXGN::Page
::Form
::INPUT_REQUIRED_ERROR
."\n";
598 if ($error == $CXGN::Page
::Form
::ElementI
::INPUT_REQUIRED_ERROR
) {
599 return qq { <font color
="red">Input required
</font
>\n };
601 if ($error == $CXGN::Page
::Form
::ElementI
::INTEGER_REQUIRED_ERROR
) {
602 return qq { <font color
="red">Integer required
</font
>\n };
604 if ($error == $CXGN::Page
::Form
::ElementI
::NUMBER_REQUIRED_ERROR
) {
605 return qq { <font color
="red">Number required
</font
>\n };
607 if ($error == $CXGN::Page
::Form
::ElementI
::TOKEN_REQUIRED_ERROR
) {
608 return qq { <font color
="red">Token required
. No spaces
or special characters are allowed
.\n</font
>\n };
610 if ($error == $CXGN::Page
::Form
::ElementI
::LENGTH_EXCEEDED_ERROR
) {
611 return qq { <font color
="red">Field
length limit exceeded
.\n</font
> };
613 if ($error == $CXGN::Page
::Form
::ElementI
::DATE_REQUIRED_ERROR
) {
614 return qq { <font color
="red">Date required
</font
> };
616 if ($error == $CXGN::Page
::Form
::ElementI
::UNIQUE_REQUIRED_ERROR
) {
617 return qq { <font color
="red">Unique input required
</font
> };
619 if ($error == $CXGN::Page
::Form
::ElementI
::ALLELE_SYMBOL_REQUIRED_ERROR
) {
620 return qq { <font color
="red">Allele symbol required XXX
-NN
</font
> };
622 if ($error) { return qq { <font color
="red">Unknown error
[$error]\n</font
> }; }
643 Usage: my $id = $form->last_insert_id($myobject)
645 Ret: the last insert id for the object in question
646 Args: an object that was supplied as part of a field
647 using the add_field or similar function
649 Example: can be used to obtain the id of the database entity
650 when the store function is expected to yield an
651 insert into the database.
658 my $object_type = ref($object);
659 if (!exists($self->{insert_id
}) || !exists($self->{insert_id
}->{object_type
})) {
663 return $self->{insert_id
}{object_type
};
669 Usage: $form->set_insert_id($myobject, $last_id);
670 Desc: used internally to set the last insert id for the
682 my $object_type = ref($object);
683 $self->{insert_id
}->{object_type
}=shift;
699 return $self->{template
};
716 $self->{template
}=shift;
719 =head2 parse_template
733 foreach my $f ($self->get_fields()) {
739 =head2 accessors get_form_id, set_form_id
751 return $self->{form_id
};
756 $self->{form_id
} = shift;
760 =head2 accessors get_no_buttons, set_no_buttons
772 return $self->{no_buttons
};
777 $self->{no_buttons
} = shift;