2015-07-29 17:32:28 5959次浏览 1条回答 0 悬赏 11 金钱

现在我要添加数据 就2个输入框(都是必填的)
控制器:

public function actionAddNotice()
{
	$noticec = new NoticeC();
	if(isset($_POST['Notice']) && $_POST['Notice']){
		$post = $_POST['Notice'];
		$flag = false;
		//验证公告标题
		if(empty($post['title'])){
			$flag = true;
			Yii::app()-> user -> setFlash('title_error','公告标题必填');**$this->refresh();**
		}
		//验证公告内容
		if(empty($post['content'])){
			$flag = true;
			Yii::app()-> user -> setFlash('content_error','公告内容必填');**$this->refresh();**
		}

		if(!$flag){
			$result = $noticec -> addNotice($post['title'],$post['content']);
			$result = json_decode($result,true);

			if($result['status'] == ERROR_NONE){
				$this->redirect(array('noticeList'));
			}else{
				echo '数据保存失败,错误状态码:'.$result['status'];exit;
			}
		}
	}
	$this->render('addNotice');
}

点击提交的时候,(现在都没填) 标题那出现错误提示(标题必填) 内容那没有提示 在这里$this->refresh();好像断点一样,下面代码不运行了 当时界面还是渲染的 又不像是断点 实在搞不清楚$this->refresh()的作用?希望给位请教

  • 回答于 2015-07-29 17:44 举报

    refresh()执行的是redirect,在CController中的函数式这样定义的:

    /**
     * Refreshes the current page.
     * The effect of this method call is the same as user pressing the
     * refresh button on the browser (without post data).
     * @param boolean $terminate whether to terminate the current application after calling this method
     * @param string $anchor the anchor that should be appended to the redirection URL.
     * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
     */
    public function refresh($terminate=true,$anchor='')
    {
        $this->redirect(Yii::app()->getRequest()->getUrl().$anchor,$terminate);
    }
    

    你的title为空,就不会检查content了,因为重定向了就没有必要执行后面的语句。所以setFlash就只有title的信息。

    你要想继续执行而不中断当前程序,就设refresh的第一个参数为false。但是你有两个refresh,这样做逻辑上不合适。

    3 条回复
    回复于 2015-07-29 17:50 回复

    没有必要执行后面的语句。那为什么$this->render('addNotice');还是可以渲染的??

    回复于 2015-07-29 17:53 回复

    有表单提交的action是会被执行两次的,第一次跳过(因为不是表单POST数据)

    if(isset($_POST['Notice']) && $_POST['Notice']){
    

    渲染你的view,提交后处理POST的数据,一般不会再render了。后面的语句的确没有执行,但是你reresh()后会重来一次。

    回复于 2015-07-30 09:53 回复

    因为refresh之后,没有进入那个if分支啊...

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
xyf90314
副总裁

xyf90314

注册时间:2015-03-04
最后登录:2023-03-13
在线时长:95小时23分
  • 粉丝21
  • 金钱5257
  • 威望40
  • 积分6607

热门问题