Login Retreive lost passwordRegister
Search

Manual  /  Functions  /  Data handling  /  OnCorrect

OnCorrect
OnCorrect (
mixed $validator = null 
)

Arguments:

Name Description
$validator The name of the function which should be called when the form is correct. It is also possible to use a method. In this case you should pass an array where the first item is the object and the second item the name of the method.

Description:

Set the function which has to be called when the form is correct. Your function will get 2 arguments from FormHandler:

  1. An array of the data which is saved. The keys of this array represents the field names of the form.

  2. A reverence to the FormHandler object. This can be used to make some changes, like check a field and set it to invalid



Note: The second argument which is passed to your oncorrect function is added on 01-04-2006. Before then, only argument 1 was passed trough to your function.

You can also use a method which has to be called when the form is correct. You can do this like shown in example 2.

It is also possible to validate some fields in your oncorrect function. This is shown in example 3.

When you return false in the onCorrect function, the form is shown again. When returning true or null, the form will not be displayed again. When returning a string, the string will be displayed.

Example

<?php

// include the class
include("FH3/class.FormHandler.php");

// create new formhandler object
$form = new FormHandler("myForm");

// simple textfield + submitbutton
$form -> textField    ("Name""name"FH_STRING);
$form -> submitbutton ("Save");

// set the commit after correct function
$form -> onCorrect    ("doRun");

// flush the form
$form -> flush();

// the oncorrect function
// make sure you add a & before the second argument!!!
function doRun$data, &$form 
{
    echo 
"Hello! Your name is " $data["name"];
}

?>

The result:
resizeIMG


Example 2: Calling a method
<?php

// include the class
include("FH3/class.FormHandler.php");

/**** Simple example class!! *******/
class Example 
{
    
// this is the method we are going to call
    // when the form is correct!
    
function doRun$data, &$form 
    {
        echo 
"Hello! Your name is " $data["name"];
    }
}

    
// create a new example object
// for usage in the onCorrect call!
$example = new Example();

// create new formhandler object
$form = new FormHandler("myForm");

// simple textfield + submitbutton
$form -> textField    "Name""name"FH_STRING );
$form -> submitbutton "Save" );

// set the method which we should call when the form is saved
$form->onCorrect(
  
// note: the & caracter is needed!
  
array(&$example"doRun")
);

// flush the form
$form -> flush();

?>

The result is the same as the example above.

Example 3: Validating
Possible since FH3 v1.2.1 (01-04-2006)

<?php

// include the class
include("FH3/class.FormHandler.php");

// create new formhandler object
$form = new FormHandler("myForm");

// simple textfield + submitbutton
$form -> textField    ("Name""name"FH_STRING);
$form -> submitbutton ("Save");

// set the commit after correct function
$form -> onCorrect    ("doRun");

// flush the form
$form -> flush();

// the oncorrect function
function doRun$data, &$form 
{
    
// is the name shorter then 3 characters ?
    
if( strlen$data['name'] ) < )
    {
        
// set an error message for the name field
        
$form -> setError(
          
'name'
          
'Your name has to be at least 3 characters!'
        
);

        
// display the form again
        
return false;
    }
    else
    {
        echo 
"Hello! Your name is " $data["name"];
    }
}

?>

The result:
resizeIMG

Latest change: 05 March 10 / 08:53

Comments

powered by PHP-GLOBE   © 2004 - 2024 FormHandler. All rights reserved.   -   Open source license