yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

activelabel's htmloptions doesn't honor ('id'=>false) #559

Closed jazztickets closed 12 years ago

jazztickets commented 12 years ago

inside /framework/web/helpers/CHtml.php around line 1145, the function is missing the code that appears in textarea, inputfield and others:

if(!isset($htmlOptions['id']))
    $htmlOptions['id']=self::getIdByName($name);
else if($htmlOptions['id']===false)
    unset($htmlOptions['id']);
mdomba commented 12 years ago

Textarea,inputfield and others "must" have an ID, so there is a check if that is set and if not the ID is generated automatically... for label that is not the case.

jazztickets commented 12 years ago

That's not what's happening in my version of yii (1.1.10). Setting 'id'=>false removes the id attribute from the element, which is exactly what i want it to do. You can see in the tag() and renderAttributes() functions.

Why do they need an ID? Maybe for ajax, but i'm not using ajax.

mdomba commented 12 years ago

I'm not sure I understand you problem... please give more information... the code you use, the generated HTML and how should it look instead.

jazztickets commented 12 years ago
<div class="row">
    <?php echo $form->labelEx($model,'name', array('id'=>false)); ?>
    <?php echo $form->textField($model,'name', array('id'=>false)); ?>
    <?php echo $form->error($model,'name', array('id'=>false)); ?>
</div>

produces

<div class="row">
    <label id="" for="ContactForm_name" class="required">Name <span class="required">*</span></label>
    <input name="ContactForm[name]" type="text" />
    <div id="" class="errorMessage" style="display:none"></div>
</div>

Notice how the input field is the only one without id="". I was hoping that setting 'id'=>false would remove it completely for

. You're asserting that those tags must have an ID tag, and they get generated automatically if not present. Obviously that's not the case.

This behavior isn't documented anywhere. I ran into this issue because i have a comment form that can appear multiple times on a page (one comment form per image). I wanted to remove the id fields because Yii was generating duplicate values. You don't need ID attributes unless you're using ajax, which i'm not.

In any case since this probably won't get fixed so I just had to create unique IDs for each field, even though i shouldn't have needed to.

mdomba commented 12 years ago

Why are you adding the id=>false to the labelEx... just remove it... and no ID will be generated...

jazztickets commented 12 years ago

Okay, so that fixes it for labelEx, but $form->error still has the problem.

As you can see it's not very consistent between labelEx, inputfield, and error.

mdomba commented 12 years ago

for the input and error if the ID is not set they are automatically generated because that is used by the ajax validation

jazztickets commented 12 years ago
...
else if($htmlOptions['id']===false)
    unset($htmlOptions['id']);

The question here is: why is this line of code in Yii?

mdomba commented 12 years ago

I still don't see what is your problem here... the code above can be used if you don't want an ID for the input field... but the same cannot be done for the CActiveForm::error() because this is used by the ajax validation as I explained above....

In the end... why are you using CActiveForm at all... why not use the CHtml equivalents... note that the CHtml::error() do not generate the ID automatically.

jazztickets commented 12 years ago

Because id="" is not valid html according to W3C. That's the problem. I want to completely remove id="".

enableAjaxValidation is set to false. IDs shouldn't be needed if that's set to false.

CActiveForm can still be used if it was made more flexible. It shouldn't assume that we're always using ajax validation.