Introduction
Providing some functions for validation
How to use
Import Fegg\Validation and create it with new. Call a check method for each field, then read the result with Validation::getErrorMessage.
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
use Fegg\Validation;
class Contact extends Controller
{
public function confirm()
{
$validation = new Validation();
$id = 'name';
$name = 'お名前';
$validation->required($id, $this->in($id), array('@required', $name));
$validation->maxlength($id, $this->in($id), 50, array('@maxlength', $name, 50));
$id = 'mail';
$name = 'メールアドレス';
$validation->required($id, $this->in($id), array('@required', $name));
$validation->email($id, $this->in($id), array('@email', $name));
$errorMessage = $validation->getErrorMessage();
if (!$errorMessage) {
$this->displayPage('contact/confirm');
} else {
$this->page['error'] = $errorMessage;
$this->displayPage('contact/input');
}
}
}
How checks work
- Every check method takes the field name
$namefirst. It is the key of the error message returned by Validation::getErrorMessage. - Only the first error of each field is kept: once a field has an error, the next checks of the same field are skipped and return
false. Write the checks in order of priority (e.g.requiredbeforeemail). - Except Validation::required and Validation::upload, the checks do not check an empty value (they return
true). Userequiredfor required fields. $codedecides the error message, see Configuration.
Methods
Validation::__construct
Create the validator and load the error messages
Validation::required
Check required
Validation::alphameric
Check half-width alphanumeric characters
Validation::numeric
Check numeric or not
Validation::date
Check date format
Validation::email
Check email address format
Validation::tel
Check telephone number format
Validation::time
Check time format
Validation::url
Check URL format
Validation::zipcode
Check Japanese postal code format
Validation::userid
Check user ID format
Validation::password
Check password format
Validation::maxbyte
Check maximum byte length
Validation::length
Check exact character length
Validation::maxlength
Check maximum character length
Validation::minlength
Check minimum character length
Validation::hankaku
Check half-width characters
Validation::katakana
Check Katakana characters for Japanese language
Validation::upload
Check that a file was uploaded
Validation::setError
Set an error by error code
Validation::setErrorMessage
Set an error message
Validation::isError
Check whether any error was set
Validation::getErrorMessage
Get error messages