1. Go to this page and download the library: Download mix/validator 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/ */
mix / validator example snippets
namespace App\Forms;
use Mix\Validator\Validator;
class UserForm extends Validator
{
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $age;
/**
* @var string
*/
public $email;
/**
* @return array
*/
public function rules(): array
{
return [
'name' => ['string', 'maxLength' => 25, 'filter' => ['trim']],
'age' => ['integer', 'unsigned' => true, 'min' => 1, 'max' => 120],
'email' => ['email'],
];
}
/**
* @return array
*/
public function scenarios(): array
{
return [
'create' => ['
// 使用表单验证
$form = new UserForm($request->getAttributes());
if (!$form->scenario('create')->validate()) {
$data = ['code' => 1, 'message' => $form->error()];
$ctx->JSON(200, $data);
return;
}
// 将表单对象直接传递到模型中保存数据
(new UserModel())->add($form);