two ng-model with the same value on a form - angularjs

I have a simple form with two text inputs like below:
<form>
// this is visible in mobile view
<input id="mobileView" type="email" required ng-model="myValue" />
// this is visible on desktop view
<input id="desktopView" type="email" required ng-model="myValue" />
</form>
my question is does doing so violate angular form validation? because both of the inputs are in DOM and in one view one of them have value and in other view it doesn't have any value. does this break angular's validation?

Your code is correct and use ng-if it handle the DOM elements.
<form>
// this is visible in mobile view
<input id="mobileView" type="email" ng-if="condition for mobile view" required ng-model="myValue" />
// this is visible on desktop view
<input id="desktopView" type="email" ng-if="condition for desktop view" required ng-model="myValue" />
</form>

Related

AngularJS form validation testing

Sometimes forms become very complicated and it is impossible to test every case manually after code changes.
I already have unit testing with karma on the project.
Is there any tools or best practices how to test AngularJS form validation with jasmine and karma?
For example how can I test such form with jasmine and karma automatically?
<form name="appForm" novalidate>
<div>
Username: <input type="text" ng-model="data.username" name="username" ng-maxlength="15" required />
</div>
<div>
Email: <input type="email" ng-model="data.email" name="email" required />
</div>
<div>
Age: <input type="number" ng-model="data.age" name="age" />
</div>
<div>
<button ng-click="submit()" ng-disabled="appForm.$invalid">Submit</button>
</div>
</form>
It depends on what you actually want to make sure when testing form validation.
If you want to be sure invalid form will not be submited, then it is one case. And I don't see problems with this case.
If you want to be sure that appropriate messages are displayed for invalid fields, then, for example, you can make a directive, that is aware of all your possible field restrictions ('required', 'ng-maxlength', 'url', etc.) and is responsible for displaying appropriate error messages. So you will need to create tests only for this directive.
Example:
<input type="text" ng-model="data.username" my-directive name="username" ng-maxlength="15" required />
myDirective is aware of required and ng-maxlength restrictions, that were put on the field, & it is responsible for displaying appropriate error messages for invalid state of the field.

Angular input validity property without a form

I have inputs in a web page without the form tag (useless to me).
How can I get their validity status inside the HTML ? This
<input name="myInput" type="text" ng-pattern="/^[0-9]{13}$/">
<input type="button" ng-show="myInput.$valid">
doesn't work.
I'm afraid that won't work without wrapping it in a form as you need to access those fields via the form's controller.
<form name="myForm">
<input name="myInput" type="text" ng-pattern="/^[0-9]{13}$/">
<input type="button" ng-show="myForm.myInput.$valid">
</form>
Should work.
If you're unable to use the form tag for any reason, you'll have to wire that up manually.

AngularJS - input text element deselect event

Suppose I have the following setup:
<form>
<input type="text" id="text1">
<input type="text" id="text2">
</form>
In AngularJS, is there any way for me to determine when, say, the user deselects #text1, for example by clicking #text2, or clicking somewhere else on the screen? I am aware the ng-change lets me listen to changes in the value of #text1 itself, but I see no way to determine when the user actually leaves the field.
You can use ngBlur for this
https://docs.angularjs.org/api/ng/directive/ngBlur
<form>
<input type="text" id="text1" ng-blur="iHaveLostFocusDoSomethingWithIt()">
<input type="text" id="text2">
</form>

Is ng-model needed when using ng-disabled and $invalid on a form?

I'm using AngularJS and have a form where I want the Submit button to be disabled if some fields are not filled in.
The standard way seems to be the following:
<form ng-app name="myForm">
<label>Name</label>
<input type="text" name="name" ng-model="form.name" required>
<input type="submit" ng-disabled="myForm.name.$invalid">
</form>
http://jsfiddle.net/YMSRU/
However, if I omit the model from the input field the validation doesn't work and I don't need any models on my input fields (I submit my form using the ngUpload directive so it's actually sent to the form action in an iframe).
Is there any solution or should I add random models just to make the validation work? It seems like a bad work-around.
You could simply do the invalid check at the form level, then no need to define a model for each input:
<form ng-app name="myForm">
<label>Name</label>
<input type="text" name="name" required>
<input type="submit" ng-disabled="myForm.$invalid">
</form>
You are missing your model at your test input tag : ng-model="form.name"

not all POST data are received/handled by Cake

I have a PresentationsController which handles some POST action form. In this form I have data related to Presentation such as:
<input name="data[Presentation][title]" class="init-focus span4" type="text" id="PresentationTitle" required="required">
and those fields are handled correctly by controller. But PresentationModel hasMany Subject. So I want to include some presentation subjects in form. I did it like this:
<input name="data[Subject][0][subject]" disabled="disabled" class="subject" maxlength="255" type="text" id="Subject0Subject" required="required">
<input name="data[Subject][1][subject]" disabled="disabled" class="subject" maxlength="255" type="text" id="Subject0Subject" required="required">
But those data are not handled by Cake - I tried var_dump($this->request->data) in Controller but they are missing... for some reason Cake just ignores those data...
I am generating inputs dynamicalyy with jquery but it inputs are added correctly to form - I can see them in my browser html elements viewer:
<input name="data[Subject][0][subject]" maxlength="255" type="text" id="Subject0Subject" required="required">
<input name="data[Subject][1][subject]" disabled="disabled" class="subject" maxlength="255" type="text" id="Subject0Subject" required="required">
The above is what I view in html elements viewer - the first input is added "inline" from php and second is added dynamically with jquery. And only the first one is visible after POST.
When you set an input to disabled="disabled" it is NOT submitted. That goes for normal HTML and is not something CakePHP specific.
According to W3Schools.com:
Disabled elements in a form will not be submitted.

Resources