1

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 this idea of setting array('autofocus'=>TRUE); or array('autofocus'=>'autofocus');, but this does not work. It renders either <field autofocus="1"> or <field autofocus="autofocus"> 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.

Community
  • 1
  • 1
trejder
  • 17,148
  • 27
  • 124
  • 216
  • 1
    autofucus ? I take it this was misspelt just in this post, right? – crafter Mar 05 '15 at 07:42
  • @crafter Sure thing. Thanks for pointing out. Just fixed. BTW: You can use `edit` link on other's posts as well from time to time. That's how SE works! :> – trejder Mar 05 '15 at 08:35
  • Got that, just wanted to make sure it was not the cause of your issues. I'll delete my comments shortly. – crafter Mar 05 '15 at 11:04

3 Answers3

1

I think you can't do this with yii. You need to implement this by yourself. Yii provides CActiveForm#textField for normal use and you need to use text field in advanced mode. I think there is no way for doing this, except writing plain html <input> tag. You can write html <input> and assign id and name similar to yii.

hamed
  • 7,939
  • 15
  • 60
  • 114
  • That's bad, because I'm using Bootstrap, not pure Yii's `CActiveForm`, so this will include a lot of work. But... if there is no other option, then I'll have to follow this. – trejder Mar 04 '15 at 13:52
  • 1
    I think you have two way: 1. Writing plain html input tag. 2. using jquery(as you mentioned in your question). And I think the first way is better. – hamed Mar 04 '15 at 13:55
1

According to Maurizio Domba Cerin, a Yii Framework 1.x forum admin, you should use property CHtml::renderSpecialAttributesValue (available since Yii 1.1.13) like this (set it to FALSE):

CHtml::$renderSpecialAttributesValue = FALSE;

This will tell CHtml class to render all fields' attributes without value in the way, it was introduced in HTML5 (i.e. <input autofocus>) instead of "old" XHTML way (<input autofocus="autofocus").

trejder
  • 17,148
  • 27
  • 124
  • 216
1

I've just found out that it's possible in some way. All modern browsers treat following lines equally:

<input autofocus>
<input autofocus="autofocus">

So, basically, you cannot add key-only attribute to Yii, but you can add either 'autofocus' => 'autofocus' or 'autofocus' => trueand it will behave in the same way. And it works for CHtml::activeTextField as well as for usual input.

The Godfather
  • 4,235
  • 4
  • 39
  • 61
  • The fact, that browsers accept it does not always mean, that HTML5 validity checkers will not see this as a bug. I don't remember details right know and I don't have the possibility to check this, but I've been using `autofocus="autofocus"` for years and if I'm not mistaken, then this is not HTML5 valid. I think, that `` is required. But, I may be wrong. – trejder May 24 '16 at 14:27
  • At least, this approach worked for me, there were no any errors, so I'm pretty happy with it. – The Godfather May 24 '16 at 21:05
  • As I said, I may be wrong. However, did you put your website through tools like HTML5 validator, CSS validator etc.? The fact, that your console does not report any errors (i.e. your website is _syntactically_ correct) doesn't always mean, that your site won't fail on validator (i.e. will be _semantically_ correct). If I'm not mistaken, your solution is syntactically correct, but semantically incorrect. See for yourself in validator, if you wish. I have at least a dozen of my own pages that does not report any error in console while having hundreds of them in semantic validator! – trejder May 26 '16 at 08:36