本文整理汇总了PHP中app\models\Book::validate方法的典型用法代码示例。如果您正苦于以下问题:PHP Book::validate方法的具体用法?PHP Book::validate怎么用?PHP Book::validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Book
的用法示例。
在下文中一共展示了Book::validate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionCreate
/**
* Creates a new Book model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Book();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$image = UploadedFile::getInstance($model, 'imageFile');
if (!empty($image)) {
$model->image = Yii::$app->security->generateRandomString() . '.' . $image->extension;
$path = Yii::$app->basePath . '/' . Book::IMAGES_DIRECTORY . $model->image;
$model->save();
$image->saveAs($path);
} else {
$model->save();
}
return $this->redirect(['index']);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例2: save
/**
* @param Book $model
* @param $template
* @return array|Response
*/
protected function save($model, $template)
{
if ($model->load(Yii::$app->request->post())) {
if ($model->validate()) {
if ($model->save(false)) {
$params = ArrayHelper::merge(['index'], $this->getSession()->get(self::SESSION_PARAMS));
return $this->redirect($params);
} else {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
} else {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
} else {
return $this->{$this->getCurrentRender()}($template, ['model' => $model, 'authors' => $this->getAuthors()]);
}
}
示例3: actionFile
public function actionFile()
{
\Yii::$app->response->format = Response::FORMAT_JSON;
if (\Yii::$app->request->isPost) {
$book = new Book();
$book->imageFile = UploadedFile::getInstanceByName('imageFile');
$book->bookFile = UploadedFile::getInstanceByName('bookFile');
if (!$book->validate(['imageFile', 'bookFile'])) {
\Yii::$app->response->statusCode = 400;
return ['status' => 'Err', 'errors' => $book->getErrors()];
} else {
if (!empty($book->imageFile)) {
$fileDir = \Yii::$app->basePath . '/web/image/book/tmp/1/';
if (!file_exists($fileDir)) {
mkdir($fileDir, 0755, true);
}
array_map('unlink', glob(\Yii::$app->basePath . $fileDir . '*'));
$book->imageFile->saveAs($fileDir . $book->imageFile->baseName . '.' . $book->imageFile->extension);
}
if (!empty($book->bookFile)) {
$fileDir = \Yii::$app->basePath . '/files/tmp/book-form/1/book-file/';
if (!file_exists($fileDir)) {
mkdir($fileDir, 0755, true);
}
$book->bookFile->saveAs($fileDir . $book->bookFile->baseName . '.' . $book->bookFile->extension);
}
\Yii::$app->response->statusCode = 200;
return ['status' => 'OK'];
}
}
if (\Yii::$app->request->isGet) {
if (!empty(\Yii::$app->request->get('remove')) && !empty(\Yii::$app->request->get('file'))) {
unlink(\Yii::$app->basePath . '/files/tmp/book-form/1/book-file/' . \Yii::$app->request->get('file'));
\Yii::$app->response->statusCode = 200;
return ['status' => 'OK'];
}
}
}