View file error message

I am new to Yii Framework. I want to check if a user exists or not using ajax.

view file

<form method="post" action="<?php echo $baseUrl;?>/persons/login" role="form"><br>
    <div style="display: block;margin-left:14%">
        <div>İstifadəçi adı</div>
        <div>
            <input type="text" name="istiad" autocomplete="off" role="textbox" tabindex="1">
        </div>
        <div>Parol</div>
        <div>
            <input type="password" name="parol" autocomplete="off" role="textbox" maxlength="20" tabindex="2"><br>
        </div>
        <?=CHtml::button('daxil ol', ['submit'=>['persons/login', 'i'=>1]]); ?>
    </div>
</form>

      

EDIT

the error should look like this

How can i do this? thank!

+3


source to share


2 answers


if you added a rule for unique in your model, then just

Enable Ajax validation in _form.php file

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

      



In your controller actionCreate () and actionUpdate () Uncomment the following line if AJAX validation is needed

 $this->performAjaxValidation($model);

      

+1


source


add below line to model class inside path / in / protected / models / modelname.php array('username', 'unique','on'=>'insert', 'message'=>'Username already exists'),

in function public function rules() { }

. Here modelname specifies your corresponding model file name.

If you want to show the error text to be shown without loading the page, enable the ajax submission as shown below in the _form.php / page of the corresponding form



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

      

+1


source







All Articles