formName() public method

The form name is mainly used by ActiveForm to determine how to name the input fields for the attributes in a model. If the form name is "A" and an attribute name is "b", then the corresponding input name would be "A[b]". If the form name is an empty string, then the input name would be "b". The purpose of the above naming schema is that for forms which contain multiple different models, the attributes of each model are grouped in sub-arrays of the POST-data and it is easier to differentiate between them. By default, this method returns the model class name (without the namespace part) as the form name. You may override it when the model is used in different forms.
See also: load()
public formName ( ) : string
return string the form name of this model class.
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (!$this->header) {
         $this->header = "<h2>" . Inflector::camel2words($this->model->formName()) . " change log:</h2>";
     }
 }
Beispiel #2
3
 /**
  * Performs model ajax validation
  *
  * @param Model $model
  * @return array|null
  */
 protected function performAjaxValidation(Model $model)
 {
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) && Yii::$app->request->post('ajax') == $model->formName()) {
         // AJAX validation
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
     return null;
 }
Beispiel #3
2
 /**
  * Берет значения из POST и возвраает знаяения для добавления в БД
  *
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array
  */
 public static function onLoad($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $value = ArrayHelper::getValue(\Yii::$app->request->post(), $model->formName() . '.' . $fieldName, false);
     if ($value) {
         $value = true;
     }
     $model->{$fieldName} = $value;
 }
 /**
  * @inheritdoc
  */
 public function formName()
 {
     if (!empty($this->_formName)) {
         return $this->_formName;
     } else {
         return parent::formName();
     }
 }
 /**
  * @param \yii\base\Model $model
  *
  * @return string
  */
 protected function getCategory(Model $model)
 {
     if (!$model->isNewRecord) {
         $id = $model->id;
         $category = self::className() . ':' . $model->formName() . '-' . $id;
     } else {
         $category = self::className() . ':' . $model->formName();
     }
     return $category;
 }
 /**
  * @return integer
  */
 protected function getCountAttempts()
 {
     if (!isset($this->_attemptsCount)) {
         $this->_attemptsCount = (new Query())->from($this->table)->where(['form' => $this->owner->formName(), 'ip' => ip2long(Yii::$app->getRequest()->getUserIP())])->count('*', $this->db);
     }
     return $this->_attemptsCount;
 }
Beispiel #7
0
 /**
  * Берет значения из POST и возвраает знаяения для добавления в БД
  *
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array
  */
 public static function onUpdate($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $value = ArrayHelper::getValue(\Yii::$app->request->post(), $model->formName() . '.' . $fieldName, null);
     if ($value == '') {
         $value = null;
     }
     return [$fieldName => $value];
 }
Beispiel #8
0
 /**
  * Возвращает массив идентификаторов которые были отмечены
  *
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array [1, 2, 3, ... ] массив идентификаторов
  */
 public static function getValue($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $value = ArrayHelper::getValue(Yii::$app->request->post(), $model->formName() . '.' . $fieldName, []);
     return $value;
 }
Beispiel #9
0
 /**
  *
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array
  */
 public static function onUpdate($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $post = \Yii::$app->request->post()[$model->formName()];
     $fieldIntCountry = $fieldName . '_country';
     $fieldIntRegion = $fieldName . '_region';
     $fieldIntTown = $fieldName . '_town';
     $fieldTextCountry = $fieldName . '_country-name';
     $fieldTextRegion = $fieldName . '_region-name';
     $fieldTextTown = $fieldName . '_town-name';
     $valueTextCountry = ArrayHelper::getValue($post, $fieldTextCountry, '');
     $valueTextRegion = ArrayHelper::getValue($post, $fieldTextRegion, '');
     $valueTextTown = ArrayHelper::getValue($post, $fieldTextTown, '');
     if ($valueTextCountry == '' && $valueTextRegion == '' && $valueTextTown == '') {
         return [$fieldIntCountry => null, $fieldIntRegion => null, $fieldIntTown => null];
     }
     if ($valueTextCountry != '' && $valueTextRegion == '' && $valueTextTown == '') {
         return [$fieldIntCountry => ArrayHelper::getValue($post, $fieldIntCountry, null), $fieldIntRegion => null, $fieldIntTown => null];
     }
     if ($valueTextCountry != '' && $valueTextRegion != '' && $valueTextTown == '') {
         return [$fieldIntCountry => ArrayHelper::getValue($post, $fieldIntCountry, null), $fieldIntRegion => ArrayHelper::getValue($post, $fieldIntRegion, null), $fieldIntTown => null];
     }
     return [$fieldIntCountry => ArrayHelper::getValue($post, $fieldIntCountry, null), $fieldIntRegion => ArrayHelper::getValue($post, $fieldIntRegion, null), $fieldIntTown => ArrayHelper::getValue($post, $fieldIntTown, null)];
 }
Beispiel #10
0
 /**
  * Emulates file uploading with existing file.
  * @param $model
  * @param $attribute
  * @param array $options =
  * ['name' => 'file.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/asdZXC', 'error' => 0, 'size' => 123123]
  */
 public static function setUploadedFile(Model $model, $attribute, $options = [])
 {
     foreach ($options as $name => $value) {
         $_FILES[$model->formName()][$name][$attribute] = $value;
     }
 }
 /**
  * Generates the hierarchy to access the ngModel under the $form object in the AngularJS scope.
  * @param Model $model
  * @param string $attribute
  * @return string
  */
 public static function getFormNgModel($model, $attribute)
 {
     $formName = $model->formName();
     return $formName == '' ? $attribute : "{$formName}.{$attribute}";
 }
Beispiel #12
0
 public function formName()
 {
     $event = new FormNameEvent(['formName' => parent::formName()]);
     $this->trigger(self::EVENT_FORM_NAME, $event);
     return $event->formName;
 }
 /**
  * @return string
  */
 public function formName()
 {
     return $this->_formName ?: parent::formName();
 }
Beispiel #14
0
 /**
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return bool
  */
 public static function onLoad($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $modelName = $model->formName();
     $valueUrl = ArrayHelper::getValue(Yii::$app->request->post(), $modelName . '.' . $fieldName . '-url', '');
     $value = ArrayHelper::getValue(Yii::$app->request->post(), $modelName . '.' . $fieldName . '-value', '');
     $model->{$fieldName} = ['url' => $valueUrl, 'file' => UploadedFile::getInstance($model, $fieldName), 'value' => $value];
     return true;
 }
Beispiel #15
0
 /**
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array поля для обновления в БД
  */
 public static function onLoad($field, $model)
 {
     $fieldName = $field[\cs\base\BaseForm::POS_DB_NAME];
     $array = Yii::$app->request->post($model->formName());
     $value = ArrayHelper::getValue($array, $fieldName, '');
     if ($value == '') {
         $model->{$fieldName} = null;
     } else {
         $dateFormat = ArrayHelper::getValue($field, 'widget.1.dateFormat', 'php:d.m.Y');
         if (strncmp($dateFormat, 'php:', 4) === 0) {
             $dateFormat = substr($dateFormat, 4);
         } else {
             $dateFormat = FormatConverter::convertDateIcuToPhp($dateFormat);
         }
         $model->{$fieldName} = \DateTime::createFromFormat($dateFormat, $value);
     }
 }
Beispiel #16
0
 /**
  * @param array $field
  * @param \yii\base\Model $model
  */
 public static function onLoad($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $post = Yii::$app->request->post();
     $query = $model->formName() . '.' . $fieldName;
     $model->{$fieldName} = ArrayHelper::getValue($post, $query, '');
 }
Beispiel #17
0
 /**
  * Возвращает значение поля формы из поста
  *
  * @param string          $fieldName
  * @param \yii\base\Model $model
  *
  * @return string
  */
 public static function getParam($fieldName, $model)
 {
     $formName = $model->formName();
     $query = $formName . '.' . $fieldName;
     return ArrayHelper::getValue(\Yii::$app->request->post(), $query, '');
 }
Beispiel #18
0
 /**
  * Generates an appropriate input name for the specified attribute name or expression.
  *
  * This method generates a name that can be used as the input name to collect user input
  * for the specified attribute. The name is generated according to the [[Model::formName|form name]]
  * of the model and the given attribute name. For example, if the form name of the `Post` model
  * is `Post`, then the input name generated for the `content` attribute would be `Post[content]`.
  *
  * See [[getAttributeName()]] for explanation of attribute expression.
  *
  * @param Model $model the model object
  * @param string $attribute the attribute name or expression
  * @return string the generated input name
  * @throws InvalidParamException if the attribute name contains non-word characters.
  */
 public static function getInputName($model, $attribute)
 {
     $formName = $model->formName();
     if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {
         throw new InvalidParamException('Attribute name must contain word characters only.');
     }
     $prefix = $matches[1];
     $attribute = $matches[2];
     $suffix = $matches[3];
     if ($formName === '' && $prefix === '') {
         return $attribute . $suffix;
     } elseif ($formName !== '') {
         return $formName . $prefix . "[{$attribute}]" . $suffix;
     } else {
         throw new InvalidParamException(get_class($model) . '::formName() cannot be empty for tabular inputs.');
     }
 }
Beispiel #19
0
 /**
  * Сохранить значения атрибутов модели в сессии в хлебной крошке.
  * @param ActiveRecord|Model $Model Модель, атрибуты которой будут сохранены в сессии в последней хлебной крошки.
  * @param bool $PreviusBC Если True, то сохранить в сессии предыдущей хлебной крошки.
  * @throws \Exception
  */
 public static function SetSessionValuesFromAR($Model, $PreviusBC = false)
 {
     if ($Model instanceof ActiveRecord) {
         $BC = self::GetBreadcrumbsFromSession();
         end($BC);
         if ($PreviusBC) {
             prev($BC);
         }
         foreach ($Model as $attr => $value) {
             $BC[key($BC)]['dopparams'][$Model->formName()][$attr] = $value;
         }
         $session = new Session();
         $session->open();
         $session['breadcrumbs'] = $BC;
         $session->close();
     } else {
         throw new \Exception('Ошибка в Proc::SetSessionValuesFromAR()');
     }
 }