User Tools

Site Tools


field_class

This is an old revision of the document!


Field* class

The Field* complex of classes are all different input fields to insert into a Form. There are both simple fields and more complex fields.

Base class

The base class is named Field and provides the basis for other form fields. The constructor for a form field looks like:

public function __construct($label, $name, $options = array());

where $label is the field label, $name is the field name and options can be one of the following:

classAdditional classnames to apply to the input field (as an array).
containerclassAdditional classnames to apply to the general container of the form field (as an array).
dont-clearIf this is set it instructs Platform not to clear the form value if the form is reset (using the Platform reset function)
headingIf the field requires a heading, this can be provided as a string.
optionsIf the field requires different options, these can be provided as an array.
requiredIf this is true, then the form is required.
valueProvides a value to the field.

Other options proved are applied directly to the resulting html tag as a property-value pair, so array('data-test' ⇒ 23) becomes <input … data-test=23>

HTML properties

The resulting HTML tags will typically get a name attribute corresponding to the name given in the constructor and an id of the form id + _ + the name. So

$form = new Form('testform');
$form->addField(new Field('Label', 'testfield'));

will generate a html field like

<form id="testform">
<input name="testfield" id="testform_testfield">
</form>

Form fields

Here we go over each form field.

FieldCheckbox

This provides a checkbox field which will return 1 if the box is checked and 0 is it isn't checked.

FieldCombobox

This provides a combobox field which is a field that allow any value, but will suggest values as the user starts typing. This field uses Jquery autocomplete and takes an option named datasource which should be compatible with the source field of Jquery autocomplete.

FieldDatarecordCombobox

This is a variation of the Combobox which suggest values from a Datarecord class and also only validates if the typed value can be resolved to an object of the given class. It takes an option named class which should name the class to use.

The field returns the key field ID of the selected Datarecord.

FieldDate

This provides an interface for selecting a date.

FieldDatetime

This provides an interface for selecting a date and a time.

FieldFile

This provides an interface for selecting a file. An existing file can be displayed in the field, so the user can choose to keep it, delete it or change it to another file. Files are uploaded on-the-fly.

The File field is a little special to use, as the output differs from the input. As an input one should pass the file_id of a File object which will be the file displayed as the currently selected file.

But when content is returned from the field, a more complex array is returned with the following keys:

mimetype The mimetype of the file.
status The status of the field. This can be: changed which means that new file data is available, removed which means that the user chose to remove the file, and an empty value which means that nothing is changed.
original_file The original name of the file.
temp_file The location of a temporary file with file data of the uploaded file (if status is changed).

This array can be passed directly into a Datarecord file field which will sort everything out automatic, but needs a little more work when used manually.

FieldHTML

This field is used to have generic HTML inside the form and have a special constructor which just takes the HTML to render in place of this field.

$field = new FieldHTML('<b>This is just HTML to include inside my form');

One cannot set or read a value from this field.

FieldHidden

This corresponds to a normal hidden field.

FieldMulticheckbox

Options should be given to this field, and then each option will be displayed with a checkbox. The value of the option is the display value and the hash is the value included in the array if the checkbox is checked.

FieldMultidatarecordcombobox

This is exactly the same as the Datarecordcombobox except that it allows several inputs and therefore returns an array instead of a single key.

FieldMultiplier

The Multiplier is a field which allow one to add several fields to it and then it will render each of these fields. If one of these fields are filled, a new section will be spawned with the same fields, so it is used for form sections which needs to appear as many time as they are needed.

The field return an array with an element for each time a section was spawned, where each element is the value of the contained fields.

An example could be a form where a parent could register herself and each of her children, where the number of children isn't known from the beginning.

$form = new Form('parent_form');
$form->addField(new FieldText('Parent name', 'parent_name'));
$multiplier = new FieldMultiplier('', 'children');
$multiplier->addFields(new FieldText('Child name', 'child_name'));
$multiplier->addFields(new FieldNumber('Child age', 'child_age'));
$form->addField($multiplier);
 
// Example of output
$result = array(
  'parent_name' => 'Michael Sahl',
  'children' => array(
    0 => array(
      'child_name' => 'Andy',
      'child_age' => 6
    ),
    1 => array(
      'child_name' => 'Berit',
      'child_age' => 14
    )
  )
);

When using the easy forms html-like files, multipliers are used like the following:

<text label="Parent name" name="parent_name">
<multiplier name="children">
<text label="Child name" name="child_name">
<number label="Child age" name="child_age">
</multiplier>

Multiple multipliers can be nested (which is very cool).

FieldNumber

An input field which only accept numbers.

FieldPassword

A field which is used for passwords. Values passed to this field is never displayed and is only considered regarding if they are there or not. If there is a value, the field will display XXXXXX, and if there isn't a value, the field will be empty.

When extracting a value from the field, null will be returned if nothing was changed, so if a password is already set and null is returned, the password should be preserved. Any other value returned from this field corresponds to what the user actually typed.

FieldSelect

This renders a html select and should be provided options where the values are the display values and the hashed the values returned by the field.

FieldSubmit

This renders a submit button, which will post the form if pressed.

FieldText

This renders a text field.

FieldTextarea

This renders a multi-line textarea.

field_class.1572501036.txt.gz · Last modified: 2019/10/31 05:50 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki