load() public method

This method provides a convenient shortcut for: php if (isset($_POST['FormName'])) { $model->attributes = $_POST['FormName']; if ($model->save()) { handle success } } which, with load() can be written as: php if ($model->load($_POST) && $model->save()) { handle success } load() gets the 'FormName' from the model's Model::formName method (which you may override), unless the $formName parameter is given. If the form name is empty, load() populates the model with the whole of $data, instead of $data['FormName']. Note, that the data being populated is subject to the safety check by Model::setAttributes.
public load ( array $data, string $formName = null ) : boolean
$data array the data array to load, typically `$_POST` or `$_GET`.
$formName string the form name to use to load the data into the model. If not set, [[formName()]] is used.
return boolean whether `load()` found the expected form in `$data`.
Esempio n. 1
3
 /**
  * Performs ajax validation.
  *
  * @param \yii\base\Model $model
  *
  * @throws \yii\base\ExitException
  */
 protected function performValidationModel(Model $model)
 {
     if ($model->load(Yii::$app->request->post())) {
         return ActiveForm::validate($model);
     }
     return;
 }
Esempio n. 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;
 }
Esempio n. 3
2
 /**
  * Performs ajax validation.
  * @param Model $model
  * @throws \yii\base\ExitException
  */
 protected function performAjaxValidation(Model $model)
 {
     if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
         \Yii::$app->response->format = Response::FORMAT_JSON;
         echo json_encode(ActiveForm::validate($model));
         \Yii::$app->end();
     }
 }
 /**
  * Performs ajax validation.
  * @param Model $model
  */
 protected function performAjaxValidation(Model $model)
 {
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
 }
Esempio n. 5
0
 /**
  * Populates the model with input data.
  * @param Model $model model instance.
  * @param array $data the data array to load, typically `$_POST` or `$_GET`.
  * @return boolean whether expected forms are found in `$data`.
  */
 protected function load($model, $data)
 {
     /* @var $this Action */
     $event = new ActionEvent($this, ['model' => $model, 'result' => $model->load($data)]);
     $this->trigger('afterDataLoad', $event);
     return $event->result;
 }
Esempio n. 6
0
 public function load($data, $formName = null)
 {
     $isLoad = true;
     foreach ($this->models as $key => $model) {
         $isLoad = $model->load($data, $formName) && $isLoad;
     }
     return parent::load($data, $formName) && $isLoad;
 }
Esempio n. 7
0
 /**
  * @inheritdoc
  */
 public function load($data, $formName = null)
 {
     $files = \yii\web\UploadedFile::getInstancesByName('logo');
     if (count($files) != 0) {
         $file = $files[0];
         $this->logo = $file;
     }
     return parent::load($data, $formName);
 }
Esempio n. 8
0
 public function load($data, $formName = null)
 {
     if (parent::load($data, $formName)) {
         $this->file = UploadedFile::getInstance($this, 'file');
         return true;
     } else {
         return false;
     }
 }
 public function load($post)
 {
     if ($post["AdminUserFunctionsForm"]["id_user"]) {
         foreach ($post["AdminUserFunctionsForm"]["id_user"] as $id_user) {
             $this->id_user[] = $id_user;
         }
     }
     return parent::load($post);
 }
 /**
  * @inheritdoc
  */
 public function load($data, $formName = null)
 {
     if (!parent::load($data, $formName)) {
         return false;
     }
     $this->name = preg_replace('/\\s+/', '_', $this->name);
     $this->name = preg_replace('/[^0-9a-zA-Z$_]+/', '', $this->name);
     return true;
 }
Esempio n. 11
0
 public function load($data = null, $formName = '')
 {
     parent::load($data, $formName);
     foreach ($this->_defaults as $k => $v) {
         if (is_null($this->{$k})) {
             $this->{$k} = $v;
         }
     }
     return true;
 }
Esempio n. 12
0
 protected function performAjaxValidation(Model $model)
 {
     $isAjaxRequest = \Yii::$app->request->isAjax();
     $postData = \Yii::$app->request->post();
     if ($isAjaxRequest && $model->load($postData)) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         echo json_encode(ActiveForm::validate($model));
         Yii::$app->end();
     }
 }
Esempio n. 13
0
 /**
  * @inheritdoc
  */
 public function load($data, $formName = '')
 {
     if (strlen($data['fqdn']) && strpos($data['fqdn'], '.') === false) {
         if (strlen($data['zone'])) {
             $data['fqdn'] .= '.' . $data['zone'];
             unset($data['zone']);
         } else {
             $data['fqdn'] .= '.' . static::DEFAULT_ZONE;
         }
     }
     return parent::load($data, $formName);
 }
Esempio n. 14
0
 /**
  * Validate Form action
  * @param Model $model
  * @param string $action
  * @return array
  */
 public function setAjaxData(Model $model, $action = 'save', $json = false)
 {
     $response = ['status' => 0, 'errors' => '', 'text' => ''];
     if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
         if ($model->{$action}()) {
             $response['status'] = 1;
         } else {
             $response['errors'] = Html::errorSummary($model);
         }
     }
     return $json ? json_encode($response) : [$model, $response];
 }
Esempio n. 15
0
 /**
  * Populates the main model and variation models with input data.
  * @param Model $model main model instance.
  * @param array $data the data array to load, typically `$_POST` or `$_GET`.
  * @return boolean whether expected forms are found in `$data`.
  */
 protected function load($model, $data)
 {
     if (!$model->load($data)) {
         return false;
     }
     foreach ($this->getVariationModelBatches($model) as $variationName => $variationModels) {
         if (!Model::loadMultiple($variationModels, $data)) {
             return false;
         }
     }
     return true;
 }
 /**
  * @inheritdoc
  */
 public function load($data, $formName = null)
 {
     if (!parent::load($data, $formName)) {
         return false;
     }
     if (in_array($this->delete, ['', 'RESTRAINT', 'NO ACTION'])) {
         $this->delete = null;
     }
     if (in_array($this->update, ['', 'RESTRAINT', 'NO ACTION'])) {
         $this->update = null;
     }
     return true;
 }
Esempio n. 17
0
 public function load($data, $formName = null)
 {
     if (array_key_exists('MenuItemForm', $data) && ($items = $data['MenuItemForm'])) {
         $this->items = [];
         foreach ($items as $item) {
             $model = new MenuItemForm();
             $model->setAttributes($item);
             $this->items[] = $model;
         }
     }
     return parent::load($data, $formName);
     // TODO: Change the autogenerated stub
 }
 /**
  * Load owner model and translation models from data.
  * @param $data
  * @param null $languages array of languages for loading
  * @param null $formName
  * @param null $translationFormName
  * @return bool
  */
 public function loadWithTranslations($data, $languages, $formName = null, $translationFormName = null)
 {
     if ($this->owner->load($data, $formName)) {
         if (!$languages) {
             throw new InvalidParamException('Languages must be set.');
         }
         foreach ($languages as $language) {
             $modelI18n = static::getTranslation($language);
             $modelI18n->load($data, $translationFormName);
         }
         return true;
     }
     return false;
 }
Esempio n. 19
0
 public function load($data, $formName = null)
 {
     if (!array_key_exists('ConfigurationFieldModel', $data)) {
         \Yii::trace('not found');
         return false;
     }
     foreach ($data['ConfigurationFieldModel'] as $field) {
         if ($desiredField = $this->getField($field['id'])) {
             $desiredField->setAttributes($field);
             $this->setField($desiredField);
         }
     }
     return parent::load($data, $formName);
     // TODO: Change the autogenerated stub
 }
Esempio n. 20
0
 public function load($data, $formName = null)
 {
     if (isset($data['ImportModel']) && isset($data['ImportModel']['fields']) && isset($data['ImportModel']['fields']['property']) && $data['ImportModel']['fields']['property']) {
         foreach ($data['ImportModel']['fields']['property'] as $key => $property) {
             if (!isset($property['enabled']) || !$property['enabled']) {
                 unset($data['ImportModel']['fields']['property'][$key]);
             }
         }
     }
     if (isset($data['ImportModel']) && isset($data['ImportModel']['fields']) && isset($data['ImportModel']['fields']['additionalFields']) && $data['ImportModel']['fields']['additionalFields']) {
         foreach ($data['ImportModel']['fields']['additionalFields'] as $key => $additionalFields) {
             if (!isset($additionalFields['enabled']) || !$additionalFields['enabled']) {
                 unset($data['ImportModel']['fields']['additionalFields'][$key]);
             }
         }
     }
     return parent::load($data, $formName = null);
 }
Esempio n. 21
0
 /**
  * @param array $data
  * @param null $formName
  * @return bool
  */
 public function load($data, $formName = null)
 {
     $formName = $formName === null ? '' : $formName;
     return parent::load($data, $formName);
 }
Esempio n. 22
0
 public function load($post, $formName = null)
 {
     if (!parent::load($post, $formName)) {
         return false;
     }
     foreach (static::$fields as $field) {
         $class = ArrayHelper::getValue($field, 'widget.0', '');
         if ($class != '') {
             if (method_exists($class, 'onLoad')) {
                 $class::onLoad($field, $this);
             }
         }
     }
     if (isset(static::$fields2)) {
         foreach (self::$fields as $field) {
             $class = ArrayHelper::getValue($field, 'widget.0', '');
             if ($class != '') {
                 if (method_exists($class, 'onLoad')) {
                     $class::onLoad($field, $this);
                 }
             }
         }
     }
     return true;
 }
Esempio n. 23
0
 protected function performAjaxValidation(Model $model)
 {
     if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
         \Yii::$app->response->format = Response::FORMAT_JSON;
         $validation = ActiveForm::validate($model);
         //            var_dump($validation);die;
         //            if ($validation) {
         //                echo json_encode($validation);
         //                \Yii::$app->end();
         //            } else {
         //                return true;
         //            }
         echo json_encode($validation);
         \Yii::$app->end();
     }
 }
Esempio n. 24
0
 /**
  * Populates the model with the data from end user.
  * @param Model $model
  * @param array $data
  * @return boolean whether the model is successfully populated with some data.
  */
 protected function loadModel($model, $data)
 {
     return $model->load($data);
 }
Esempio n. 25
0
 /**
  * Performs AJAX validation.
  *
  * @param array|Model $model
  *
  * @throws ExitException
  */
 protected function performAjaxValidation($model)
 {
     if (Yii::$app->request->isAjax && !Yii::$app->request->isPjax) {
         if ($model->load(Yii::$app->request->post())) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             Yii::$app->response->content = ActiveForm::validate($model);
             Yii::$app->end();
         }
     }
 }
Esempio n. 26
0
 public function load($data, $formName = null)
 {
     if (!parent::load($data, $formName)) {
         return false;
     }
     $this->setPrice(isset($_POST['EventPrice']) ? $_POST['EventPrice'] : []);
     return true;
 }
Esempio n. 27
0
 /**
  * @inheritdoc
  */
 public function load($data, $formName = null)
 {
     $this->dashboardPanel->load($data);
     return parent::load($data, $formName);
 }
Esempio n. 28
0
 /**
  * @param Model $model
  * @return array
  */
 public function ajaxValidateForm(Model $model)
 {
     $model->load(\Yii::$app->request->post());
     return ActiveForm::validate($model);
 }
Esempio n. 29
0
 /**
  * @param Model $model
  * @param array $data the data array.
  * @param string $scope
  * @return boolean whether the model is successfully populated with some data.
  */
 protected function loadModel($model, $data, $scope = null)
 {
     return $model->load($data, $scope);
 }
 /**
  * @inheritdoc
  */
 public function load($data, $formName = null)
 {
     if (!parent::load($data, $formName)) {
         return false;
     }
     $this->schema = [];
     if (empty($this->length)) {
         $this->schema[] = $this->type;
     } else {
         $this->schema[$this->type] = $this->length;
     }
     if ($this->autoIncrement && !in_array($this->type, static::integerTypes())) {
         $this->autoIncrement = false;
     }
     if ($this->unique && !in_array($this->type, static::primaryKeyTypes())) {
         $this->schema[] = 'unique';
     } else {
         $this->unique = false;
     }
     if ($this->notNull && !in_array($this->type, static::primaryKeyTypes())) {
         $this->schema[] = 'notNull';
     } else {
         $this->notNull = false;
     }
     if ($this->unsigned && in_array($this->type, static::integerTypes())) {
         $this->schema[] = 'unsigned';
     } else {
         $this->unsigned = false;
     }
     if (!is_null($this->defaultValue) && strlen($this->defaultValue) > 0 && !in_array($this->type, static::primaryKeyTypes())) {
         $this->schema['defaultValue'] = $this->defaultValue;
     } else {
         $this->defaultValue = null;
     }
     return true;
 }