Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder does not play well with required in 1.3 and ng-required in 1.2 #6

Open
tdakhla opened this issue Dec 3, 2014 · 7 comments

Comments

@tdakhla
Copy link

tdakhla commented Dec 3, 2014

In 1.3 I'd expect a required error key to be set when the input initially loads, both when using required and ng-required. Only when you type in a character and remove it will the required error key be set:
http://jsfiddle.net/eL55yebe/1/

In 1.2, using required works fine, but ng-required doesn't set the required error key:
http://jsfiddle.net/at4jkx46/

Note: in all cases, removing the placeholder directive allows the required error key to be set on load.

@tdakhla tdakhla changed the title Placeholder does not play well with required in 1.3 Placeholder does not play well with required in 1.3 and ng-required in 1.2 Dec 3, 2014
@CullenShane
Copy link

We've experienced the same issue.

Because the placeholder text is being set on the element's val, the required directive sees it as a view value. So all our required inputs with placeholders are being set as valid when they aren't.
https://github.com/angular/angular.js/blob/v1.3.15/src/ng/directive/validators.js#L12

@CullenShane
Copy link

I ended up writing a directive that overrides the angular 'required' directive and adds a situation where if the placeholder attribute is equal the viewValue (element.val()) it marks the input as invalid.

https://gist.github.com/CullenShane/ebe103df0790a0d86262

@fidian
Copy link
Contributor

fidian commented Apr 8, 2015

I've changed the code in the issue-5 branch and I don't use the value in the scope any longer. Angular appears to use the scope value as the source of truth, thankfully, and so maybe this problem is resolved? I've tried the updated code in the fiddles provided and have seen the correct results.

@CullenShane
Copy link

I assume the previous comment was meant for #5 ?

@fidian
Copy link
Contributor

fidian commented Apr 8, 2015

No. It applies to this issue. As I mentioned, I don't use scope any longer. This issue cropped up because I did use scope a tiny amount. I also tested the issue-5 branch against the two fiddles mentioned at the start of this issue and I believe the code I added will solve both problems at once.

@CullenShane
Copy link

Ah, I understand. I will try that branch tonight and get back to you.

@CullenShane
Copy link

@fidian I have tried the issue-5 branch, and it does correct the styles issue, it does not correct the validation issue.

The issue is only present on pristine forms. Before the user types anything into the form, it should be invalid because input is required.

I've updated the jsfiddle provided earlier to demonstrate the difference in behavior of vanilla vs with this project included. https://jsfiddle.net/y78ohbuw/1/embedded/result/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants