2017 © Pedro Peláez
 

yii2-extension yii2-image-require-validator

Behavior for validating required image

image

vadymsemeniuk/yii2-image-require-validator

Behavior for validating required image

  • Wednesday, March 15, 2017
  • by Vadym Semeniuk
  • Repository
  • 1 Watchers
  • 0 Stars
  • 323 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 21 % Grown

The README.md

Total Downloads Latest Stable Version Dependency Status, (*1)

Yii2 Image require validator

Extension for validating image, throw EntityToFile model, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

php composer.phar require --prefer-dist vadymsemeniuk/yii2-image-require-validator "*"

or add, (*5)

"vadymsemeniuk/yii2-image-require-validator": "*"

to the require section of your composer.json file., (*6)

Usage

An example of usage could be:, (*7)

Your code must look like this, (*8)

use vadymsemenykv\imageRequireValidator\ImageRequireValidator

/**
 * @property EntityToFile $image
 */
class Article extends ActiveRecord {
    public $titleImage;

    public function getImage()
    {
        return $this->hasOne(EntityToFile::className(), ['entity_model_id' => 'id'])
            ->andOnCondition(['t2.entity_model_name' => static::formName(), 't2.attribute' => EntityToFile::TYPE_ARTICLE_TITLE_IMAGE])
            ->from(['t2' => EntityToFile::tableName()])
            ->orderBy('t2.position DESC');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['titleImage'],
                ImageRequireValidator::className(),
                'errorMessage' => 'Title image cannot be blank.',
                'imageRelation' => 'image',
                'skipOnEmpty' => false
            ],
        ];
    }   
}

If you save in relation many images, and also want to validate min or|and max num of images your code must look like this:, (*9)

use vadymsemenykv\imageRequireValidator\ImageRequireValidator

/**
 * @property EntityToFile $images
 */
class Article extends ActiveRecord {
    public $titleImages;

    public function getImages()
    {
        return $this->hasMany(EntityToFile::className(), ['entity_model_id' => 'id'])
            ->andOnCondition(['t2.entity_model_name' => static::formName(), 't2.attribute' => EntityToFile::TYPE_ARTICLE_TITLE_IMAGE])
            ->from(['t2' => EntityToFile::tableName()])
            ->orderBy('t2.position DESC');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['titleImages'],
                ImageRequireValidator::className(),
                'errorMessage' => 'Title images cannot be blank.',
                'validateNum' => true,
                'errorNumMinMessage' => 'Title images count should not be less than 3',
                'errorNumMaxMessage' => 'Title images count should not be more than 6',
                'minNumOfImages' => 3,
                'maxNumOfImages' => 6,
                'imageRelation' => 'images',
                'skipOnEmpty' => false
            ],
        ];
    }   
}

The Versions

15/03 2017

dev-master

9999999-dev

Behavior for validating required image

  Sources   Download

MIT

The Requires

 

by Avatar Vadym Semeniuk

extension yii2 require image require validator image min count validator image max count validator