codeonyii/yii2-at-least-validator
Composer 安装命令:
composer require codeonyii/yii2-at-least-validator
包简介
Validates at least one (or more) attributes.
README 文档
README
Sometimes, in a set of fields, you need to make at least one of them (sometimes two, or more) be filled. For example, phone OR e-mail, (facebook OR linkedin) OR (linkedin OR instagram) and so on. You can do it using required validator with a bunch of conditional rules. Or you can use AtLeastValidator.
Installation
Use composer:
composer require "codeonyii/yii2-at-least-validator"
In your Model, import the validator:
use codeonyii\yii2validators\AtLeastValidator; class MyModel extends Model { ... public function rules() { // see examples below } ...
Examples
In the following example, the phone and email attributes will
be verified. If none of them are filled phone will receive an error.
Please, note that in param is always mandatory.
// in rules() return [ ['phone', AtLeastValidator::className(), 'in' => ['phone', 'email']], ];
Here, facebook, linkedin and instagram attributes will
be verified. If at least 2 (note the min param) of them are not filled,
facebook and instagram will receive an error:
// in rules() return [ [['facebook', 'instagram'], AtLeastValidator::className(), 'in' => ['facebook', 'linkedin', 'instagram'], 'min' => 2], ];
Showing errors in summary
If you want to show errors in a summary instead in the own attributes, you can do this:
Note that summary will not work for client-side validation. If you want to use it, you should disable the client-side validation for your fields or for your entire form.
// in the rules() // please note the exclamation mark. It will avoid the pk attribute to be massively assigned. return [ ['!id', AtLeastValidator::className(), 'in' => ['attr1', 'attr2', 'attr3']], // where `id` is the pk ]; // in the view, show all errors in the summary: ... echo yii\helpers\Html::errorSummary($model, ['class' => ['text-danger']]); // OR, to show only `id` errors: echo yii\helpers\Html::error($model, 'id', ['class' => ['text-danger']]);
Changelog
Please, access Releases to see versions with a some description.
codeonyii/yii2-at-least-validator 适用场景与选型建议
codeonyii/yii2-at-least-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 261.85k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2016 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「validation」 「yii2」 「at least」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codeonyii/yii2-at-least-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codeonyii/yii2-at-least-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codeonyii/yii2-at-least-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
A custom URL rule class for Yii 2 which allows to create translated URL rules
Extension for Opis JSON Schema
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
统计信息
- 总下载量: 261.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-05-22