What is the proper way to submit hidden form information in AngularJS? - angularjs

I'm working on a site where a user can submit several different kinds of forms. I would like to include the type of form submitted (basic, advanced, other). I've read that using hidden fields with AngularJS is possible, but not recommended. Rather than finding a hack to solve the problem, I'd prefer to do things right. What is the proper way to submit information that does not need to be displayed to the user but should be included with a submission?
Here's the HTML for the form:
<form name="myForm">
<div class="control-group">
<label>Name</label>
<input type="text" name="name">
</div>
<label>Description</label>
<textarea name="description"></textarea>
<button ng-click="editProject.save()" class="btn btn-primary">Save</button>
<!--{{formselection}}-->
</form>

If you use ng-model the hidden fields are in the object memory context, only show the data to the user fill.
HTML:
<form name="myForm">
<div class="control-group">
<label>Name</label>
<input type="text" name="name" ng-model="form.name">
</div>
<label>Description</label>
<textarea name="description" ng-model="form.description"></textarea>
<button ng-click="editProject.save(form)" class="btn btn-primary">Save</button> <!-- Pass the form to the method in controller-->
</form>
Controller:
$scope.form = {
hiddenField1: "anyValue",
hiddenField2: "anotherValue"
};
$scope.editProject = {save: function(form){
//http request given the form, this contain the name, description and the two hidden field
}}
http://plnkr.co/edit/fjnGc4Q4f8ZfwhupkOdR

Related

How to obtain values from input fields with Angular JS after ng-repeat

I'm new in AngulsrJS and I've got following code:
<form ng-if="editorCtrl.edit" class="form-horizontal" role="form" ng-submit="editorCtrl.saveEntry()">
<div class="form-group">
<div class="col-md-6" ng-repeat="(key,field) in editorCtrl.editEntry">
<label>{{key}}</label>
<input type="text" class="form-control" value={{field}} />
</div>
</div>
<!-- ng-model="editorCtrl.toSave[key]" ng-value="{{field}}" that part was in input attributes -->
<div class="form-group">
<div style="padding-left:110px">
<input type="submit" value="Update selected entry" ng-disabled="form.$invalid" class="btn btn-primary"/>
</div>
</div>
</form>
Now I need to obtain values from input fields. I tried to use ng-model="editorCtrl.toSave[key], but it's not working in a correct way.
Any suggestions how to resolve this situation ?
if you can consider no necesary the "toSave" object maybe you can use the 2-way data binding properly only using editEntry Object :
<input type="text" class="form-control" value={{field}} ng-model="editorCtrl.editEntry[key]"/>
In this way after a submit you will get editEntry with the fields modifieds (or not), here is an example
https://jsfiddle.net/pv8qrwty/1/
run the example and if you modified the fields after you press the submit button it will be displayed in your browser console
hope this help ! and sorry for my english !

What is the correct way to validate all form fields on submit when using ng-repeat for dynamic form?

I have a form with some fields (build with ng-repeat, so I dont know how much). I added to the submit button
ng-disabled=form.field.$invalid
and added to all the fields "required" but the ng-disabled work just for this last field. my code:
<form name="form" novalidate>
<div ng-repeat="q in questions">
<textarea name="answer" type="text" ng-model="answers[$index]" required></textarea>
</div>
<button ng-disabled="form.answer.$invalid || form.answer.$pristine" type="submit" ng-click="submit(q)">'Submit'</button>
</form>
how can I validate the user change all fields? thanks!
That is because all your inputs have the same name, hence the behavior due to the form controller instance will have just one property with the name answers and it will point to the last ng-model with that name in the form. Instead provide different names for your inputs using ng-attr-name with probably appending $index and use $invalid property of the form, form will be invalid as long as one of the ng-model(s) (with constraints) within the form is invalid. So just disable the button until the form is invalid since it is a catch-all property on the form controller.
<div ng-repeat="q in questions">
<!-- Provide dynamic form names -->
<textarea ng-attr-name="answer{{$index}}" type="text"
ng-model="answers[$index]" required></textarea>
</div>
<!-- Use form.$invalid -->
<button ng-disabled="form.$invalid || form.$pristine"
type="submit" ng-click="submit(q)">Submit</button>
angular.module('app', []).controller('ctrl', function($scope) {
$scope.questions = ["q1", "q2"]
$scope.answers = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<form name="form" novalidate>
<div ng-repeat="q in questions">
<textarea ng-attr-name="answer{{$index}}" type="text" ng-model="answers[$index]" required></textarea>
</div>
<button ng-disabled="form.$invalid || form.$pristine" type="submit" ng-click="submit(q)">Submit</button>
</form>
</div>

AngularJS - Where can I find, in the framework, the logic behind the "required" and not "ng-required" attribute?

The question is as simple as it is described in the "Title" :)
I just want to be able to put a break-point in the non-minified version of the AngularJS framework and see the logic that is executed when you try to submit a form that has an empty input with the "required" attribute. This logic should normally check if the input is not empty. The "this.$isEmpty = function(value) {...}" or "requiredDirective" are not hit when you click submit!
E.g. from AngularJS official site (https://docs.angularjs.org/guide/forms):
<form name="form" class="css-form" novalidate>
Name:
<input type="text" ng-model="user.name" name="uName" required=""/>
<br/>
<div ng-show="form.$submitted || form.uName.$touched">
<div ng-show="form.uName.$error.required">Tell us your name.</div>
</div>
<input type="submit" ng-click="update(user)" value="Save"/>
</form>

Angular JS Process form before submit

I have a scenario as follows :
I have one searchbox and the user can enter two types of queries
Scenario One the Search Starts with i.e. A1234
Scenario Two the search Starts with i.e. B1234
Depending on one or two above I want to hit a different webservice.
Whats the best way to distinguish the search on the above input. Is it when the user clicks on my ng-submit I use a regular expression ?
Or should I have a scope variable on .watch() to find out as the user is typing ?
<form role="form" ng-submit="searchForm.$valid && searchCode()" name="searchForm" novalidate>
<div class="input-group">
<input type="text" class="form-control" ng-model="searchQuery" name="searchQuery" required autocomplete="off" spellcheck="false" focus>
<input type="submit" value="Search" class="btn btn-default btn-sarch-bar">
</div>
</form>
If the search is activated upon submit-button click, then you can handle the login in the submit function, and hit the proper webservice accordingly. something along these lines:
<form role="form" ng-submit="submit(searchForm.$valid, data)" name="searchForm" novalidate>
<div class="input-group">
<input type="text" class="form-control" ng-model="data.searchQuery" name="searchQuery" required autocomplete="off" spellcheck="false" focus>
<input type="submit" value="Search" class="btn btn-default btn-sarch-bar">
</div>
</form>
submit function:
$scope.submit = function(isValid, data) {
if(!isValid) return;
//check data.searchQuery and access the appropriate webservice here
}
If you're using angularjs 1.3, and you'd like to perform a search upon user interaction with the input, you might be interested in adding a directive to the input element, and use $asyncValidators
there's a great tutorial by yearofmoo regarding forms and validators using directives.

Doing form events with Angular directive

I have custom html5 error message for input, which changes validation error text in chrome.
<input
oninvalid="setCustomValidity('It's custom message!')"
onchange="try{setCustomValidity('')}catch(e){}">
How can I do this with Angular directive?
Updated
Let's say, I want type <input custom-validity> instead of this.
You can learn all about doing form validation the Angular way in their documentation.
You don't need to create your own directives, because angular already has great form validation support built in.
Below is an example how to use the $dirty and $invalid attributes to show or hide validation messages. 'dirty' means that the form has been modified by the user.
<div ng-app="app">
<form name="myForm" novalidate>
<p>
<label>Name
<input type="text" name="name" ng-model="name" required>
<span ng-show="myForm.name.$invalid && myForm.name.$dirty">
Name required
</span>
</label>
</p>
<p>
<label>Email
<input type="email" name="email" ng-model="email">
<span ng-show="myForm.email.$invalid && myForm.email.$dirty">
Put a valid email
</span>
</label>
</p>
<input type="submit" value="Submit" ng-disabled="myForm.$invalid">
</form>
</div>
You can also style the valid/invalid fields using a style rule like this:
form input.ng-invalid.ng-dirty { ... }

Resources