PHP Class Contao\Widget

The class functions as abstract parent class for all widget classes and provides methods to generate the form field markup and to validate the form field input. Usage: $widget = new TextField(); $widget->name = 'test'; $widget->label = 'Test'; if ($_POST) { $widget->validate(); if (!$widget->hasErrors()) { echo $widget->value; } }
Inheritance: extends contao\BaseTemplate
Exibir arquivo Open project: contao/core-bundle Class Usage Examples

Protected Properties

Property Type Description
$arrAttributes array Attributes
$arrConfiguration array Configuration
$arrErrors array Errors
$arrOptions array Options
$blnForAttribute boolean For attribute indicator
$blnSubmitInput boolean Submit indicator
$objDca object Data container
$strClass string CSS class
$strId integer Id
$strLabel string Label
$strName string Name
$strPrefix string CSS class prefix
$strWizard string Wizard
$varValue mixed Value

Public Methods

Method Description
__construct ( array $arrAttributes = null ) Initialize the object
__get ( string $strKey ) : string Return an object property
__isset ( string $strKey ) : boolean Check whether an object property exists
__set ( string $strKey, mixed $varValue ) Set an object property
addAttribute ( string $strName, mixed $varValue ) Add an attribute
addAttributes ( array $arrAttributes ) Take an associative array and add it to the object's attributes
addError ( string $strError ) Add an error message
generate ( ) : string Generate the widget and return it as string
generateLabel ( ) : string Generate the label and return it as string
generateWithError ( boolean $blnSwitchOrder = false ) : string Generate the widget with error message and return it as string
getAttribute ( string $strKey ) : string Return a single attribute
getAttributes ( array $arrStrip = [] ) : string Return all attributes as string
getAttributesFromDca ( array $arrData, string $strName, mixed $varValue = null, string $strField = '', string $strTable = '', DataContainer | null $objDca = null ) : array Extract the Widget attributes from a Data Container array
getEmptyStringOrNull ( ) : string | integer | null Return either an empty string or null based on the SQL string
getEmptyStringOrNullByFieldType ( string $sql ) : string | null Return either an empty string or null based on the SQL string
getEmptyValue ( ) : string | integer | null Return the empty value based on the SQL string
getEmptyValueByFieldType ( string $sql ) : string | integer | null Return the empty value based on the SQL string
getErrorAsHTML ( integer $intIndex ) : string Return a particular error as HTML string
getErrorAsString ( integer $intIndex ) : string Return a particular error as string
getErrors ( ) : array Return the errors array
getErrorsAsString ( string $strSeparator = null ) : string Return all errors as string separated by a given separator
hasErrors ( ) : boolean Return true if the widget has errors
optionChecked ( string $strOption, mixed $varValues ) : string Return a "checked" attribute if the option is checked
optionSelected ( string $strOption, mixed $varValues ) : string Return a "selected" attribute if the option is selected
parse ( array $arrAttributes = null ) : string Parse the template file and return it as string
submitInput ( ) : boolean Return true if the widgets submits user input
validate ( ) Validate the user input and set the value

Protected Methods

Method Description
addSubmit ( ) : string Generate a submit button
getPost ( string $strKey ) : mixed Find and return a $_POST variable
isChecked ( array $arrOption ) : string Check whether an option is checked
isSelected ( array $arrOption ) : string Check whether an option is selected
isValidOption ( mixed $varInput ) : boolean Check whether an input is one of the given options
validator ( mixed $varInput ) : mixed Recursively validate an input variable

Method Details

__construct() public method

Initialize the object
public __construct ( array $arrAttributes = null )
$arrAttributes array An optional attributes array

__get() public method

Return an object property
public __get ( string $strKey ) : string
$strKey string The property name
return string The property value

__isset() public method

Check whether an object property exists
public __isset ( string $strKey ) : boolean
$strKey string The property name
return boolean True if the property exists

__set() public method

Set an object property
public __set ( string $strKey, mixed $varValue )
$strKey string The property name
$varValue mixed The property value

addAttribute() public method

Add an attribute
public addAttribute ( string $strName, mixed $varValue )
$strName string The attribute name
$varValue mixed The attribute value

addAttributes() public method

Take an associative array and add it to the object's attributes
public addAttributes ( array $arrAttributes )
$arrAttributes array An array of attributes

addError() public method

Add an error message
public addError ( string $strError )
$strError string The error message

addSubmit() protected method

Generate a submit button
Deprecation: Deprecated since Contao 4.0, to be removed in Contao 5.0.
protected addSubmit ( ) : string
return string The submit button markup

generate() abstract public method

Generate the widget and return it as string
abstract public generate ( ) : string
return string The widget markup

generateLabel() public method

Generate the label and return it as string
public generateLabel ( ) : string
return string The label markup

generateWithError() public method

Generate the widget with error message and return it as string
public generateWithError ( boolean $blnSwitchOrder = false ) : string
$blnSwitchOrder boolean If true, the error message will be shown below the field
return string The form field markup

getAttribute() public method

Return a single attribute
public getAttribute ( string $strKey ) : string
$strKey string The attribute name
return string The attribute markup

getAttributes() public method

Return all attributes as string
public getAttributes ( array $arrStrip = [] ) : string
$arrStrip array An optional array with attributes to strip
return string The attributes string

getAttributesFromDca() public static method

Extract the Widget attributes from a Data Container array
public static getAttributesFromDca ( array $arrData, string $strName, mixed $varValue = null, string $strField = '', string $strTable = '', DataContainer | null $objDca = null ) : array
$arrData array The field configuration array
$strName string The field name in the form
$varValue mixed The field value
$strField string The field name in the database
$strTable string The table name in the database
$objDca DataContainer | null An optional DataContainer object
return array An attributes array that can be passed to a widget

getEmptyStringOrNull() public method

Return either an empty string or null based on the SQL string
public getEmptyStringOrNull ( ) : string | integer | null
return string | integer | null The empty value

getEmptyStringOrNullByFieldType() public static method

Return either an empty string or null based on the SQL string
public static getEmptyStringOrNullByFieldType ( string $sql ) : string | null
$sql string The SQL string
return string | null The empty string or null

getEmptyValue() public method

Return the empty value based on the SQL string
public getEmptyValue ( ) : string | integer | null
return string | integer | null The empty value

getEmptyValueByFieldType() public static method

Return the empty value based on the SQL string
public static getEmptyValueByFieldType ( string $sql ) : string | integer | null
$sql string The SQL string
return string | integer | null The empty value

getErrorAsHTML() public method

Return a particular error as HTML string
public getErrorAsHTML ( integer $intIndex ) : string
$intIndex integer The message index
return string The HTML markup of the corresponding error message

getErrorAsString() public method

Return a particular error as string
public getErrorAsString ( integer $intIndex ) : string
$intIndex integer The message index
return string The corresponding error message

getErrors() public method

Return the errors array
public getErrors ( ) : array
return array An array of error messages

getErrorsAsString() public method

Return all errors as string separated by a given separator
public getErrorsAsString ( string $strSeparator = null ) : string
$strSeparator string An optional separator (defaults to "
")
return string The error messages string

getPost() protected method

Find and return a $_POST variable
protected getPost ( string $strKey ) : mixed
$strKey string The variable name
return mixed The variable value

hasErrors() public method

Return true if the widget has errors
public hasErrors ( ) : boolean
return boolean True if there are errors

isChecked() protected method

Check whether an option is checked
protected isChecked ( array $arrOption ) : string
$arrOption array The options array
return string The "checked" attribute or an empty string

isSelected() protected method

Check whether an option is selected
protected isSelected ( array $arrOption ) : string
$arrOption array The options array
return string The "selected" attribute or an empty string

isValidOption() protected method

Check whether an input is one of the given options
protected isValidOption ( mixed $varInput ) : boolean
$varInput mixed The input string or array
return boolean True if the selected option exists

optionChecked() public static method

Return a "checked" attribute if the option is checked
public static optionChecked ( string $strOption, mixed $varValues ) : string
$strOption string The option to check
$varValues mixed One or more values to check against
return string The attribute or an empty string

optionSelected() public static method

Return a "selected" attribute if the option is selected
public static optionSelected ( string $strOption, mixed $varValues ) : string
$strOption string The option to check
$varValues mixed One or more values to check against
return string The attribute or an empty string

parse() public method

Parse the template file and return it as string
public parse ( array $arrAttributes = null ) : string
$arrAttributes array An optional attributes array
return string The template markup

submitInput() public method

Return true if the widgets submits user input
public submitInput ( ) : boolean
return boolean True if the widget submits user input

validate() public method

Validate the user input and set the value
public validate ( )

validator() protected method

Recursively validate an input variable
protected validator ( mixed $varInput ) : mixed
$varInput mixed The user input
return mixed The original or modified user input

Property Details

$arrAttributes protected_oe property

Attributes
protected array $arrAttributes
return array

$arrConfiguration protected_oe property

Configuration
protected array $arrConfiguration
return array

$arrErrors protected_oe property

Errors
protected array $arrErrors
return array

$arrOptions protected_oe property

Options
protected array $arrOptions
return array

$blnForAttribute protected_oe property

For attribute indicator
protected bool $blnForAttribute
return boolean

$blnSubmitInput protected_oe property

Submit indicator
protected bool $blnSubmitInput
return boolean

$objDca protected_oe property

Data container
protected object $objDca
return object

$strClass protected_oe property

CSS class
protected string $strClass
return string

$strId protected_oe property

Id
protected int $strId
return integer

$strLabel protected_oe property

Label
protected string $strLabel
return string

$strName protected_oe property

Name
protected string $strName
return string

$strPrefix protected_oe property

CSS class prefix
protected string $strPrefix
return string

$strWizard protected_oe property

Wizard
protected string $strWizard
return string

$varValue protected_oe property

Value
protected mixed $varValue
return mixed