Validation
Framework validate data from form. Controller has Validater. You
can validate easily by using Validater to Model. Way of validation is next two
ways.
- Validate some data automatically by chaining Model and Validater.
- Validate one data.
Chain Validater to Model.
Framework validate automatically if you specify array $validatefunc
of Model. For example, define $validatefunc as follows
user.php |
<?php
class CUser extends CModel
{
var $validatefunc = array( "name" => "notempty",
"email" => "email",
"age" => array("notempty","number") );
}
?>
|
Specify field name to key and function name to value. Each function is called
by calling function 'validate'. Validation function to specify is functions
like 'function( $data, $errmsg ="" )'. You can also add function of
this format. If you specify validation functions with array, functions specified
is called sequentially.
Next, call function of Model 'validate'. It's tutorial.
regist.html |
<form method="post" action="regist.php">
NAME<br>
<input type="text" name="user/name"><br>
EMAIL<br>
<input type="text" name="user/email"><br>
AGE<br>
<input type="text" name="user/age"><br>
</form>
|
regist.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
if( count( $_POST ) )
{
if( $c->ic_user->validate( $c->data["user"] ) )
{
//Succeeded to validate
$c->ic_user->insert( $c->data["user"] );
}
else
{
$c->set( "err", "invalidte!" );
}
}
}
?>
|
You can get error message by specifying array $validatemsg of
Model.
For example, string 'Input name.<br>' is returned if 'name'
validatin is failed. string 'Input name.<br>Input number to age.<br>'
is returned if 'name' and 'age' validation is failed.
user.php |
<?php
class CUser extends CModel
{
var $validatefunc = array( "name" => "notempty",
"email" => "email",
"age" => array("notempty","number") );
var $validatemsg = array( "name" => "Input name.<br>",
"email" => "Input right email.<br>",
"age" => array(
"Input age.<br>",
"Input number to age.<br>") );
?>
|
regist.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
if( count( $_POST ) )
{
$err = $c->ic_user->validatemsg( $c->data["user"] );
if( $err == "" )
{
//Succeeded to validate
$c->ic_user->insert( $c->data["user"] );
}
else
{
$c->set( "err", $err );
}
}
}
?>
|
You can get array of error message by GetValidate Error if you called validate.
$c->ic_user->validate( $c->data["user"] );
$err = $c->ic_user->GetValidateError();
$c->set( "nameerror", $err["name"] );
|
Validate one value.
Call Validater directory from Controller. Way of calling is next
two ways.
$c->validate->function();
$c->v->function();
|
You can specify $errmsg like notempty( $data, $errmsg = "" ) in validation
function. If you don't specify $errmsg, function returns TRUE or FALSE. If you
specify $errmsg, function return empty string when validation is succeeded and
return $errmsg when validation is not succeeded.
When you don't specify $errmsg |
if( $c->v->notempty( $name ) )
{
$c->set( "msg", "OK!" );
}
else
{
$c->set( "msg", "Please input your name." );
}
|
When you specify $errmsg |
$err = "";
$err .= $c->v->notempty( $name, "Please input your name." );
$err .= $c->v->email( $email, "Please input right email." );
if( $err == "" )
{
Regist( $name, $email )
}
$c->set( $err );
|
Function
- mixed notempty( mixed data [, string errmsg ] )
Validate if data is empty
- mixed len( string data, int min, int max [, string errmsg ] )
Validate if min <= length of data <= max. ( You can't specify this to
$validatefunc )
- mixed number( mixed data [, string errmsg ] )
Validate if data is number.
- mixed eisu( string data [, string errmsg ] )
Validate if data consisted by only number and English.
- mixed email( string data [, string errmsg ] )
Validate if data is email format.
|