2016-12-27 15:01:37 2840次浏览 3条回答 0 悬赏 25 金钱
public function validateRecipient()
{
    if(!$this->hasErrors()){
        $data=Member::find()->where(['username'=>$this->recipient])->one();
        if(is_null($data)){
            $this->addError('recipient','没有该收件人,请重新确认');
        }
    }
}

为什么这句话它错误之后没有提示..要是输入错误的信息它不会走到后面的save(),但是就是没有提示.应该要怎么弄才可以在form表单提示错误信息?

补充于 2016-12-28 11:50

好像可以了..应该是我前台样式有问题..我把验证弄到后台的那个就出来了

最佳答案

  • 最爱红烧肉 发布于 2016-12-28 11:17 举报
     $this->addError('recipient','没有该收件人,请重新确认');  
    

    关键是这句,如果验证错误,那么会把信息报给

    <?= $form->field($model, 'recipient')->textInput() ?> 
    

    所以,要想form有提示,你得保证你表单中有这个字段。

    3 条回复
    回复于 2016-12-28 11:19 回复

    有啊.

    <?= $form->field($model, 'recipient')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
    

    , 就是不懂为什么不提示

    回复于 2016-12-28 11:49 回复

    你在验证那里打下断点,看走到那块没有。

    回复于 2016-12-28 11:51 回复

    ..好像是我的前台样式有问题..我刚刚弄到后台是. 这个验证是走到了的

  • 回答于 2016-12-27 19:08 举报

    debug调试一下这段代码

  • 回答于 2016-12-28 22:48 举报

    有两种解决方法:
    1、用验证规则验证
    在模型的验证规则中加入 ['recipient', 'exist', 'message' => '没有该收件人,请重新确认'],
    2、使用的自定义验证规则
    ['recipient', 'validateRecipient'],
    public function validateRecipient($attribute, $params)
    {

    if(!$this->hasErrors()){
        $data=Member::find()->where(['username'=>$this->recipient])->one();
        if(is_null($data)){
            $this->addError($attribute,'没有该收件人,请重新确认');
        }
    }
    

    }

    1 条回复
    回复于 2016-12-29 12:02 回复

    嗯嗯..我用的就是第二种验证方法.之所以没有显示错误,好像是我样式有问题,我弄到后台没有修改过的就可以显示了,谢谢回答

您需要登录后才可以回答。登录 | 立即注册
页面修改
总监

页面修改

注册时间:2016-11-01
最后登录:2020-01-19
在线时长:79小时33分
  • 粉丝7
  • 金钱1640
  • 威望10
  • 积分2530

热门问题