3

I'm new in Yii Framework. I want to check user exists or not with 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

error must looks like this

how can i do this ? thanks!

Mirjalal
  • 1,292
  • 1
  • 17
  • 40

2 Answers2

1

if you have added rule for unique in your model then just do

Enable Ajax validation in _form.php file

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

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

 $this->performAjaxValidation($model);
Yatin Mistry
  • 1,246
  • 2
  • 13
  • 35
1

add below line in your model class inside path/to/protected/models/modelname.php array('username', 'unique','on'=>'insert', 'message'=>'Username already exists'), in the public function rules() { } function. Here modelname in indicates your corresponding model file name.

If you want to show the error text to be shown without page load enable ajax submission like below in _form.php/corresponding form page

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