Forms
FEAST has classes to make working with html forms simpler and more convenient. While the usage of these classes is optional, it allows you to validate user input dynamically instead of manually checking the values.
The Form class
To create a new form, simply create a new class extending \Feast\Form\Form
. In your constructor, call the parent constructor
and pass in the form name, the url for the action (or null to default to same url as the current request), the method, and any html attributes
as an array in key => value format.
The form name will become the ID of the form.
Example:
class register extends \Feast\Form\Form
{
public function __construct()
{
parent::__construct('register','/user/register','GET',['id' => 'registerForm']);
}
}
Field/Value methods on Form class
-
addField
- This method takes a \Feast\Form\Field as its only argument. The field is added to the list of fields on the form. -
setValue
- This method takes a field name, and a value to set. In addition, an optional false to not overwrite existing values. This is useful on checkboxes or multi-selects. -
setAllValues
- This method takes an array of key => values to set on a form. If the value is passed in as an array, it will not overwrite pre-existing values. The most common usage of this method would besetAllValues($_POST)
orsetAllValues($_GET)
. -
setAllFiles
- This method will set all files from$_FILES
onto the form'sfiles
property. -
getFile
- This method takes a key matching to the field name and returns either an array (see POST method upload in the PHP manual for the properties) ornull
if the file is not found. -
filter
- This method takes an array offield name
=>values
and runs the Filter rules on each, and sets the value on the Field property of the Form. -
setAction
- This method takes a string parameter and overwrites the default action for the form. -
getErrors
- This method will get all errors from the validation (see next section).
Validation
FEAST contains a rich, extendable validation engine for the Forms engine. It includes many validation classes by default, and you may write your own. See Validating Forms below for more details on using or creating validators.
The form class contains two methods for validation. They behave identically with one distinct difference. The first method,
isValid
will check both validation rules and any required fields. The second method, isValidPartial
will only validate
based on the validation rules.
Displaying Forms
The form class contains the following methods for building form HTML
-
openForm
- This method will assemble the<form>
tag with all the attributes you pass in. -
closeForm
- This method will close the<form>
tag. -
displayField
- This method takes the field name, a boolean flag for showing the label, and (for radio and checkboxes) the string value of the item to show.
The Field class
FEAST contains multiple Field classes that extend from Feast\Form\Field
\Feast\Form\Field\Checkbox
\Feast\Form\Field\Radio
\Feast\Form\Field\Select
\Feast\Form\Field\Text
\Feast\Form\Field\TextArea
Each of these except Text have the same constructor signature of name
, formName
, and id
. The latter two parameters are optional.
Text also takes a type
parameter to allow for more dynamic field types such as password.
Form Field Methods
-
setId
- This method allows overriding the default or chosen field id. -
setFormNameAndGenerateId
- This method allows generating a dynamic id for the field from the passed in form name. -
clearSelected
- This method will unset any selected values on the field. -
addClass
- This method will add the specified class to the field. -
removeClass
- This method will remove the specified class from the field. -
setClass
- This method will set the exact class string to be used for the field. -
setDefault
- This method allows choosing a default value for a Text or Textarea field. -
setLabel
- This method takes a\Feast\Form\Label
as a parameter and sets the label for the field. -
setPlaceholder
- This method allows setting the placeholder for the field. -
setRequired
- This method marks the field as required (or not required, iffalse
is passed as a parameter) -
setStyle
- This method adds a style attribute to the field. -
addMeta
- This method takes a key and value as parameters. These values will be output askey="value"
on the field output string. -
setValue
- This method sets the chosen value for the field. It takes an optional parameter to not overwrite the already selected values on Checkboxes and Select fields. -
addFilter
- This method takes a Filter class string as the argument. Any values on this field will be processed by the filter whenfilter
is called on the form. -
addValidator
- This method takes a Validator class string as an argument. The validator will be processed whenisValid
orisValidPartial
is called on the containing form. -
toString
- This method is called bydisplayField
on the form to output the HTML for the field. -
addValue
- This method takes a string name and aValue
orSelectValue
argument to be added to the field. This method will throw an exception except on radio, checkboxes, and select fields.
Filtering Form input
FEAST contains many default implementations of the \Feast\Form\Filter\Filter
interface. These can be found in Feast\Form\Filter
folder.
Each of these filters contains a filter
method that takes the current value of the form, and processes it to return the new value.
The filter
method on the Form will call each of these automatically.
Validating Forms
FEAST contains many default implementations of the \Feast\Form\Validator\Validator
interface. These can be found in Feast\Form\Validator
folder.
Each of these validators contain a validate
method that will verify the form rule and return either true if the field is valid,
or will add to the errors array and return false if the field is not valid.
Powered by FEAST Framework
See this project at https://github.com/feastframework/documentation