本文整理匯總了PHP中app\models\Post::validate方法的典型用法代碼示例。如果您正苦於以下問題:PHP Post::validate方法的具體用法?PHP Post::validate怎麽用?PHP Post::validate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app\models\Post
的用法示例。
在下文中一共展示了Post::validate方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: actionCreate
/**
* create a post
*
* @return null|string|\yii\web\Response
* @throws Exception
* @throws \Exception
*/
public function actionCreate()
{
try {
Yii::trace('Trace :' . __METHOD__, __METHOD__);
$response = null;
$model = new Post(['scenario' => 'create']);
$query = (new Query())->select('name, id')->from('categories')->all();
$categories = ArrayHelper::map($query, 'id', 'name');
if ($model->load($_POST) === true && $model->validate() === true) {
$model->category_id = (int) $model->category_name;
$model->created = Yii::$app->formatter->asDateTime('now', 'php:Y-m-d H:i:s');
$model->user_id = Yii::$app->user->id;
$status = $model->save();
if ($status === true) {
$sidebarCacheName = isset(Yii::$app->params['cache']['sidebar']) ? Yii::$app->params['cache']['sidebar'] : null;
if ($sidebarCacheName !== null) {
Yii::$app->cache->delete($sidebarCacheName);
}
$response = $this->redirect(['/post/view', 'slug' => $model->slug]);
}
}
if ($response === null) {
$response = $this->render('create', ['model' => $model, 'categories' => $categories]);
}
return $response;
} catch (Exception $e) {
Yii::error($e->getMessage(), __METHOD__);
throw $e;
}
}