Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form doesn't validate and submit in yii

I try load form via ajax in CJuiDialog. The form was success loaded but when I submitted form or write text, form not validate and not submit.I tried set "true" fourth parameter in renderPartialbut after then dialog window didn't open. In console, I got error

$(...).dialog is not a function

In view I have this:

Yii::app()->clientScript->registerScript(
       "test",
       "jQuery.ajax({ 
                 type: 'POST',
                 url: '".$this->createUrl("/Site/ShowForm")."',
                 success: function(html){
                            $('#form-test').html(html);
                            });
                             ",
                          CClientScript::POS_READY); ?>
        <div id="form-test"></div>
<?php  $this->endWidget('zii.widgets.jui.CJuiDialog');

My actions in controller:

  public function actionShowForm()
{

            $model = new RegistrationForm;

             if(Yii::app()->request->isAjaxRequest )
              return $this->renderPartial('register', array('model' => $model),false,false);

    }       

          public function actionRegister()
{   
    $model = new RegistrationForm;
            $this->ajaxValidate($model);
            $model->attributes =$_POST['RegistrationForm'];
            if($model->validate())
            {
                $user = new User;
                $user->attributes = $model->attributes;
                if($user->save())
                echo 1;
            }
}

and my form (register.php)

<?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'register-form',
                //'enableClientValidation'=>true,
                'enableAjaxValidation' => true,
                'clientOptions'        => array(
                        //'validateOnSubmit' => true,
                        'validateOnChange' => true
                ),
                'action' => array('site/Register'),
            ));
            ?>

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



            <div class="row">
                <?php echo $form->labelEx($model, 'first_name'); ?>
                <?php echo $form->textField($model, 'first_name'); ?>
                <?php echo $form->error($model, 'first_name'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($model, 'last_name'); ?>
                <?php echo $form->textField($model, 'last_name'); ?>
                <?php echo $form->error($model, 'last_name'); ?>

            </div>
            <div class="row">
                <?php echo $form->labelEx($model, 'email'); ?>
                <?php echo $form->emailField($model, 'email'); ?>
                <?php echo $form->error($model, 'email'); ?>

            </div>
            <div class="row">
                <?php echo $form->labelEx($model, 'password'); ?>
                <?php echo $form->textField($model, 'password'); ?>
                <?php echo $form->error($model, 'password'); ?>

            </div>
            <div class="row">
                <?php echo $form->labelEx($model, 'Repeat password'); ?>
                <?php echo $form->textField($model, 'repeat_password'); ?>
                <?php echo $form->error($model, 'repeat_password'); ?>

            </div>
            <div>
                <?php echo $form->textField($model, 'verifyCode'); ?>
                <?php $this->widget('CCaptcha'); ?>
                <?php echo $form->error($model, 'verifyCode'); ?>
            </div>



            <div class="row buttons">
                <?php
                echo CHtml::ajaxSubmitButton('Register', $this->createUrl("/Site/Register"), array(

                    'type' => 'POST',
                    'dataType' => 'json',
                    'success' => 'js:function(data){       
                                   if(data == 1){
                                    window.location ="' . $this->createUrl('site/index') . '"
                                   }
                         }',
                ));
                ?>

                     </div>

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

How Can I fix this problem?

like image 665
YuriiChmil Avatar asked Jul 09 '15 07:07

YuriiChmil


2 Answers

In your controller, where you check if the request is Ajax, you should be doing the Ajax validation in there.

like image 125
Igbanam Avatar answered Oct 29 '22 08:10

Igbanam


I found solution! I added

Yii::app()->clientScript->scriptMap = array(
                'jquery.js'     => false,
                'jquery.ui.js'  => false,
                'jquery.yiiactiveform.js' => false,
            );

in my actionShowForm.

like image 27
YuriiChmil Avatar answered Oct 29 '22 06:10

YuriiChmil