vadymsemeniuk / yii2-image-require-validator
Behavior for validating required image
Installs: 679
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-11-09 20:39:23 UTC
README
Yii2 Image require validator
Extension for validating image, throw EntityToFile model
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist vadymsemeniuk/yii2-image-require-validator "*"
or add
"vadymsemeniuk/yii2-image-require-validator": "*"
to the require section of your composer.json
file.
Usage
An example of usage could be:
Your code must look like this
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:
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 ], ]; } }