Yii Framework Forum: Ajax Validation Doesn't Work - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Validation Doesn't Work Rate Topic: -----

#1 User is offline   andrzej1_1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 67
  • Joined: 16-April 13

Posted 08 June 2013 - 01:26 PM

Hi. From two weeks i work in Yii. I have a big problem, ajax validation doesn't work! I include jquery, set all options but it doesn't help. I have that:
controllers/SiteController.php:
        public function actionContact()
        {
                $model=new ContactForm;
                if(isset($_POST['ContactForm']))
                {
                        $model->attributes=$_POST['ContactForm'];
                        if($model->validate())
                        {
                                $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
                                $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
                                $headers="From: $name <{$model->email}>\r\n".
                                        "Reply-To: {$model->email}\r\n".
                                        "MIME-Version: 1.0\r\n".
                                        "Content-type: text/plain; charset=UTF-8";

                                mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
                                Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
                                $this->refresh();
                        }
                }
                $this->render('contact',array('model'=>$model));
        }



views/contact.php:
<?php $this->title='Kontakt'; ?>
<?php $contact = new ContactForm(); ?>
<table cellspacing="0">
    <tbody>
        <?php $form=$this->beginWidget('CActiveForm', array(
                'id'=>'contact-form',
                'enableAjaxValidation' => true,
                'enableClientValidation'=>true,
                'clientOptions'=>array(
                    'validateOnSubmit' => true,
                    'validateOnChange' => true,
                    'validateOnType' => true
                ),
        )); ?>

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

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

                <tr>
                        <?php echo $form->labelEx($model,'name'); ?>
                        <?php echo $form->textField($model,'name'); ?>
                        <?php echo $form->error($model,'name'); ?>
                </tr>

                <tr>
                        <?php echo $form->labelEx($model,'email'); ?>
                        <?php echo $form->textField($model,'email'); ?>
                        <?php echo $form->error($model,'email'); ?>
                </tr>

                <tr>
                        <?php echo $form->labelEx($model,'subject'); ?>
                        <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>
                        <?php echo $form->error($model,'subject'); ?>
                </tr>

                <tr>
                        <?php echo $form->labelEx($model,'body'); ?>
                        <?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50,'class'=>'textarea2')); ?>
                        <?php echo $form->error($model,'body'); ?>
                </tr>

                <?php if(CCaptcha::checkRequirements()): ?>
                <tr>
                        <?php echo $form->labelEx($model,'verifyCode'); ?>
                        <div>
                        <?php $this->widget('CCaptcha'); ?>
                        <?php echo $form->textField($model,'verifyCode'); ?>
                        </div>
                        <div class="hint">Please enter the letters as they are shown in the image above.
                        <br/>Letters are not case-sensitive.</div>
                        <?php echo $form->error($model,'verifyCode'); ?>
                </tr>
                <?php endif; ?>

                <tr>
                        <?php echo CHtml::submitButton('',array('class'=>'submit submit_okay')); ?>
                </tr>

        <?php $this->endWidget(); ?>   
    </tbody>
</table>



views/layout/main.php:
 ...
<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
 ...


Validation work only after submit. In debug tool there aren't any ajax request. Where I make mistake?
0

#2 User is offline   Ghanshyam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 18-March 13
  • Location:Ahmedabad

Posted 08 June 2013 - 10:59 PM

Hi,

Please to change like that...

controllers/SiteController.php:
$this->render('contact',array('model'=>$model),false,true);



views/contact.php:

<?php $form=$this->beginWidget('CActiveForm', array(
                'id'=>'contact-form',
                'enableAjaxValidation' => true,
                'enableClientValidation'=>true,
                'clientOptions'=>array(
                    'validateOnSubmit' => true,
                    'validateOnChange' => true,
                    'validateOnType' => true,
                    'afterValidate' => 'js:checkErrors'   //add this line
                ),
                'htmlOptions' => array(                      //add this line
                    'onsubmit' => "return false;",
                ),
        )); ?>


<script type="text/javascript">

     function checkErrors(form, data, hasError) {
             if (hasError != true) {

             }
     }
</script>


Thanks
Ghanshyam
0

#3 User is offline   andrzej1_1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 67
  • Joined: 16-April 13

Posted 09 June 2013 - 02:54 AM

It works but only when I click submit. Besides I think in Yii ajax validation will be work without writing any line of code. Is there any way to change this to work always when user entered data?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users