Getting rid of * for required labels

In CActiveForm, the


$form->labelEx($model,'username');

produces a label. It also includes an asterix [color="#FF0000"]*[/color] if the field is required.

Is there a way to prevent the asterix from being included?

follow-up question:

The errorMessage is included with


style="display: block;"

How do I get rid of that style?

try $form->label()

You can just call


$form->label($model,'username');

instead, the whole point of labelEx is to determine if the attribute is required and append that required span.

If you wanted it to work differently you would need to extend CActiveForm::labelEx.

Edit: too slow.

You are right. Both of you! Thanks!

By the way, … anybody up for that second question: how do I get rid of the


style: display: none

that JS puts in there when a new error has been found at validation?

Got it!

In CActiveForm Widget

By setting


hideErrorMessage

to true in


$htmloptions

, one can prevent the style


display: block

from being inserted


$form->error($model,'username', array('hideErrorMessage' => true));

see here

That’s not a very good idea. Why?

Using a red asterisk for required fields is universally recognized.

But suit yourself.

At least you know how to do this now. ;)

True.

But it so happens I have a number of forms where each and every field is required. So the red asterix looks weird.

Unless the webapp end users are nerds, you’ll want to keep them :)

I’ve seen some people submitting forms and thinking the click of the button is enough: ie they don’t wait for the submission results, and they don’t pay attention to eventual validation problems. Eventually, I added alert and confirm boxes on submit, before validation, and after validation / before save :)

PS at the same time, huge form with 50 fields and related models in 7 steps :)

How does that work precisely?

Something along these lines:


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

    'id'=>'some-form',

    'enableClientValidation'=>false,

    'enableAjaxValidation'=>true,

    'clientOptions'=>array(

        'validateOnSubmit'=>true,

        'validateOnChange'=>false,

        'beforeValidate'=>"js:function(form) {

            alert('Going to validate. Please click Ok and wait few moments.')

            return true;

        }",

        'afterValidate'=>"js:function(form, data, hasError) {

            if(hasError) {

                alert('We have detected some input errors and has not saved your data. Please click Ok to correct them.');

                return false;

            }

            else {

                if(confirm('We have validated your input and we are ready to save your data. Please click Ok to save or Cancel to return to input.'))

                    return true;

                else

                    return false;

            }

        }",

    )

)); ?>

Interesting. Do yo mind if I steal the afterValidate from you? I am going to try it out now… :)

update:

Oh, looks good! :D

You’re welcome to use it as you like, it’s just an example anyway :)

For further reference, the API doc is great: http://www.yiiframework.com/doc/api/1.1/CActiveForm#clientOptions-detail

Hello, Im trying to do the same, but in my aplication i have yiiBooster extension.

Here is my code:




$usuario= new UsuarioForm();

                    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

                        'id'=>'verticalForm',

                        'action'=>Yii::app()->request->baseUrl.'/site/login',

                        'enableClientValidation'=>true,

                        'enableAjaxValidation'=>true,                        

                        'clientOptions'=>array(

                                'validateOnSubmit'=>true,

                        ),                        

                    )); 


                    echo $form->textFieldRow($usuario, 'nombre'); 

                    echo $form->passwordFieldRow($usuario, 'password'); 

                    $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit','type'=>'primary', 'label'=>'Login', 'size' => 'large')); 

                    $this->endWidget(); 



Any suggestion?

Thanks.

Actually, you can just set CHtml::beforeRequiredLabel, CHtml::afterRequiredLabel and CHtml::requiredCss manually somewhere in your code.