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


PHP Book::validate方法代码示例

本文整理汇总了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]);
     }
 }
开发者ID:kmolchanov,项目名称:rgkgroup-test,代码行数:23,代码来源:BookController.php

示例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()]);
     }
 }
开发者ID:vitalik74,项目名称:gosman-test-app,代码行数:24,代码来源:BookController.php

示例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'];
         }
     }
 }
开发者ID:CrystalEller,项目名称:yii2ebooks,代码行数:38,代码来源:BookController.php


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