All Projects → sdlins → yii2-at-least-validator

sdlins / yii2-at-least-validator

Licence: Apache-2.0 license
Makes one or more attributes mandatory inside a set of attributes.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-at-least-validator

angular-yii2-model
AngularJS 1.x service to consume Yii2 RESTful API framework
Stars: ✭ 13 (-53.57%)
Mutual labels:  yii2
yii2-inertia
The Yii 2 server-side adapter for Inertia.js.
Stars: ✭ 52 (+85.71%)
Mutual labels:  yii2
kontrolio
Simple standalone data validation library inspired by Laravel and Symfony
Stars: ✭ 51 (+82.14%)
Mutual labels:  validator
guice-validator
Guice javax.validation method validation integration
Stars: ✭ 35 (+25%)
Mutual labels:  validator
cron-validate
A cron-expression validator for TypeScript/JavaScript projects.
Stars: ✭ 40 (+42.86%)
Mutual labels:  validator
gulp-html
Gulp plugin for HTML validation, using the official Nu Html Checker (v.Nu)
Stars: ✭ 70 (+150%)
Mutual labels:  validator
rust-phonenumber
Library for parsing, formatting and validating international phone numbers.
Stars: ✭ 99 (+253.57%)
Mutual labels:  validator
luya-kickstarter-bootstrap4
LUYA boilerplate to kickstart projects with Bootstrap 4.
Stars: ✭ 13 (-53.57%)
Mutual labels:  yii2
garn-validator
Create validations with ease
Stars: ✭ 42 (+50%)
Mutual labels:  validator
yii2-render-many
Trait for Yii Framework 2
Stars: ✭ 14 (-50%)
Mutual labels:  yii2
ngx-translate-lint
Simple CLI tools for check `ngx-translate` keys
Stars: ✭ 25 (-10.71%)
Mutual labels:  validator
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-35.71%)
Mutual labels:  validator
yii2-newsletter
Module for saving user contacts from newsletter form to database
Stars: ✭ 17 (-39.29%)
Mutual labels:  yii2
yii2-s
🚀speed up yii2 tcp server、 restful by swoole
Stars: ✭ 34 (+21.43%)
Mutual labels:  yii2
yii2-config-kit
Configuration kit for Yii applications
Stars: ✭ 24 (-14.29%)
Mutual labels:  yii2
yii2-symfonymailer
Yii 2 Symfony mailer extension.
Stars: ✭ 29 (+3.57%)
Mutual labels:  yii2
yii2-notifications
This Yii2 extension provides support for sending notifications across a variety of delivery channels, including mail, SMS, Slack, Telegram etc.
Stars: ✭ 62 (+121.43%)
Mutual labels:  yii2
ATGValidator
iOS validation framework with form validation support
Stars: ✭ 51 (+82.14%)
Mutual labels:  validator
validation
Developer experience focused validator.
Stars: ✭ 15 (-46.43%)
Mutual labels:  validator
djburger
Framework for safe and maintainable web-projects.
Stars: ✭ 75 (+167.86%)
Mutual labels:  validator

Yii 2 - AtLeastValidator

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality CodeFactor

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].