A
A
alex5e2014-03-21 22:58:40
Yii
alex5e, 2014-03-21 22:58:40

How to disable form submit button before ajax validation in Yii?

Good evening. Can you tell me how to make the form submit button in CActiveForm inactive until the ajax validation passes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Litvinov, 2014-03-21
@alex5e

You can use CHtml::submitButton instead of CHtml::ajaxSubmitButton
Example

div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'user-form',
        'enableAjaxValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'afterValidate'=>'js:function(form,data,hasError){
                        if(!hasError){
                                $.ajax({
                                        "type":"POST",
                                        "url":"'.CHtml::normalizeUrl(array("test/eleven")).'",
                                        "data":form.serialize(),
                                        "success":function(data){$("#test").html(data);},
                                        
                                        });
                                }
                        }'
        ),
)); ?>

        <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'=>64)); ?>
                <?php echo $form->error($model,'username'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($model,'age'); ?>
                <?php echo $form->textField($model,'age'); ?>
                <?php echo $form->error($model,'age'); ?>
        </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 buttons">
                <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
        </div>

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

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

<!-- AJAX update div element -->

<div id="test"></div>

M
mikitachu, 2014-03-22
@mikitachu

www.yiiframework.com/doc/api/1.1/CActiveForm#clien...
In beforeValidate block the button (for example, set the disabled attribute to it), in afterValidate return everything back, that is, remove the added attribute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question