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.
Controller
<?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.