1. Go to this page and download the library: Download rrzu/validation 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/ */
rrzu / validation example snippets
class DemoValidator extends AbstractValidator
{
public $language = 'en';
public function rulesWhenCreate(): array
{
return [
'rules' => [
'name' => '' => '
class DemoValidator extends AbstractValidator
{
public function rulesWhenCreate(): array
{
return [
'rules' => [
'username' => ' 'username' => 'rray
{
return [
'
class DemoValidator extends AbstractValidator
{
public function rulesWhenCreate(): array
{
// 局部 messages 和 attributes
return [
'rules' => [
'username' => '商家名称',
],
];
}
public function rulesWhenUpdate()
{
// 局部 messages 和 attributes
return [
'rules' => [
'username' => 'red' => '该:attribute必填',
];
}
/**
* 默认自定义属性名称 【全局】
*
* @return array
*/
public function attributes(): array
{
return [
'username' => '用户名',
];
}
}
class DemoController extends Controller
{
public function actionCreate()
{
$param = Yii::$app->request->post();
DemoValidator::create()->validate('create', $param);
(new DemoService())->create($param);
// Proceed with user creation logic
return $this->asJson(['success' => true]);
}
public function actionUpdate()
{
$param = Yii::$app->request->post();
DemoValidator::create()->validate('update', $param);
(new DemoService())->update($param);
// Proceed with user creation logic
return Response::successMsg(['success' => true]);
}
}
public function actionUpdate()
{
$param = Yii::$app->request->post();
DemoValidator::create()->validate('update', $param);
(new DemoService())->update($param);
// Proceed with user creation logic
return Response::successMsg(['success' => true]);
}
public function actionUpdate()
{
$param = Yii::$app->request->post();
DemoValidator::create()->validate('update', $param, false);
if ($validate->getValidator()->fails()) {
return Response::error(422, $validate->getValidator()->firstErrorMessage());
}
(new DemoService())->update($param);
// Proceed with user creation logic
return Response::successMsg(['success' => true]);
}
public function rulesWhenCreate()
{
return [
'rules' => [
'even_number' => ['(is_numeric($value) AND $value % 2 === 0);
}]
],
];
}
public function rulesWhenCreate()
{
return [
'rules' => [
'even_number' => [
'ribute must be numeric.";
}
if ($value % 2 !== 0) {
return ":attribute is not even number.";
}
// 如果值有效,可以返回 true 或不返回任何内容
}
]
],
];
}
'id'=>'nullable|integer|min:1',
text
public function rulesWhenCreate(): array
{
return [
'rules' => [
'password' => '
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.