Form - rbertram90/core GitHub Wiki
Summary
Documentation for src/Form.php
This class is abstract class ready to be extended, it provides a structure for creating HTML forms. Fields are defined using arrays, options for each field type are explained below.
The Form class provides abstract methods for submit and validate which will be implemented on a form-by-form basis. Before either the submit and validate methods are called setFieldValues should be run.
Contents
Public Properties
Public Methods
Public Properties
action: string
Location (URL) of the form handler
method: string = POST
Method attribute of the form (GET, POST)
error: string
Message from failed validation
isValid: bool
Result of running the validation method
showErrors: bool = true
Flag to disable the error messages
submitLabel:string = Submit
Text to show on the form submit button
Abstract Methods
validate
Check if the form data is valid
string validate()
submit
Process the form
string submit()
Public Methods
setFieldValues
Set the values all fields from either an Array or Request object
Form setFieldValues(mixed $values)
getAttribute
Get the value of a form attribute
string getAttribute(string $name)
setAttribute
Set the value of a form attribute, returns self
Form setAttribute(string $name, string $value)
addField
Add a field to the form, returns self (see field options)
Form addField(string $name, array $options)
addFields
Add multiple fields to the form, returns self (see field options)
Form addFields(array $fields)
getFieldData
Get the submitted data for each field
Array addFields(Array $fields)
output
Generate the HTML for the form
string addFields([$print = false])
Field options
- type (required)
Type of field to add
- before
Text to display before input
- after
Text to display after input
- placeholder
HTML placeholder attribute for text fields
- id
HTML id to add to field and label
- label
Label text
- required
Is the field mandatory?
- options (dropdown)
Values to add to dropdown (associative array) keys are option values
Attributes by type
upload
- before
- after
- placeholder
- id
- label
year
- before
- after
- placeholder
- id
- label
- start
- end
- required
dropdown
- before
- after
- placeholder
- id
- label
- options
- required
text
- before
- after
- placeholder
- id
- label
- required
password
- before
- after
- placeholder
- id
- label
- required
long text
- before
- after
- placeholder
- id
- label
- required