rougin/valla 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

rougin/valla

Composer 安装命令:

composer require rougin/valla

包简介

A simple validation package in PHP.

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Total Downloads

A simple validation package based on Valitron.

use Rougin\Valla\Check;

class UserCheck extends Check
{
    protected $labels = array(
        'name' => 'Name',
        'email' => 'Email',
    );

    protected $rules = array(
        'name' => 'required',
        'email' => 'required|email',
    );
}

Installation

Install the package using Composer:

$ composer require rougin/valla

Basic usage

The core of Valla is the Check class which is used to create a set of validation rules:

use Rougin\Valla\Check;

class UserCheck extends Check
{
    /**
     * @var array<string, string>
     */
    protected $labels =
    [
        'age' => 'Age',
        'email' => 'Email',
        'name' => 'Name',
    ];

    /**
     * @var array<string, string>
     */
    protected $rules =
    [
        'age' => 'required|numeric',
        'email' => 'required|email',
        'name' => 'required',
    ];
}

The $labels property defines user-friendly names for the fields, which will be used in error messages:

use Rougin\Valla\Check;

class UserCheck extends Check
{
    /**
     * @var array<string, string>
     */
    protected $labels =
    [
        'age' => 'Age',
        'email' => 'Email',
        'name' => 'Name',
    ];

    // ...
}

While the $rules property specifies the validation rules for each field:

use Rougin\Valla\Check;

class UserCheck extends Check
{
    // ...

    /**
     * @var array<string, string>
     */
    protected $rules =
    [
        'age' => 'required|numeric',
        'email' => 'required|email',
        'name' => 'required',
    ];
}

Note

A list of available rules can be found in the Valitron documentation.

Once the Check class is created, it can be used to validate an array of data, such as data from a HTTP request:

$check = new UserCheck;

$data = /* e.g., data from a request */;

if (! $check->valid($data))
{
    // Get all available errors
    $errors = $check->errors();

    // Or get only the first error
    echo $check->firstError();

    return;
}

// Data has passed validation

Labels, rules

For more complex scenarios, the labels and rules methods can be overridden to define them dynamically:

use Rougin\Valla\Check;

class UserCheck extends Check
{
    /**
     * Returns the specified labels.
     *
     * @return array<string, string>
     */
    public function labels()
    {
        $this->labels['is_company'] = 'Is a Company?';

        return $this->labels;
    }

    /**
     * Returns the specified rules based on the data.
     *
     * @param array<string, mixed> $data
     *
     * @return array<string, string>
     */
    public function rules($data)
    {
        if (array_key_exists('is_company', $data))
        {
            $this->rules['company_name'] = 'required';
        }

        return $this->rules;
    }
}

Using PSR-7 requests

If using ServerRequestInterface from PSR-7, the Request class provides a convenient way to validate request data:

use Rougin\Valla\Request;

class UserCheck extends Request
{
    /**
     * @var array<string, string>
     */
    protected $aliases =
    [
        'username' => 'name',
        'email_add' => 'email',
        'new_age' => 'age',
    ];

    // ...
}

The Request class provides two methods for validation: isParamsValid for validating query parameters and isParsedValid for validating the parsed body:

$check = new UserCheck;

// Should return the ServerRequestInterface ---
$request = Http::getServerRequest();
// --------------------------------------------

// Checks against data from "getQueryParams" ---
if ($check->isParamsValid($request))
{
    // Query parameters are valid
}
// ---------------------------------------------

// Checks against data from "getParsedBody" ---
if ($check->isParsedValid($request))
{
    // Parsed body is valid
}
// --------------------------------------------

When an alias is specified, it will be used to look for the field in the ServerRequestInterface data. For example, if the request data contains a username field, it will be validated against the rules defined for the name field.

Overriding the valid method

When extending the Request class and overriding the valid method, the setAlias method must be called to apply the defined aliases:

use Rougin\Valla\Request;

class UserCheck extends Request
{
    // ...

    public function valid($data)
    {
        // Always include this if aliases are defined ---
        $data = $this->setAlias($data);
        // ----------------------------------------------

        $valid = parent::valid($data);

        if (! $valid)
        {
            return count($this->errors) === 0;
        }

        // Add extra custom validation conditions here

        return count($this->errors) === 0;
    }
}

Changelog

Please see CHANGELOG for more recent changes.

Contributing

See CONTRIBUTING on how to contribute to the project.

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-26