百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
V

validation-composite

> 后端框架
开源

允许将多个验证规则合并为单一规则,以便于重新使用

180 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

允许将多个验证规则合并为单一规则,以便于重新使用

Laravel Composite Validation

This extension allows uniting several Laravel validation rules into a single one for easy re-usage.

For license information check the LICENSE-file. Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist illuminatech/validation-composite

or add

json
"illuminatech/validation-composite": "*"

to the require section of your composer.json.

Usage

The same sequence of the validation rules may repeat over the application many times. For example: you may have a set of restrictions related to the user's password, like it should be at least 8 symbols long, but shorter then 200 to fit the database field reserved for its storage. Your program may also allow user to upload an image to be his avatar, but in order to make it safe, you should validate uploaded file mime type and size. Thus validation for the user profile form may looks like following:

php
validate([
            'password' => ['required', 'string', 'min:8', 'max:200'],
            'avatar' => ['required', 'file', 'mimes:png,jpg,jpeg', 'max:1024'],
            // ...
        ]);
        
        // ...
    }
}

The problem is: validation for user's password or avatar may appear in several different places. For example: password can be setup at sign-up process, during password reset and so on. You may also have a separated administration panel, which allows system administrator to adjust existing user's record or create a new one. Thus you will have to duplicate all these validation rules many times throughout your project source code. In case requirements change, for example: we decide that password length should be at least 10 symbols instead of 8, or disallow '*.png' files from avatar - you'll have to manually changes validation rules at all those places.

This extension allows uniting several validation rules into a single one for easy re-usage. For the example above you should create 2 separated validation rule classes extending Illuminatech\Validation\Composite\CompositeRule:

php
validate([
            'password' => ['required', new PasswordRule],
            'avatar' => ['required', new AvatarRule],
            // ...
        ]);
        
        // ...
    }
}

With such approach you can change validation for the 'password' and 'avatar' at the single place.

In case composite validation rule fails, validator instance will pick up an error message from the particular sub-rule. For example:

php
 'short'],
    [
        'password' => ['required', new PasswordRule],
    ]
);

if ($validator->fails()) {
    echo $validator->errors()->first('password'); // outputs 'The password must be at least 8 characters.'
}

Note: do not use rules like 'sometimes', 'required', 'required_with', 'required_without' and so on in the composite rule. These are processed at the different validation level and thus will have no effect or may behave unexpectedly.

You may define composite validation rules using validation factory extensions feature. For such case you may use Illuminatech\Validation\Composite\DynamicCompositeRule. For example:

php
app->extend('validator', function (Factory $validatorFactory) {
            $validatorFactory->extend('password', function ($attribute, $value) {
                return (new DynamicCompositeRule(['string', 'min:8', 'max:200']))->passes($attribute, $value);
            });
            
            $validatorFactory->extend('avatar', function ($attribute, $value) {
                return (new DynamicCompositeRule(['file', 'mimes:png,jpg,jpeg', 'max:1024']))->passes($attribute, $value);
            });

            return $validatorFactory;
        });
        
        // ...
    }
}

Note that with such approach automatic pick up of the validation error message becomes impossible, and you will have to setup it explicitly in language files.

You may specify custom error messages per each validation rule used in the composite one, overriding messages() method. For example:

php
 'Only string is allowed.',
            'min' => ':attribute is too short.',
            'max' => ':attribute is too long.',
        ];
    }
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPcombinecompositelaravelrule

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架