本文整理汇总了PHP中yii\base\DynamicModel::getErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP DynamicModel::getErrors方法的具体用法?PHP DynamicModel::getErrors怎么用?PHP DynamicModel::getErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\DynamicModel
的用法示例。
在下文中一共展示了DynamicModel::getErrors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
*
* @return type
* @throws BadRequestHttpException
*/
public function run()
{
if (Yii::$app->request->isPost) {
if (!empty($this->getParamName) && Yii::$app->getRequest()->get($this->getParamName)) {
$this->paramName = Yii::$app->getRequest()->get($this->getParamName);
}
$file = UploadedFile::getInstanceByName($this->paramName);
$model = new DynamicModel(compact('file'));
$model->addRule('file', $this->_validator, $this->validatorOptions);
if ($model->validate()) {
if ($this->unique === true) {
$model->file->name = uniqid() . (empty($model->file->extension) ? '' : '.' . $model->file->extension);
}
$result = $model->file->saveAs($this->path . $model->file->name) ? ['key' => $model->file->name, 'caption' => $model->file->name, 'name' => $model->file->name] : ['error' => 'Can\'t upload file'];
} else {
$result = ['error' => $model->getErrors()];
}
if (Yii::$app->getRequest()->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
}
return $result;
} else {
throw new BadRequestHttpException('Only POST is allowed');
}
}
示例2: 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;
}