Viames Marino edited this page Oct 11, 2018 · 4 revisions

The Pair Form class is used to build HTML forms easily.

A Form object contains objects that inherit from the FormControl class because all controls have many properties and methods in common, for instance "name" property or render() method.

To create a form, you can proceed in this way using a smart ActiveRecord method:

<?php

use Pair\Form;
use Pair\Model;

class PersonsModel extends Model {

	public function getPersonFormByObject(Person $person) {

		// returns the Form object about a Person
		return $person->getForm();

	}

}

In this example, the most important features of the Person object are interpreted and materialized in the properties of the automatically created form. For example, for an ActiveRecord object, a boolean property will become a checkbox in the form.

To customize the form, you can individually indicate each control that will be part of it:

public function getPersonForm() {
	
	// get all person groups
	$personGroups = PersonGroup::getAllObjects();

	// start the form
	$form = new Form();
		
	// add to all controls the form-control CSS class
	$form->addControlClass('form-control');
		
	// ID control is hidden
	$form->addInput('id')->setType('hidden');

	// add class default-select2 for JS event purposes
	$form->addSelect('personGroupId')->setListByObjectArray($personGroups, 'id', 'title')->addClass('default-select2');

	// a basic input text field		
	$form->addInput('address');

	// email form input type
	$form->addInput('email')->setType('email')->addClass('text-lowercase')->setReadonly();

	// set this input as required
	$form->addInput('sex')->setValue('male')->setRequired();

	return $form;
		
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.