本文整理汇总了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;
}
}