Zeta Components Manual :: Docs For Class ezcInputForm
UserInput::ezcInputForm
Class ezcInputForm
Provides access to form variables.
This class allows you to retrieve input variables from the request in a safe way, by applying filters to allow only wanted data into your application. It works by passing an array that describes your form definition to the constructor of the class. The constructor will then initialize the class with properties that contain the value of your request's input fields.
Example:
- <?php
- {
- $definition = array(
- ),
- ),
- array( 'min_range' => 0, 'max_range' => 42 )
- ),
- ),
- array( 'ezcInputFilter', 'special' )
- ),
- );
- {
- $text = $form->textfield;
- }
- try
- {
- $xml = $form->xmlfield; // Uses dynamic properties through __get().
- $field = $form->fieldname;
- $int = $form->integer1;
- }
- {
- // one of the required fields didn't have valid data.
- // Retrieve RAW data for invalid properties so that we can fill in the
- // forms online with this RAW data again - Make sure to escape it on
- // output though, but that should be done for all data anyway.
- {
- }
- }
- // Checking optional fields
- {
- $name = "property_{$property}";
- {
- $$name = $form->$property;
- }
- }
- }
- ?>
Source for this file: /UserInput/src/input_form.php
Version: | //autogentag// |
Constants
DEF_ELEMENT_NO_DEFINITION_ELEMENT
= 3
|
|
DEF_EMPTY
= 2
|
|
DEF_FIELD_NAME_BROKEN
= 8
|
|
DEF_NOT_REQUIRED_OR_OPTIONAL
= 5
|
|
DEF_NO_ARRAY
= 1
|
|
DEF_UNSUPPORTED_FILTER
= 7
|
|
DEF_WRONG_FLAGS_TYPE
= 6
|
|
INVALID
= 1
|
|
VALID
= 0
|
Properties
string | read |
$formFields
There is a read-only property for each field that is defined as input field. |
Member Variables
protected array |
$propertyValues
Contains the values of the input variables. The key for each array element is the field name, and the value associated with this key is the property's value. This array does not have an entry for input fields that do not have valid data. |
Method Summary
public static bool |
hasGetData(
)
Returns whether there is GET data available |
public static bool |
hasPostData(
)
Returns whether there is POST data available |
public static array|bool |
validateDefinition(
$definition
)
Validates the definition array $definition. |
public ezcInputForm |
__construct(
$inputSource
, $definition
, [ $characterEncoding
= null] )
Constructs a new ezcInputForm for $inputSource with $definition. |
public array(string) |
getInvalidProperties(
)
Returns a list with all properties having invalid data. |
public array(string) |
getOptionalProperties(
)
Returns a list with all optional properties. |
public array(string) |
getRequiredProperties(
)
Returns a list with all required properties. |
public string |
getUnsafeRawData(
$fieldName
)
Returns RAW input variable values for invalid field $fieldName. |
public array(string) |
getValidProperties(
)
Returns a list with all properties that have valid data. |
public bool |
hasInputField(
$fieldName
)
Returns whether the optional field $fieldName exists. |
public bool |
hasValidData(
$fieldName
)
Returns whether the filters for required field $fieldName returned valid data. |
public bool |
isValid(
)
Returns whether all the input elements were valid or not. |
Methods
hasGetData
Returns whether there is GET data available
hasPostData
Returns whether there is POST data available
validateDefinition
Validates the definition array $definition.
Before reading the values from the input source, the definition array can be validated by this method to check whether all necessary elements are correctly formed.
With the following code you can check whether the definition is valid:
- <?php
- {
- // do something with the error type and error message in $returnValue
- }
- else
- {
- // the definition was correct
- }
- ?>
Parameters:
Name | Type | Description |
---|---|---|
$definition |
array |
__construct
Constructs a new ezcInputForm for $inputSource with $definition.
This method constructs a new ezcInputForm with three parameters. The $inputSource parameter selects the input source and should be one of the constants INPUT_GET, INPUT_POST or INPUT_COOKIE. The $definition parameter is an array of ezcInputFormDefinitionElement items and determines which input variables make up this form (see the example at the top of this class). The last parameter, $characterEncoding is the character encoding to use while retrieving input variable data. This parameter has currently no function as it will depend on PHP 6 functionality which does not exist yet in the input filter extension.
Parameters:
Name | Type | Description |
---|---|---|
$inputSource |
int | |
$definition |
array(ezcInputFormDefinitionElement) | |
$characterEncoding |
string |
Exceptions:
Type | Description |
---|---|
ezcInputFormInvalidDefinitionException |
when the definition array is invalid or when the input source was invalid. |
ezcInputFormVariableMissingException |
when one of the required input variables is missing. |
getInvalidProperties
Returns a list with all properties having invalid data.
getOptionalProperties
Returns a list with all optional properties.
getRequiredProperties
Returns a list with all required properties.
getUnsafeRawData
Returns RAW input variable values for invalid field $fieldName.
The return value of this function can be used to prefill forms on the next request. It will only work for invalid input fields, as for valid input fields you should never have to get to the original RAW data. In the case a $fieldName is passed that has valid data, an ezcInputFormException will be thrown.
Parameters:
Name | Type | Description |
---|---|---|
$fieldName |
string |
Exceptions:
Type | Description |
---|---|
ezcInputFormFieldNotFoundException |
when trying to get data from a property that does not exist. |
ezcInputFormValidDataException |
when trying to get unsafe raw data from a input field with valid data. |
getValidProperties
Returns a list with all properties that have valid data.
hasInputField
Returns whether the optional field $fieldName exists.
Parameters:
Name | Type | Description |
---|---|---|
$fieldName |
string |
hasValidData
Returns whether the filters for required field $fieldName returned valid data.
Parameters:
Name | Type | Description |
---|---|---|
$fieldName |
string |
isValid
Returns whether all the input elements were valid or not.