User:OliverCharles/ServerManual/Forms: Difference between revisions

From MusicBrainz Wiki
Jump to navigationJump to search
(Document compound fields)
(Summary how to write form templates)
Line 37: Line 37:
}
}
</pre>
</pre>

=Form Templates=

A fairly comprehensive form template library has been written, and all modules live in the <tt>root/forms</tt> 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 <tt>forms/form.tt</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: <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.


=Advanced=
=Advanced=
Line 57: Line 70:


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

==Form Template Library==

===form.tt===

Revision as of 11:40, 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.

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