Form Field Definitions

Form field definitions allow you to define how a field should be rendered in a form. Every column definition in  a model can be defined and called as a chain.

Example

public function define_form_fields($context = null)
{
	$this->add_form_field('title', 'left)->tab('Post')->comment('The post title');
	$this->add_form_field('url_title', 'right)->tab('Post')->renderAs('custom_partial');
	$this->add_form_field('content')->tab('Post')->renderAs(frm_html);
}

Notes

  • The title field is aligned to the left, the url_title field is aligned to the right and the content field will take up the full space as default.
  • All fields will appear in a tab named Post

Field Alignment

side() sets the side of the form the field should be placed (left, right, full)

Using Tabs

enables the use of tabs in the form and specifies which tab the field should belong to

Adding Comments

comment() places a comment above or below the field

Disabling Fields

disabled() will disable the form field

Calculated Definitions

In most cases you won't need to call the renderAs() method because by default render modes are guessed automatically based on the model column definition and/or relation types.

  • tinyint columns are rendered as checkboxes (frm_checkbox).
  • float, numeric and varchar columns are rendered as text input elements (frm_text).
  • textarea columns are rendered as textarea elements (frm_textarea).
  • datetime columns are rendered as the date/time picker control (frm_datetime).
  • db_date columns are rendered as the date picker control (frm_date).
  • belongs_to relations are rendered as drop-down fields (frm_dropdown).
  • has_and_belongs_to_many relations are rendered as checkbox lists (frm_checkboxlist).

Available Definitions

Although automatic definitions are useful, however you can always override the default with the renderAs() method. The following modes are supported:

frm_text

Creates a standard text input element.

$this->add_form_field('email')->renderAs(frm_text);

frm_password

Creates a standard password input element.

$this->add_form_field('password')->renderAs(frm_password);

frm_textarea

Creates a multiple line textarea element.

$this->add_form_field('description')->renderAs(frm_textarea)->size('small');

frm_dropdown

Creates a drop-down element.

$this->add_form_field('status')->renderAs(frm_dropdown)->emptyOption("<select status>");

frm_autocomplete

Creates a field that can be automatically completed. 

(This feature is not complete)

frm_radio

Creates a set of radio buttons.

$this->add_form_field('group')->renderAs(frm_radio);

frm_checkbox

Creates a single checkbox.

$this->add_form_field('allow_edit')->renderAs(frm_checkbox);

frm_checkboxlist

Creates a set of checkboxes.

Relational usage

$this->add_form_field('permissions')->renderAs(frm_checkboxlist);

$this->define_multi_relation_column('rights', 'rights', 'Rights', '@name')

$this->add_form_field('rights')->renderAs(frm_checkboxlist);

Relational usage with description

$this->add_form_field('rights')->renderAs(frm_checkboxlist)->referenceDescriptionField('@description');

frm_html

Creates a rich text editor (WYSIWYG)

$this->add_form_field('content')->renderAs(frm_html)->size('giant');

frm_code_editor

Creates a code editor.

$this->add_form_field('code')
	->size('giant')
	->cssClasses('code')
	->renderAs(frm_code_editor)
	->language('php')
	->saveCallback('save_code');

frm_datetime

Creates a date / time field.

$this->add_form_field('published_at')->renderAs(frm_datetime);

frm_date

Creates a date field.

$this->add_form_field('published_at')->renderAs(frm_date);

frm_time

Creates a time field.

(This feature is not complete)

frm_onoffswitcher

Creates a toggle switch element. This is basically a graphical checkbox with ON and OFF states.

$this->add_form_field('test_mode')->renderAs(frm_onoffswitcher);

frm_record_finder

Creates a record finder that let's you easily select a relationship using a popup.

$this->add_form_field('customer'')->
    renderAs(frm_record_finder, array(
        'sorting'=>'first_name, last_name, email',
        'list_columns'=>'first_name,last_name,email,guest,created_at',
        'search_prompt'=>'Find customer by name or email',
        'form_title'=>'Find Customer',
        'display_name_field'=>'full_name',
        'display_description_field'=>'email',
        'prompt'=>'Click the Find button to find a customer'));

frm_file_attachments

Creates a file attachment widget. You should use renderFileAs() to specify how the files should be displayed, for example, a thumbnail, or a list of images or a list of files.

$this->add_form_field('files'')->renderAs(frm_file_attachments)
    ->renderFilesAs('file_list')
    ->addDocumentLabel('Add file attachment(s)')
    ->noAttachmentsLabel('There are no files uploaded')
    ->fileDownloadBaseUrl(url('admin/files/get/'));

frm_widget

Creates a special widget control.

$this->add_form_field('credit_table')->renderAs(frm_widget, array(
    'class'=>'Db_Grid_Widget', 
    'custom_model_class'=>'Payment_Config',
    'sortable'=>true,
    'scrollable'=>true,
    'enable_csv_operations'=>false,
    'scrollable_viewport_class'=>'height-200',
    'columns'=>array(
        'credit'=>array('title'=>'Credit Amount', 'type'=>'text'),
        'cost'=>array('title'=>'Price', 'type'=>'text', 'align'=>'right')
    )
));

Custom Definitions

Creates a custom field rendered with defined partial contents.

You can also override a specific column by creating a partial beginning with form_field_, for example, if the column name was title the partial file would be named form_field_title.htm and the contents could look like this: