|
|
|
|
|
|
CatchErrors |
CatchErrors (
) |
This function is still beta!
Arguments:
Name |
Description |
$display |
Should there still be error messages displayed behind the invalid fields? |
Description:
Get the error messages of the invalid fields in the form.
The error messages are returned in array format where the array keys are the names of the fields.
This method has one argument: display the error messages in the form or not? Set display to false (default), and the error messages will not be displayed in the form (so you have to handle that yourself!). If set to true, the error messages will be displayed in the form.
KNOWN ERROR. Fetching the title with getTitle like in the example below does not work correctly when this code is still in the flush method:
<?php
// remove all vars to free memory
$vars = get_object_vars($this);
foreach( $vars as $name => $value )
{
// remove all vars except these..
if( !in_array($name, array( '_cache', 'edit', 'insert', '_posted', '_name' ) ) )
{
unset( $this->{$name} );
}
}
?>
|
Example
<?php
// include the class
include("FH3/class.FormHandler.php");
// create new formhandler object
$form = new FormHandler();
// textfield
$form -> textField("Name", "name", FH_STRING);
// submitbutton
$form -> submitButton("Save");
// get the errors of invalid fields
$errors = $form->catchErrors();
// oncorrect function...
$form -> onCorrect('doRun');
// flush
$form -> flush();
/** handle your own errors! **/
// any errors?
if( sizeof($errors) > 0 )
{
// create a JS message
$msg = "Some fields are incorrect!\\n";
foreach($errors as $field => $error)
{
$msg .= "- ". $form -> getTitle( $field )."\\n";
}
echo
"<script language='javascript'>\n".
"alert('".$msg."');\n".
"</script>\n";
}
function doRun($data)
{
echo "Hi ". $data['name'];
}
?>
|
Preview image
Latest change: 05 March 10 / 08:48
Comments
|
|
|
|