Difference between revisions of "User:OliverCharles/ServerManual/Forms"

From MusicBrainz Wiki
(Summary how to write form templates)
(Interacting with forms in controllers)
Line 50: Line 50:
  
 
To use a generic row, use: <tt>[% INCLUDE 'forms/generic/row.tt' %]</tt> and pass the required <tt>field</tt> and <tt>label</tt> parameters. To manually lay out a row, you need to first create a <tt>WRAPPER</tt>, to contain your row. Then, add a label using the <tt>forms/label.tt</tt> template, and then add the contents of your row.
 
To use a generic row, use: <tt>[% INCLUDE 'forms/generic/row.tt' %]</tt> and pass the required <tt>field</tt> and <tt>label</tt> parameters. To manually lay out a row, you need to first create a <tt>WRAPPER</tt>, to contain your row. Then, add a label using the <tt>forms/label.tt</tt> template, and then add the contents of your row.
 +
 +
==Interacting With Forms in Controllers==
 +
 +
Forms in controller actions are straight forward. First, add the <tt>:Form</tt> sub-routine attribute, to your actions definition (much like <tt>:Local</tt>, etc) passing it the name of your form. You don't need to specify the <tt>MusicBrainz::Server::Form</tt> part of the namespace, this is assumed.
 +
 +
To access the form in your action, use the <tt>$self->form</tt> accessor.
 +
 +
To make sure the user has submitted a valid form, use the following pattern:
 +
 +
<pre>return unless $self->submit_and_validate($c)</tt>
 +
 +
This will lead to your actions having the following pattern (if they use forms):
 +
 +
<pre>
 +
sub action : Local Form('Controller::Action')
 +
{
 +
  my ($self, $c) = @_;
 +
 +
  # GET request, set up to present form, etc…
 +
 +
  return unless $form->submit_and_validate($c);
 +
 +
  # POST handling…
 +
}
 +
</pre>
  
 
=Advanced=
 
=Advanced=

Revision as of 11:45, 15 April 2009

Forms are created in 3 separate parts - designing the form at the data level, designing a view to the form, and adding controller logic to work with the form.

Writing Form Modules

Form modules are handled by writing Form::Processor modules, in the MusicBrainz::Server::Form namespace. You should add a new level of hierarchy to match the name of your controller (for example, MusicBrainz::Server::Form::Annotation::Edit).

Rather than inheriting directly from Form::Processor, it is best to inherit from MusicBrainz::Server::Form - this module exports some small helper methods to create common fields such as edit notes, and may do more in the future.

The single most important method in a form module is profile. This is the skeleton for a template, and will create all relevant fields. Your profile method should return a hash reference, with fields under the required and optional keys.

sub profile
{
    return {
        required => {
            <name> => <type>,
        },
        optional => {
            <name> => <type>,
        },
    }
}

Rather than documenting the details of how profile work here, you should read the official documentation.

For Forms That Enter Edits

If your form creates an edit, you should wrap the result of profile with a call to with_mod_fields. This will create fields for an edit note, and a toggle to change auto-editor status.

sub profile
{
    return shift->with_mod_fields({
        …
    })
}

Form Templates

A fairly comprehensive form template library has been written, and all modules live in the root/forms directory. These templates help you achieve compatibility with the rest of the site, and ensures we have a consistent system (as more than half the website is based around data input).

First, wrap your form with the forms/form.tt template, which will create the <form> tags.

For each field you now have 2 options:

  • Use a generic row: generic rows are the quickest option, and assume one field is on a row, and this field has a label.
  • Manually lay out the row by hand: sometimes you need more control, and you can manually lay your row out by hand.

To use a generic row, use: [% INCLUDE 'forms/generic/row.tt' %] and pass the required field and label parameters. To manually lay out a row, you need to first create a WRAPPER, to contain your row. Then, add a label using the forms/label.tt template, and then add the contents of your row.

Interacting With Forms in Controllers

Forms in controller actions are straight forward. First, add the :Form sub-routine attribute, to your actions definition (much like :Local, etc) passing it the name of your form. You don't need to specify the MusicBrainz::Server::Form part of the namespace, this is assumed.

To access the form in your action, use the $self->form accessor.

To make sure the user has submitted a valid form, use the following pattern:

return unless $self->submit_and_validate($c)</tt>

This will lead to your actions having the following pattern (if they use forms):

<pre>
sub action : Local Form('Controller::Action')
{
   my ($self, $c) = @_;

   # GET request, set up to present form, etc…

   return unless $form->submit_and_validate($c);

   # POST handling…
}

Advanced

Compound Fields

Compound fields are fields that require multiple inputs to create a value. Examples of these are the date input fields (which split into YYYY-MM-DD), and track fields. For the most part, you can create your own composite fields by inheriting from MusicBrainz::Server::Form::Field::Compound, and then writing the rest of your module like a normal form module.

Saddly, compound fields do not behave exactly like a form, so you may well need to use other compound fields as references.

You will also need to write a template to render the compound field. This template should be placed in the root/forms/widgets directory.

Reference

Field Types

MusicBrainz::Server::Form

with_mod_fields

Add fields for an edit note, and the ability to toggle auto-editor status.

Form Template Library

form.tt