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


PHP DynamicModel::hasErrors方法代码示例

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


在下文中一共展示了DynamicModel::hasErrors方法的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: testValidateAttribute

 public function testValidateAttribute()
 {
     $this->tester->haveInCollection(Number::collectionName(), ['number' => '9101234567']);
     $this->specify("Test validate", function ($number, $expected) {
         $this->model['number'] = $number;
         $this->model->validate(['number']);
         $this->tester->assertEquals($expected, $this->model->hasErrors());
     }, ['examples' => [['9101234567', false], ['1111111111', true], ['9121111111', true]]]);
 }
开发者ID:shubnikofff,项目名称:mobiles,代码行数:9,代码来源:MobileNumberValidatorTest.php

示例3: testValidateWithAddRule

 public function testValidateWithAddRule()
 {
     $email = 'invalid';
     $name = 'long name';
     $age = '';
     $model = new DynamicModel(compact('name', 'email', 'age'));
     $model->addRule(['email', 'name', 'age'], 'required')->addRule('email', 'email')->addRule('name', 'string', ['max' => 3])->validate();
     $this->assertTrue($model->hasErrors());
     $this->assertTrue($model->hasErrors('email'));
     $this->assertTrue($model->hasErrors('name'));
     $this->assertTrue($model->hasErrors('age'));
 }
开发者ID:noname007,项目名称:yii2-1,代码行数:12,代码来源:DynamicModelTest.php

示例4: 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

示例5: 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

示例6: savePhoto

 /**
  * Save photo
  *
  * @param \app\models\UserProfile $profile
  * @param string $photo
  * @return void
  */
 private function savePhoto($profile, $photo)
 {
     $file = $this->makeUploadedFile($photo);
     $model = new DynamicModel(compact('file'));
     $model->addRule('file', 'image', $profile->fileRules('photo', true))->validate();
     if (!$model->hasErrors()) {
         $profile->createFile('photo', $file->tempName, $model->file->name);
     }
 }
开发者ID:rkit,项目名称:bootstrap-yii2,代码行数:16,代码来源:SignupProviderForm.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…')]);
     }
     $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

示例8: 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

示例9: 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

示例10: 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

示例11: hasError

 public function hasError()
 {
     return $this->model->hasErrors();
 }
开发者ID:nthrnth,项目名称:catering-terminal,代码行数:4,代码来源:CredentialsForm.php

示例12: 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

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