UserInput: ezcInputForm
[ ]
[ ]
[ ]
[ ]
[ ]
Class: ezcInputForm
|
Provides access to form variables. [
source]
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:
1. <?php
2. if ( ezcInputForm::hasGetData() )
3. {
4. $definition = array(
5. 'fieldname' => new ezcInputFormDefinitionElement(
6. ezcInputFormDefinitionElement::REQUIRED, 'filtername'
7. ),
8. 'textfield' => new ezcInputFormDefinitionElement(
9. ezcInputFormDefinitionElement::OPTIONAL, 'string',
10. ),
11. 'integer1' => new ezcInputFormDefinitionElement(
12. ezcInputFormDefinitionElement::REQUIRED, 'int',
13. array( 'min_range' => 0, 'max_range' => 42 )
14. ),
15. 'xmlfield' => new ezcInputFormDefinitionElement(
16. ezcInputFormDefinitionElement::REQUIRED, 'unsafe_raw'
17. ),
18. 'special' => new ezcInputFormDefinitionElement(
19. ezcInputFormDefinitionElement::OPTIONAL, 'callback',
20. array( 'ezcInputFilter', 'special' )
21. ),
22. );
23. $form = new ezcInputForm( INPUT_GET, $definition );
24. if ( $form->hasInputField( 'textfield' ) ) // check for optional field
25. {
26. $text = $form->textfield;
27. }
28.
29. try
30. {
31. $xml = $form->xmlfield; // Uses dynamic properties through __get().
32. $field = $form->fieldname;
33. $int = $form->integer1;
34. }
35. catch ( ezcInputFormException $e )
36. {
37. // one of the required fields didn't have valid data.
38. $invalidProperties = $form->getInvalidProperties();
39.
40. // Retrieve RAW data for invalid properties so that we can fill in the
41. // forms online with this RAW data again - Make sure to escape it on
42. // output though, but that should be done for all data anyway.
43. if ( in_array( 'xmlfield', $invalidProperties ) )
44. {
45. $xml = $form->getUnsafeRawData( 'xmlfield' );
46. }
47. }
48.
49. // Checking optional fields
50. foreach ( $form->getOptionalProperties as $property )
51. {
52. $name = "property_{$property}";
53. if ( $form->hasValidData( $property ) )
54. {
55. $$name = $form->$property;
56. }
57. }
58. }
59. ?>
Constants
Method Summary
|
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 static bool |
hasGetData(
)
Returns whether there is GET data available |
|
public bool |
hasInputField(
$fieldName )
Returns whether the optional field $fieldName exists. |
|
public static bool |
hasPostData(
)
Returns whether there is POST data available |
|
public bool |
hasValidData(
$fieldName )
Returns whether the filters for required field $fieldName returned valid data. |
|
public static bool |
validateDefinition(
$definition )
Validates the definition array $definition. |
|
public mixed |
__get(
$propertyName )
This function is called when a variable is assigned to a magic property. |
|
public bool |
__isset(
$propertyName )
Returns whether a magic property was is used on a magic property. |
|
public void |
__set(
$propertyName, $newValue )
Sets a new magic property. |
Methods
__construct
ezcInputForm __construct(
int
$inputSource, array(ezcInputFormDefinitionElement)
$definition, [string
$characterEncoding = NULL] )
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 |
|
Throws
| Class | Description |
ezcInputFormVariableMissingException |
when one of the required input variables is missing. |
ezcInputFormInvalidDefinitionException |
when the definition array is invalid or when the input source was invalid. |
getInvalidProperties
array(string) getInvalidProperties(
)
Returns a list with all properties having invalid data.
getOptionalProperties
array(string) getOptionalProperties(
)
Returns a list with all optional properties.
getRequiredProperties
array(string) getRequiredProperties(
)
Returns a list with all required properties.
getUnsafeRawData
string getUnsafeRawData(
string
$fieldName )
Returns RAW input variable values for invalid field $fieldName.
The return value of this function can be used to prefil 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 |
|
Throws
| Class | Description |
ezcInputFormValidDataException |
when trying to get unsafe raw data from a input field with valid data. |
ezcInputFormFieldNotFoundException |
when trying to get data from a property that does not exist. |
getValidProperties
array(string) getValidProperties(
)
Returns a list with all properties that have valid data.
hasGetData
bool hasGetData(
)
Returns whether there is GET data available
hasInputField
bool hasInputField(
string
$fieldName )
Returns whether the optional field $fieldName exists.
Parameters
| Name |
Type |
Description |
$fieldName |
string |
|
hasPostData
bool hasPostData(
)
Returns whether there is POST data available
hasValidData
bool hasValidData(
string
$fieldName )
Returns whether the filters for required field $fieldName returned valid data.
Parameters
| Name |
Type |
Description |
$fieldName |
string |
|
validateDefinition
bool validateDefinition(
array
$definition )
Validates the definition array $definition.
Before reading the values from the input source, the definition array can be validated by this function to check whether all necessary elements are correctly formed.
Parameters
| Name |
Type |
Description |
$definition |
array |
|
__get
mixed __get(
string
$propertyName )
This function is called when a variable is assigned to a magic property.
When the value of a property is requested this function checks with the $properties array whether it contains valid data or not. If there is no valid data, the UserInputInValidData exception is thrown, otherwise the function returns the value associated with the input variable.
Parameters
| Name |
Type |
Description |
$propertyName |
string |
|
Throws
| Class | Description |
ezcInputFormInvalidDataException |
when trying to read a property which has no valid data. |
ezcInputFormUnknownFieldException |
when a property is being accessed which is not defined in the definition array. |
__isset
bool __isset(
string
$propertyName )
Returns whether a magic property was is used on a magic property.
This method checks whether a magic property exists and returns true of it does and false if it doesn't. The list of properties which exist is determined by the $definition array that was passed to the constructor.
Parameters
| Name |
Type |
Description |
$propertyName |
string |
|
__set
void __set(
string
$propertyName, mixed
$newValue )
Sets a new magic property.
This function is called when one of the magic properties was assigned a new value. As all magic properties are readonly for this class, all that this function does is return the exception ezcBasePropertyReadOnlyException.
Parameters
| Name |
Type |
Description |
$propertyName |
string |
|
$newValue |
mixed |
|
Throws
| Class | Description |
ezcBasePropertyPermissionException |
for every call to this function. |
Last updated: Fri, 02 Nov 2007