yii表单提交中的警告

时间:2016-09-08 12:54:21

标签: php yii

是否有任何方式只能在warning中获得yii

我有form

<?php
/* @var $this UserController */
/* @var $model User */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true,
     'enableClientValidation' => true,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>128)); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>128)); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'email'); ?>
        <?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>    

    <div class="row">
        <?php echo $form->labelEx($model,'name'); ?>
        <?php echo $form->textField($model,'name',array('size'=>50,'maxlength'=>50)); ?>
        <?php echo $form->error($model,'name'); ?>
    </div>


    <div class="row">
        <?php echo $form->labelEx($model,'mobile'); ?>
        <?php echo $form->textField($model,'mobile',array('size'=>20,'maxlength'=>20)); ?>
        <?php echo $form->error($model,'mobile'); ?>
    </div>


    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

我知道如果我想要unique手机号码,那么我必须在rule中为它定义唯一的Model,但我希望当输入已存在的手机号码时我就是获取warning消息,但允许form提交。

所以我试过这个

user.php的

 public function rules()
    {
        return array(
           array('mobile', 'checkDublicateExist', 'on'=>'createuser'),
        );
    }


    public function checkDublicateExist($attribute,$params)
    {            

        if(!empty($this->mobile)){
          $count = User::Model()->count("mobile=:mobile", array("mobile" => $this->mobile));
       if($count){
          $this->addError('mobile' 'This mobile already exist!');
          return true;
       }

    }
}

我通过传递此validation返回true,但这不允许我提交表单。我只想要警告用户

1 个答案:

答案 0 :(得分:0)

您可以像这样设置消息

Yii::app()->user->setFlash('notice', "This mobile already exist!");