承接 hasan-22/form-validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hasan-22/form-validator

最新稳定版本:v1.0.0

Composer 安装命令:

composer require hasan-22/form-validator

包简介

Form data validation

README 文档

README

Form data validator

install

composer require hasan-22/form-validator

usage

require_once __DIR__ . '/vendor/autoload.php';

$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
        ],
        'age'=>[
            'rules'=>['Required', 'Numeric'],
        ]
    ],$data);
    
print_r($result);

// Output: 
// [
//     [0] => [
//         [age] => The field `age` is required
//     ],
//     [1] => [
//         [age] => The field `age` must be numeric
//     ]
// ]

// If all validation passes, you get an empty array

with custom message

require_once __DIR__ . '/vendor/autoload.php';

$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
            'messages'=>['custom required msg']
        ],
        'age'=>[
            'rules'=>['Required', 'Numeric'],
            'messages'=>['custom required msg','custom numeric msg']
        ]
    ],$data);
    
// Output: 
// [
//     [0] => [
//         [age] => custom required msg
//     ],
//     [1] => [
//         [age] => custom numeric msg
//     ]
// ]


// If all validation passes, you get an empty array
 

If you want to add custom validation, do so

    class Email implements \Validation\ValidationInterface{
    
        private array $formData;

        private array $errorMessage;
    
        private string $field;
    
        private string $additional;
    
        public function validate(){
            if(!filter_var($this->formData[$this->field], FILTER_VALIDATE_EMAIL)){
                return $this->errorMessage;
            }
        }
        
         public function formData(array $formData){
            $this->formData = $formData;
        }
        
        public function message(string $errorMessage){
            $this->errorMessage = [$this->field => empty($errorMessage) ? "The `{$this->field}` field is not an email" : $errorMessage];
        }
    
        public function field(string $field){
            $this->field = $field;
        }
        
        public function additionalData(string $additional)
        {
            $this->additional = $additional;
        }
    } 
    

Now you can use the Email class like this

require_once __DIR__ . '/vendor/autoload.php';
require_once 'Email.php';

$formData = ['email'=>'armia@gmail.com'];
$result = \Validation\Validator::validate(
    [
        'email'=>[
            'rules'=>['Required','Email']
        ],
    ],$data);

//Output: 
//[] 
//If all validation passes, you get an empty array

Regex:pattern

The field under validation must match the given regular expression.

$data = ['password'=>'123456','age'=>''];
$result = \Validation\Validator::validate(
    [
        'password'=>[
            'rules'=>['Regex:/[1-6]/']
        ]
    ],$data);

Available validations

Validtion Description Example
Required Checks that the field is not empty, these items are considered empty [ '', 0, null ]
Between Checks if the field is between two values Between:4,10
Email Checks if the field is email
In Checks if the field has a series of values or not In:admin,customer,vendor
Max Checks the field must be less than or equal to a value Max:20
Min Checks that the field must be greater than or equal to a value Min:7
Numeric Checks that the field must be a number
Str Checks that the field must be a string
Regex The field under validation must match the given regular expression Regex:/pattern/

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固