当前位置: 首页>>代码示例>>PHP>>正文


PHP DynamicModel::getFirstError方法代码示例

本文整理汇总了PHP中yii\base\DynamicModel::getFirstError方法的典型用法代码示例。如果您正苦于以下问题:PHP DynamicModel::getFirstError方法的具体用法?PHP DynamicModel::getFirstError怎么用?PHP DynamicModel::getFirstError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yii\base\DynamicModel的用法示例。


在下文中一共展示了DynamicModel::getFirstError方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact($this->uploadParam));
         $model->addRule($this->uploadParam, 'file', ['maxSize' => $this->maxSize, 'tooBig' => Yii::t('upload', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]), 'extensions' => explode(', ', $this->extensions), 'checkExtensionByMimeType' => false, 'wrongExtension' => Yii::t('upload', 'EXTENSION_ERROR', ['formats' => $this->extensions])])->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError($this->uploadParam)];
         } else {
             $model->{$this->uploadParam}->name = uniqid() . '.' . $model->{$this->uploadParam}->extension;
             $request = Yii::$app->request;
             $image_name = $this->temp_path . $model->{$this->uploadParam}->name;
             $image = Image::crop($file->tempName . $request->post('filename'), intval($request->post('w')), intval($request->post('h')), [$request->post('x'), $request->post('y')])->resize(new Box($this->width, $this->height))->save($image_name);
             // watermark
             if ($this->watermark != '') {
                 $image = Image::watermark($image_name, $this->watermark)->save($image_name);
             }
             if ($image->save($image_name)) {
                 // create Thumbnail
                 if ($this->thumbnail && ($this->thumbnail_width > 0 && $this->thumbnail_height > 0)) {
                     Image::thumbnail($this->temp_path . $model->{$this->uploadParam}->name, $this->thumbnail_width, $this->thumbnail_height)->save($this->temp_path . '/thumbs/' . $model->{$this->uploadParam}->name);
                 }
                 $result = ['filelink' => $model->{$this->uploadParam}->name];
             } else {
                 $result = ['error' => Yii::t('upload', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException(Yii::t('upload', 'ONLY_POST_REQUEST'));
     }
 }
开发者ID:plathir,项目名称:yii2-smart-upload,代码行数:36,代码来源:ImageCropUploadAction.php

示例2: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     $result = ['err' => 1, 'msg' => 'swfupload init'];
     if (Yii::$app->request->isPost) {
         $data = Yii::$app->request->get('data');
         $data = Json::decode(base64_decode($data));
         $url = $data['url'];
         $path = $data['path'];
         $params = $data['params'];
         $callback = $data['callback'];
         $file = UploadedFile::getInstanceByName('FileData');
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', 'image', [])->validate();
         if ($model->hasErrors()) {
             $result['msg'] = $model->getFirstError('file');
         } else {
             if ($model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($path . $model->file->name)) {
                 $result['err'] = 0;
                 $result['msg'] = 'success';
                 $result['url'] = $model->file->name;
                 $result['params'] = $params;
                 $result['callback'] = $callback;
             } else {
                 $result['msg'] = $model->getFirstError('file save error!');
             }
         }
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $result;
 }
开发者ID:chenyuzou,项目名称:yii2-swfupload,代码行数:36,代码来源:UploadAction.php

示例3: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact($this->uploadParam));
         $model->addRule($this->uploadParam, 'image', ['maxSize' => $this->maxSize, 'tooBig' => Yii::t('cropper', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]), 'extensions' => explode(', ', $this->extensions), 'wrongExtension' => Yii::t('cropper', 'EXTENSION_ERROR', ['formats' => $this->extensions])])->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError($this->uploadParam)];
         } else {
             $model->{$this->uploadParam}->name = uniqid() . '.' . $model->{$this->uploadParam}->extension;
             $request = Yii::$app->request;
             $width = $request->post('width', $this->width);
             $height = $request->post('height', $this->height);
             $image = Image::crop($file->tempName . $request->post('filename'), intval($request->post('w')), intval($request->post('h')), [$request->post('x'), $request->post('y')])->resize(new Box($width, $height));
             if ($image->save($this->path . $model->{$this->uploadParam}->name)) {
                 $result = ['filelink' => $this->url . $model->{$this->uploadParam}->name];
             } else {
                 $result = ['error' => Yii::t('cropper', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException(Yii::t('cropper', 'ONLY_POST_REQUEST'));
     }
 }
开发者ID:budyaga,项目名称:yii2-cropper,代码行数:29,代码来源:UploadAction.php

示例4: actionData

 public function actionData($file = false)
 {
     \Yii::$app->response->format = Response::FORMAT_JSON;
     if (\Yii::$app->request->isPost) {
         /**@var Module $module */
         $module = Module::getInstance();
         $model = new DynamicModel(['file' => null]);
         $model->addRule('file', 'file', ['extensions' => $module->expansions, 'maxSize' => $module->maxSize]);
         $model->file = UploadedFile::getInstanceByName('image');
         if ($model->validate()) {
             if (!is_dir(\Yii::getAlias($module->uploadDir))) {
                 FileHelper::createDirectory(\Yii::getAlias($module->uploadDir));
             }
             $oldFileName = $model->file->name;
             $newFileName = $module->isFileNameUnique ? uniqid() . '.' . $model->file->extension : $oldFileName;
             $newFullFileName = \Yii::getAlias($module->uploadDir) . DIRECTORY_SEPARATOR . $newFileName;
             if ($model->file->saveAs($newFullFileName)) {
                 return ['id' => $oldFileName, 'url' => \Yii::$app->request->getHostInfo() . str_replace('@webroot', '', $module->uploadDir) . '/' . $newFileName];
             }
         } else {
             \Yii::$app->response->statusCode = 500;
             return $model->getFirstError('file');
         }
     } elseif (\Yii::$app->request->isDelete && $file) {
         return true;
     }
     throw new BadRequestHttpException();
 }
开发者ID:Hector68,项目名称:yii2-grafikart-markdown-editor,代码行数:28,代码来源:UploadController.php

示例5: run

 public function run()
 {
     $file = UploadedFile::getInstanceByName($this->inputName);
     if (!$file) {
         return $this->response(['error' => Yii::t('filemanager-yii2', 'An error occured, try again later…')]);
     }
     $model = new DynamicModel(compact('file'));
     $model->addRule('file', $this->type, $this->rules)->validate();
     if ($model->hasErrors()) {
         return $this->response(['error' => $model->getFirstError('file')]);
     } else {
         return $this->upload($file);
     }
 }
开发者ID:loveorigami,项目名称:filemanager-yii2,代码行数:14,代码来源:UploadAction.php

示例6: actionRedactorUpload

 /**
  * Redactor upload action.
  *
  * @param string $name Model name
  * @param string $attr Model attribute
  * @param string $type Format type
  * @return array List of files
  * @throws BadRequestHttpException
  * @throws InvalidConfigException
  */
 public function actionRedactorUpload($name, $attr, $type = 'image')
 {
     /** @var $module \vladdnepr\ycm\Module */
     $module = $this->module;
     $name = (string) $name;
     $attribute = (string) $attr;
     $uploadType = 'image';
     $validatorOptions = $module->redactorImageUploadOptions;
     if ((string) $type == 'file') {
         $uploadType = 'file';
         $validatorOptions = $module->redactorFileUploadOptions;
     }
     $attributePath = $module->getAttributePath($name, $attribute);
     if (!is_dir($attributePath)) {
         if (!FileHelper::createDirectory($attributePath, $module->uploadPermissions)) {
             throw new InvalidConfigException('Could not create folder "' . $attributePath . '". Make sure "uploads" folder is writable.');
         }
     }
     $file = UploadedFile::getInstanceByName('file');
     $model = new DynamicModel(compact('file'));
     $model->addRule('file', $uploadType, $validatorOptions)->validate();
     if ($model->hasErrors()) {
         $result = ['error' => $model->getFirstError('file')];
     } else {
         if ($model->file->extension) {
             $model->file->name = md5($attribute . time() . uniqid(rand(), true)) . '.' . $model->file->extension;
         }
         $path = $attributePath . DIRECTORY_SEPARATOR . $model->file->name;
         if ($model->file->saveAs($path)) {
             $result = ['filelink' => $module->getAttributeUrl($name, $attribute, $model->file->name)];
             if ($uploadType == 'file') {
                 $result['filename'] = $model->file->name;
             }
         } else {
             $result = ['error' => Yii::t('ycm', 'Could not upload file.')];
         }
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $result;
 }
开发者ID:vladdnepr,项目名称:yii2-ycm,代码行数:50,代码来源:ModelController.php

示例7: run

 public function run()
 {
     $file = UploadedFile::getInstanceByName($this->inputName);
     if (!$file) {
         return $this->response(['error' => Yii::t('filemanager-yii2', 'An error occured, try again later…')]);
     }
     $rules = $this->model->fileRules($this->attribute, true);
     $type = $this->model->fileOption($this->attribute, 'type', 'image');
     $model = new DynamicModel(compact('file'));
     $maxFiles = ArrayHelper::getValue($rules, 'maxFiles');
     if ($maxFiles !== null && $maxFiles > 1) {
         $model->file = [$model->file];
     }
     $model->addRule('file', $type, $rules)->validate();
     if ($model->hasErrors()) {
         return $this->response(['error' => $model->getFirstError('file')]);
     }
     if (is_array($model->file)) {
         $model->file = $model->file[0];
     }
     return $this->save($model->file);
 }
开发者ID:rkit,项目名称:filemanager-yii2,代码行数:22,代码来源:UploadAction.php

示例8: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->paramName);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($this->path . $model->file->name)) {
                 $result = ['name' => $model->file->name];
             } else {
                 $result = ['error' => Widget::t('fileapi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
开发者ID:cjq,项目名称:QRCode-yii2,代码行数:27,代码来源:UploadAction.php

示例9: getError

 public function getError()
 {
     return ['status' => 'error', 'message' => $this->model->getFirstError('checkField')];
 }
开发者ID:nthrnth,项目名称:catering-terminal,代码行数:4,代码来源:CredentialsForm.php

示例10: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($this->imagePath . $model->file->name)) {
                 //crop image
                 Image::thumbnail($this->imagePath . $model->file->name, $this->options['thumbWidth'], $this->options['thumbHeight'])->save($this->thumbPath . $model->file->name, ['quality' => 90]);
                 //resize image maintaining aspect ratio
                 Image::frame($this->imagePath . $model->file->name, 0, '666', 0)->thumbnail(new Box($this->options['imageWidth'], $this->options['imageHeight']))->save($this->imagePath . $model->file->name, ['quality' => 90]);
                 $result = ['filelink' => $this->url . $model->file->name];
             } else {
                 $result = ['error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
开发者ID:SObS,项目名称:yii2-imperavi-widget,代码行数:31,代码来源:ImageUploadAction.php

示例11: actionUploadAvatar

 public function actionUploadAvatar()
 {
     if (Yii::$app->user->isGuest) {
         throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     $model = new DynamicModel(['image']);
     $model->addRule('image', 'file', ['skipOnEmpty' => false, 'extensions' => 'png, jpg']);
     if (Yii::$app->request->isPost) {
         $model->image = UploadedFile::getInstanceByName('image');
         if ($model->validate()) {
             try {
                 return AvatarHelper::saveAvatar($model->image);
             } catch (Exception $exc) {
                 Yii::$app->response->statusCode = 400;
                 return Yii::t('yee', 'An unknown error occurred.');
             }
         } else {
             $errors = $model->getErrors();
             Yii::$app->response->statusCode = 400;
             return $model->getFirstError(key($errors));
         }
     }
     return;
 }
开发者ID:yeesoft,项目名称:yii2-yee-auth,代码行数:25,代码来源:DefaultController.php

示例12: testValidateOperatorGreaterOrEqual

 public function testValidateOperatorGreaterOrEqual()
 {
     $compareErrorText = Yii::t('app', '{attribute} must be greater than or equal to "{compareValue}".', ['attribute' => 'Date From', 'compareValue' => '2016-01-02']);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-02']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertEquals($errorText, $compareErrorText);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
     $model = new DynamicModel(['date_from' => '2016-01-02', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
 }
开发者ID:nepstor,项目名称:yii2-datetime-compare-validator,代码行数:22,代码来源:DateTimeCompareValidatorTest.php

示例13: run

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $md5Filename = md5($model->file->name . '_' . rand(100000, 999999) . '_' . time());
                 $filename = $md5Filename[0] . '/' . $md5Filename[1] . '/' . $md5Filename[2] . '/' . $md5Filename . '.' . $model->file->extension;
                 $model->file->name = $filename;
             }
             $uploadResult = $this->storage->uploadFile($model->file->tempName, $model->file->name);
             if ($uploadResult) {
                 if (isset($this->options['baseUrl'])) {
                     $uploadResult = Yii::getAlias($this->options['baseUrl'] . $filename);
                 }
                 $result = ['filelink' => $uploadResult];
                 if ($this->uploadOnlyImage !== true) {
                     $result['filename'] = $model->file->name;
                 }
             } else {
                 $result = ['error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
开发者ID:harrybailey,项目名称:yii2-aws-components,代码行数:36,代码来源:S3ActionUpload.php


注:本文中的yii\base\DynamicModel::getFirstError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。