PHP code example of rrzu / validation

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 rulesWhenUpdate()
  {
    return [
        'rules' => [
            'title' => 'Lorem Ipsum',
            'body' => 'Lorem ipsum dolor sit amet ...',
            'published' => null,
            'something' => '-invalid-'
        ]
      ];
  }



  $validate = $validate->validate('update', $data);

  // 获取已验证的数据
  $res = $validate->getValidatedData();
  // [
  //     'title' => 'Lorem Ipsum',
  //     'body' => 'Lorem ipsum dolor sit amet ...',
  //     'published' => '1' // notice this
  //     'something' => '-invalid-'
  // ]

  // 获取有效数据
  $res = $validate->getValidData();
  // [
  //     'title' => 'Lorem Ipsum',
  //     'body' => 'Lorem ipsum dolor sit amet ...',
  //     'published' => '1'
  // ]

  // 获取无效数据
  $res = $validate->getInvalidData();
  // [
  //     'something' => '-invalid-'
  // ]

/**
 * 默认自定义错误消息
 *
 * @return array
 */
  public function messages(): array
  {
    return [
        'eturn [
        'username' => '用户名',
      ];
  }

/**
 * 默认自定义错误消息
 *
 * @return array
 */
  public function messages(): array
  {
    return [
        'username:
 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
            'skills.*.id'           => '

// 当 is_companion 等于 是,则 companion_card 为必须,
// 当 is_companion 等于 否,则 companion_card 为非必须
'is_companion' => 'string|in:是,否',
'companion_card' => '

// 当 is_companion 等于 是,则 companion_card 为必须,
// 当 is_companion 等于 否,则 companion_card 为非必须
'is_companion' => 'string|in:是,否',
'companion_card' => '

// 当actors数组存在值时,code 必须存在
'actors' => '
 
'actors' => '' => ' '
 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
         'photos.*' => 'uploaded_file:0,2M,jpeg,png'
      ]
  }
 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
          'photos.*' => 'uploaded_file|max:2M|mimes:jpeg,png'
      ]
  }
 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
  	  'images.*' => 'uploaded_file|max:2M|mimes:jpeg,png',
      ]
  }
 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
  	     'images.profile' => 'uploaded_file|max:2M|mimes:jpeg,png',
    	     'images.cover' => 'uploaded_file|max:5M|mimes:jpeg,png',
      ]
  }

'photo' => 'mimes:jpg,bmp,png'

 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
  		'enabled' => 'default:1|date->validate('demo', $data);

// 获取有效/默认数据
$valid_data = $validate->getValidData();

$enabled = $valid_data['enabled']; // 1
$published = $valid_data['published']; // 0

 
  public function rulesWhenCreate(): array
  {
    return [
      'rules' => [
          'user' => 'array:name,username',
      ]
  }

      

$input = [
    'user' => [
        'name' => 'Taylor Otwell',
        'username' => 'taylorotwell',
        'admin' => true,
    ],
];
 
'email' => 'regex:/^.+@.+$/i',
 
'birthday' => 'date:Y-m-d',

'start_date' => '
 
'finish_date' => '
 
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' => '