Adding attribute without value to htmlOptions in Yii 1.x

Copy of Stack Exchange question, in hope to find some more specific solution.

How to force Yii 1.x to render HTML5’s attribute without value, like autofocus or readonly in htmlOptions array passed to anything? So, as a result I’d get for example:


<input autofocus>

I tried the idea of setting array(‘autofocus’=>TRUE); or array(‘autofocus’=>‘autofocus’);, but this does not work. It renders either <field autofucus=“1”> or

and both are not, what HTML5 expects, so they don’t work.

I also tried (I don’t know, why did I expect this to work) array(‘autofocus’=>NULL);, but such attributes are now removed by Yii 1.x and are not rendered at all.

I also tried a stupid workaround of setting this value using jQuery. But, that scares me even, if I think about it. There’s got to be a better solution to this.

This seemed obvious, but I failed on finding proper answer, here on in other sources.

inherit CHtml and add the functionality yourself or you can write the html instead

According to http://www.w3.org/TR/html5/forms.html#the-readonly-attribute readonly attribute can be added in xhtml style (readonly=“readonly”) and is valid if so. ‘autofocus’ is boolean attribute as well so it should work the same way.

Maybe silly question but are you sure it doesn’t work?

This can help you - http://www.yiiframew…tesValue-detail

If you set it to false it will render just


autofocus

[size="2"] instead of [/size][size="2"]


autofocus="autofocus"

[/size]

Turns out, this isn’t that silly question after all! :expressionless:

I have made some additional tests today, and it seems, that using old, XHTML way of <input autofocus=“autofocus”> works just fine. Includning IE11! I simply cannot tell you, what I did yesterday, that I didn’t see this? Code-ghosts or something.

I thought, that if “readonly” and “disabled” attributes were only back-ported to HTML5 from HTML4/XHTML, they’re respected in all possible ways. But, there was no “autofocus” before HTML5 and basing on this, I incorrectly assumed, that this will work only in HTML5-way of <input autofocus>.

Thanks for pointing this out.

That is not possible. CHtml is called internally by many of my extensions. In the mentioned situation, I’m using Yii Booster 3 to render all form elements in my application. I’d have to resign using Yii Booster at all or would have to rewrite most of its components.

maybe I did not understand it, but it is possible to inherit the CHtml and have your new methods added on new class, you still use CHtml in your other exts without changing anything


class MyCHtml extends CHtml

{

    public static function textField()

    {

        return 'foobar';

    }

}


// you can still call your good old

CHtml::textField()

// as well as new method

MyCHtml::textField()

Have you been using Yii-Booster or any other Twitter Bootstrap clone for Yii? It introduces a lot of changes, how forms are constructed, form elements used etc. to bring you all the goodies you have from Twitter Bootstrap. It isn’t an easy way (nor it isn’t advised) to mix CHtml methods (and form elements rendered this way) with Yii-Booster methods (and form elements this way). Not only this would have some strange implications to the code (Booster form elements and other widgets are constructed different way, using different data sets), but also it would look ugly (mixing Twitter Bootstrap-styled form elements with those rendered by CHtml).

So, while technically you’re of course right, overall effect isn’t acceptable by me. But, thanks for trying to help!