Activeform: Getting Required Fields

Hello, colleagues!

Does anyone know, is there any way of getting list of required/error fields on client side dynamically without ajax? I want to make block like "This fields are left to fill: phone, email, …" and refresh it during form filling.

As addition, I want to manage submit button programmatically and activate it only if all fields are filled correctly.

Maybe, you can suggest some way to extend jQuery.yiiactiveform.js to do this feature?


CActiveForm.errorSummary

might helps you

To validate the input at client side, you need to:

  • Define the rules of each attribute at your model.

  • Define the form using widget and set enable client validation, i.e




    $form = $this->beginWidget('CActiveForm', array(

        'id'=>'employees-form',

        'enableAjaxValidation'=>FALSE, //---depends on your need

        'enableClientValidation'=>TRUE, //---mandatory
[/quote]


        'clientOptions'=>array('validateOnChange'=>TRUE, 'validateOnSubmit'=>TRUE), //---mandatory

        'htmlOptions'=>array('enctype'=>'multipart/form-data'),

    ));



  • Use the solution from Ahamed Rifaideen above to display the error message of validation, or in other format you can use

$form->errorSummary($model)

But, you need to pay attention on the rules. Some rules is not available for client side validation.

Hope it might helps.

No, it’s server method of CActiveForm and it shows only a list of error messages after submit or after ajax-request, but i need determine on client side which fields are already filled correctly and which ones not. I want to display form filling progress and list of fields, that has not filled by user.

Sorry, that was may bad… Use this one:




echo $form->error($model, 'attribute_1');

echo $form->error($model, 'attribute_2');



It would be better looking if you put those lines within div element with "form" as class name.




<div class="form">

    <?php

        echo $form->error($model, 'attribute_1');

        echo $form->error($model, 'attribute_2');

    ?>

</div>



It should be as per your need…

You can also try something more user friendly this:




<div class="form">

    <div class="row">

        <?php

            echo $form->textField($model, 'attribute_1');

            echo $form->error($model, 'attribute_1');

        ?>

    </div>

    <div class="row">

        <?php

            echo $form->textField($model, 'attribute_2');

            echo $form->error($model, 'attribute_2');

        ?>

    </div>

</div>



Thanks, this variant is closest to truth. But it return only error messages (text), not field list. Apparently, i should extend activeform.js in some way…