Beispiel #1
0
 /**
  * Creates a validator object.
  * @param mixed $type the validator type. This can be a built-in validator name,
  * a method name of the model class, an anonymous function, or a validator class name.
  * @param \yii\base\Model $object the data object to be validated.
  * @param array|string $attributes list of attributes to be validated. This can be either an array of
  * the attribute names or a string of comma-separated attribute names.
  * @param array $params initial values to be applied to the validator properties
  * @return Validator the validator
  */
 public static function createValidator($type, $object, $attributes, $params = [])
 {
     $params['attributes'] = $attributes;
     if ($type instanceof \Closure || $object->hasMethod($type)) {
         // method-based validator
         $params['class'] = __NAMESPACE__ . '\\InlineValidator';
         $params['method'] = $type;
     } else {
         if (isset(static::$builtInValidators[$type])) {
             $type = static::$builtInValidators[$type];
         }
         if (is_array($type)) {
             foreach ($type as $name => $value) {
                 $params[$name] = $value;
             }
         } else {
             if (!class_exists($type)) {
                 throw new InvalidConfigException("Unknown validator: '{$type}'.");
             }
             $params['class'] = $type;
         }
     }
     return Yii::createObject($params);
 }
Beispiel #2
0
 /**
  * Creates a validator object.
  * @param string|\Closure $type the validator type. This can be either:
  *  * a built-in validator name listed in [[builtInValidators]];
  *  * a method name of the model class;
  *  * an anonymous function;
  *  * a validator class name.
  * @param \yii\base\Model $model the data model to be validated.
  * @param array|string $attributes list of attributes to be validated. This can be either an array of
  * the attribute names or a string of comma-separated attribute names.
  * @param array $params initial values to be applied to the validator properties.
  * @return Validator the validator
  */
 public static function createValidator($type, $model, $attributes, $params = [])
 {
     $params['attributes'] = $attributes;
     if ($type instanceof \Closure || $model->hasMethod($type)) {
         // method-based validator
         $params['class'] = __NAMESPACE__ . '\\InlineValidator';
         $params['method'] = $type;
     } else {
         if (isset(static::$builtInValidators[$type])) {
             $type = static::$builtInValidators[$type];
         }
         if (is_array($type)) {
             $params = array_merge($type, $params);
         } else {
             $params['class'] = $type;
         }
     }
     return Yii::createObject($params);
 }
 /**
  * @param MonsterProvidersTrait|yii\base\Model $model
  * @param string                $postKey
  */
 protected function providersSupplied(&$model, $postKey)
 {
     if ($this->action() === self::ACTION_DEFAULT) {
         // skip on default action
         return;
     }
     $providers = ArrayHelper::getValue($this->visualBuilderProvided(), "{$postKey}.providers", null);
     if (is_array($providers) === false) {
         // no providers supplied
         return;
     }
     //! @todo add check for proper class names and properties here
     $model->setEntityDataProviders($providers);
     if ($this->action() === self::ACTION_SAVE) {
         $result = $model->hasMethod('saveMonsterContent') ? $model->saveMonsterContent() : $model->saveProviders();
         if ($result === false) {
             throw new \Exception(var_export($model->errors, true));
         }
     }
 }