8

Let's say I have something like:

{{input value=someModel }}

And then I want to add the simple required HTML 5 attribute to the input.

How would I do that?


Note that I tried the following variations without success:

{{input value=someModel required }} <!-- doesn't parse -->

{{input value=someModel required='required' }} <!-- doesn't render the attribute -->

{{view Ember.TextField valueBinding=someModel 
    required='required' }} <!-- doesn't render the attribute -->

<input required {{bindAttr value=someModel}}
     /> <!-- doesn't update the model, as expected -->

Update: This question was for Ember 1.0.

Meligy
  • 35,654
  • 11
  • 85
  • 109

3 Answers3

8

I'm using Ember version 1.5.1 and required="required" seems to work fine now. This markup:

{{input class="form-control" value=firstName autofocus="autofocus" required="required"}}

...renders this:

<input id="ember392" class="ember-view ember-text-field form-control" autofocus="autofocus" required="required" type="text">
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
5

To globally add support for additional attributes you can reopen Ember.TextField

http://emberjs.com/api/classes/Ember.TextField.html

Trek Glowacki
  • 3,621
  • 1
  • 17
  • 16
  • 3
    This is horrible! (which is not your fault, thanks for pointing at it) - Any idea why it's like that? and whether there is a way to make it open for whatever I add? – Meligy Aug 12 '13 at 14:49
  • Not all browsers support `required`, so the framework ships with a minimal set, letting developers add additional attributes if they know they will target only browsers that support it. – Trek Glowacki Aug 12 '13 at 20:17
  • 2
    This might be a topic for Discourse, but really, I just think there is no point/benefit in limiting what the user can do with their markup (as long as it's not code, e.g. not Handlebars expressions). I'm guessing this is a side effect of some design limitation in the library, if it's meant to be this way as a feature, that'd be nothing but adding friction IMHO. – Meligy Aug 12 '13 at 22:24
5

First you need to add support to the required attribute:

Ember.TextSupport.reopen({  
    attributeBindings: ["required"]  
}) 

Then in your view:

{{view Ember.TextField required="required"}}
Mohamed Alaa
  • 104
  • 1