ErnestoP
ErnestoP

Reputation: 93

Angular ng-maxlength marks field invalid when no explicit value is set and field contains any characters

I'm working on dynamically creating a form with Angular (1.3b17). I'm storing my form's schema in an object, and including several properties that define my fields' behavior within that schema, for instance a field will have "type" (text, textarea, check, etc) and "maxlength" properties. I then set the angular attributes for my elements to the respective property for my object (ng-maxlength="field.maxlength" for instance).

I've noticed something a bit weird, and I wanted to see if this is working as intended. When the attribute is set, but no value is specified (ng-maxlength="") then $error is set to true immediately after typing anything, as it expects a maximum length of 0. Similarly, using an expression (ng-maxlength="field.maxlength") behaves in the same way if the property isn't set.

I've created a simple Plunker so you can see what I'm referring to. I would personally expect there to be no maxlength checks if the value isn't set, even if the attribute is present.

<input id="txtName" ng-model="myval" type="text" name="name" ng-maxlength="" /><br />
{{myForm.name.$error}}

Or

<input id="txtName" ng-model="myval" type="text" name="name" ng-maxlength="someundefinedvalue" /><br />
{{myForm.name.$error}}

Of course, I could work around this by setting ng-maxlength to something large, but it doesn't seem ideal. Is there a reason for this behavior? Or could it be a bug?

Upvotes: 3

Views: 416

Answers (1)

kentcdodds
kentcdodds

Reputation: 29031

Looks like as long as you define the directive on the form control the validator is applied to it unfortunately. So, the answer to your question is, it's how it's always been, it might be considered a bug, but at this point it would be a breaking change and it's unlikely to be changed.

Upvotes: 2

Related Questions