承接 hsnfirdaus/class-validator 相关项目开发

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

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

hsnfirdaus/class-validator

最新稳定版本:v2.0.4

Composer 安装命令:

composer require hsnfirdaus/class-validator

包简介

Class Validator using php attribute

README 文档

README

PHP Class Validator using php 8.0 attributes.

Caution

This package no longer maintained, use symfony/validator instead.

Installation

Using Composer:

composer require "hsnfirdaus/class-validator"

Usage

Example Usage:

<?php

declare(strict_types=1);

namespace MyApp\Contract\User;

use Hsnfirdaus\ClassValidator\Attribute\IsEnum;
use Hsnfirdaus\ClassValidator\Attribute\IsNotEmpty;
use Hsnfirdaus\ClassValidator\Attribute\IsOptional;
use Hsnfirdaus\ClassValidator\Validator;
use MyApp\Entity\Enum\UserRole;
use MyApp\Entity\Enum\UserType;

class AddUserContract
{
    #[Name(name: 'User Type')]
    #[IsNotEmpty]
    #[IsEnum(enum: UserType::class)]
    public string $type = 'Can\'t be empty';

    #[IsNotEmpty]
    #[IsEnum(enum: UserRole::class)]
    public string $role = 'ValidEnumValue';

    #[IsOptional]
    public string $identifier;

    public function validate()
    {
        Validator::validate($this);
    }
}

Then call validate method, it will throw exception if found an error. Currently this package only support property attribute.

Available Attributes

Available attributes (see src/Attribute folder):

  • IsEmail() : Email validation (using php filter_var function).
  • IsEnum(enum: ExampleEnum::class) : Enum validation from string value.
  • IsInteger() : Integer validation (using php is_int function).
  • IsNotEmpty() : Validate trimmed value is not empty string or null.
  • IsNumeric(length?: number, minLength?: number, maxLength?: number) : Validate numeric string (using php is_numeric function).
  • IsOptional() : Attribute to mark the property is optional, allow to be not defined.
  • IsString(length?: number, minLength?: number, maxLength?: number) : Validate string length.
  • Name(name: string) : Set error message field name (optional, default will be the property name).
  • ValidateArrayClass(type: string) : Nested validate array of class.
  • ValidateClass() : Nested validate class.

Error Message

Currently only English and Indonesian error message will thrown (see locale). Default will be English.

To change locale you can use and setLang method in your root or boot project:

<?php

declare(strict_types=1);

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

use Hsnfirdaus\ClassValidator\Validator;

Validator::setLang('id');

// Then you can call  Validator::validate method

Or, you can use your own locale with setLangDir method. For example, create your locale file in __DIR__.'/locale/kr.php, then you can use:

Validator::setLangDir(__DIR__.'/locale');
Validator::setLang('kr');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固