PHP code example of mix / validator

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);

public function rules(): array
{
    return [
        'a' => ['integer', 'unsigned' => true, 'min' => 1, 'max' => 1000000, 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'b' => ['double', 'unsigned' => true, 'min' => 1, 'max' => 1000000, 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'c' => ['alpha', 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'd' => ['alphaNumeric', 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'e' => ['string', 'length' => 10, 'minLength' => 3, 'maxLength' => 5, 'filter' => ['trim', 'strip_tags', 'htmlspecialchars']],
        'f' => ['email', 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'g' => ['phone'],
        'h' => ['url', 'length' => 10, 'minLength' => 3, 'maxLength' => 5],
        'i' => ['in', 'range' => ['A', 'B'], 'strict' => true],
        'j' => ['date', 'format' => 'Y-m-d'],
        'k' => ['compare', 'compareAttribute' => 'a'],
        'l' => ['match', 'pattern' => '/^[\w]{1,30}$/'],
        'm' => ['call', 'callback' => [$this, 'check']],
        'n' => ['file', 'mimes' => ['audio/mp3', 'video/mp4'], 'maxSize' => 1024 * 1],
        'r' => ['image', 'mimes' => ['image/gif', 'image/jpeg', 'image/png'], 'maxSize' => 1024 * 1],
    ];
}

public function check($fieldValue): bool
{
    // 验证代码
    // ...
    
    return true;
}

// 验证是否为字母与数字
Mix\Validator\Validate::isAlphaNumeric($value);

// 验证是否为字母
Mix\Validator\Validate::isAlpha($value); 

// 验证是否为日期
Mix\Validator\Validate::isDate($value, $format); 

// 验证是否为浮点数
Mix\Validator\Validate::isDouble($value); 

// 验证是否为邮箱
Mix\Validator\Validate::isEmail($value); 

// 验证是否为整数
Mix\Validator\Validate::isInteger($value);

// 验证是否在某个范围
Mix\Validator\Validate::in($value, $range, $strict = false); 

// 正则验证
Mix\Validator\Validate::match($value, $pattern); 

// 验证是否为手机
Mix\Validator\Validate::isPhone($value); 

// 验证是否为网址
Mix\Validator\Validate::isUrl($value);