Class yii\mongodb\validators\MongoIdValidator

Inheritanceyii\mongodb\validators\MongoIdValidator » yii\validators\Validator
Available since extension's version2.0.4
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/src/validators/MongoIdValidator.php

MongoIdValidator verifies if the attribute is a valid Mongo ID.

Attribute will be considered as valid, if it is an instance of \MongoId or a its string value.

Usage example:

class Customer extends yii\mongodb\ActiveRecord
{
    ...
    public function rules()
    {
        return [
            ['_id', 'yii\mongodb\validators\MongoIdValidator']
        ];
    }
}

This validator may also serve as a filter, allowing conversion of Mongo ID value either to the plain string or to \MongoId instance. You can enable this feature via $forceFormat.

Public Properties

Hide inherited properties

Property Type Description Defined By
$forceFormat string|null Specifies the format, which validated attribute value should be converted to in case validation was successful. yii\mongodb\validators\MongoIdValidator

Protected Methods

Hide inherited methods

Method Description Defined By
validateValue() yii\mongodb\validators\MongoIdValidator

Property Details

Hide inherited properties

$forceFormat public property

Specifies the format, which validated attribute value should be converted to in case validation was successful. valid values are:

  • 'string' - enforce value converted to plain string.
  • 'object' - enforce value converted to \MongoId instance. If not set - no conversion will be performed, leaving attribute value intact.
public string|null $forceFormat null

Method Details

Hide inherited methods

init() public method

public void init ( )

                public function init()
{
    parent::init();
    if ($this->message === null) {
        $this->message = Yii::t('yii', '{attribute} is invalid.');
    }
}

            
validateAttribute() public method

public void validateAttribute ( $model, $attribute )
$model
$attribute

                public function validateAttribute($model, $attribute)
{
    $value = $model->$attribute;
    $mongoId = $this->parseMongoId($value);
    if (is_object($mongoId)) {
        if ($this->forceFormat !== null) {
            switch ($this->forceFormat) {
                case 'string' : {
                    $model->$attribute = $mongoId->__toString();
                    break;
                }
                case 'object' : {
                    $model->$attribute = $mongoId;
                    break;
                }
                default: {
                    throw new InvalidConfigException("Unrecognized format '{$this->forceFormat}'");
                }
            }
        }
    } else {
        $this->addError($model, $attribute, $this->message, []);
    }
}

            
validateValue() protected method

protected void validateValue ( $value )
$value

                protected function validateValue($value)
{
    return is_object($this->parseMongoId($value)) ? null : [$this->message, []];
}