1. Go to this page and download the library: Download shonans/php-validate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shonans / php-validate example snippets
[
['tagId,userId,name,email,freeTime', '
[
['field', '
use Shonans\Validate\Validation;
class SomeController
{
public function demoAction()
{
$v = Validation::check($_POST,[
// add rule
['title', 'min', 40],
['freeTime', 'number'],
]);
if ($v->isFail()) {
var_dump($v->getErrors());
var_dump($v->firstError());
}
// $postData = $v->all(); // 原始数据
$safeData = $v->getSafeData(); // 验证通过的安全数据
$db->save($safeData);
}
}
class AdemoValidator extends \Shonan\Validate\Validator\AbstractValidator
{
public function validate($value, $data): bool
{
if ($value == 1) {
return true;
}
return false;
}
}
// 使用
['status', new AdemoValidator()],
use Shonans\Validate\Validation;
class PageValidation extends Validation
{
# 进行验证前处理,返回false则停止验证,但没有错误信息,可以在逻辑中调用 addError 增加错误信息
public function beforeValidate(): bool
{
return true;
}
# 进行验证后处理,该干啥干啥
public function afterValidate(): bool
{
return true;
}
}
public function val(string $key, $default = null) // getSafe() 的别名方法
public function getValid(string $key, $default = null) // getSafe() 的别名方法
public function getSafe(string $key, $default = null)
public function all(): array
public function getRaw(string $key, $default = null)
bash
composer
// 验证失败
public function isFail()
public function hasError() // isFail() 的别名方法
// 成功通过验证
public function isOk()
public function isPassed()
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.