replace ng-form with angular 5 form in hybrid application - angularjs

I have an AngularJS application with HTML as below.
<form name="completeForm">
<first-step></first-step>
<second-step></second-step>
<third-step></third-step>
<fourth-step></fourth-step>
<fifth-step></fifth-step>
</form>
Form will continue to next step after validating the current step by a button click event.
Each step directive HTML is implemented through ng-form tag as below.
<first-step></first-step> => <ng-form name="firstForm"></ng-form>
<second-step></second-step> => <ng-form name="secondForm"></ng-form>
<third-step></third-step> => <ng-form name="thirdForm"></ng-form>
<fourth-step></fourth-step> => <ng-form name="fourthForm"></ng-form>
<fifth-step></fifth-step> => <ng-form name="fifthForm"></ng-form>
I want to replace one of the steps (second-step) with an implementation using Angular 5, thus making it a hybrid application.
I should be able to replace the second-step in Angular 5. The HTML related to it is as below.
<form #secondForm="ngForm">
.....
<input type="text" #email="ngModel" name="email" [(ngModel)]="$scope.email" required />
<span *ngIf="!email.valid">please enter the input</span>
...
</form>
But the validation I have implemented on this Angular 5 step is not working. It is moving to the next step without validating on the
button click event. Form is always found to be valid.
Form Controller is not recognizing the secondForm defined as
<form #secondForm="ngForm">.
I tried to replace the form tag (as form at the parent level cannot have another form within it) with div tag as below but no impact at all.
<div ngForm #secondForm="ngForm">
Please could anyone help.

Related

AngularJS disable default form submit hook

I have already existing forms on admin panel and i've wrapped it to <div ng-app="app" ng-controller="app-nav"> and after that all forms stopped to work...
How can i disable default AngularJS behaviour to make all forms get back to work again?
https://jsfiddle.net/24sufpvn/
Here AngularJS prevents default form submission.
Angular.js prevent the native <form> submission because it's missing a valid action attribute.
I have edited your fiddle and added the missing action attribute, and now the form submits correctly:
<div ng-app ng-controller="LoginController">
<form method="post" action="https://formsubmit.co/your#email.com">
<input type="submit" name="s" value="Submit">
</form>
</div>
Working fiddle:
https://jsfiddle.net/SlumDog1/kg6m5r7v/11/
I didn't find any solution except editing angular.js:
Need to replace line:
if (!('action' in attr)) {
to this:
if (false) {

AngularJS Disabling Forms When Controllers are Created

AngularJS disables (cannot submit) forms that do not have a specific action set:
<form action="">
when you create a controller. It does not have this issue when creating directive or factories.
You can see this in a plunk here:
http://plnkr.co/edit/gWFRMKGO3FzZtOgs4VmW?p=preview
Form is defined as:
<form action="" method="post">
If you delete the starting on line 6, you will be able to submit the form.
A simple solution is to define the action, but I'd rather not do this, as it is not necessary.
UPDATE
Some details can be found here on trying to get this change in Angular:
https://github.com/angular/angular.js/pull/3776
You can use ng-submit to handle that.
<form ng-submit="submitForm()" method="post">
<input name="test" value="11111111" />
<button type="submit" name="submit" value="1">Send</button>
<input type="submit" name="submit2" value="Send2" />
</form>
That way the form will submit normally and you must actually send the data on your submitForm function (just an example name).
Here is a quick plnkr: http://plnkr.co/edit/lwWVG0CDHSGMtMU0B8Nj?p=preview
Notice that you can submit using the buttons and also by pressing enter on the field. I hope that's what you've been asking for.
Thanks

AngularJS Form is not in HTML

I am trying to use AngularJS Validation in order to validate a simple form, however I was having troubles getting my ng-class to show the correct class based off whether or not the input was dirty or not. Then when I looked at the actual HTML of the page, the <form> tags are not even in the document at all!
<form novalidate name="infoForm">
<p>To start, provide some basic information about the project.</p>
<ul class="ulFormGeneral">
<li>
<label>Company name</label>
<input id="CompanyName" ng-class="{ cvError : infoForm.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
</li>
</ul>
</form>
I want the cvError class to be added to this input if it is dirty, but nothing happens when I look at this in the browser. What am I doing wrong that is causing the <form> to just leave the DOM and then not work with my Angular expressions?
Welcome to the Angular world, no forms required! Here, the model is king. It looks like the problem is the ng-model and ng-class are point at different places.
Point everything at form.CompanyName (assuming that is the model name is form in the $scope):
<input id="CompanyName" ng-class="{ cvError : form.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
The ng-model binds to the $scope. When you change the input field, it is automatically updated in the $scope. No form is needed or hitting a submit button to get the data. The $scope is updated with each key stroke.
The controller should do the work of figuring out what to do with the changes in the model. For example, you can add an ng-click to a button that fires a function defined by the controller to save the model.

AngularJS post form to external URL

I have the following form in my AngularJS app which contain hidden fields with values filled based on user selection on some inputs on the form (radio buttons...etc), when the user click on the Submit link I should route the user to an external URL while passing hidden fields just as any normal form submission. Unfortunately I can't do this as some of the hidden field values are dependent on some calculations inside a function of the view related controller (as shown below in controller code, so I was wondering is there a way I can call the controller function from this form, then the controller function post the whole form and its field? Any example is highly appreciated. Thanks.
Note I am using link instead of a button.
<form name="clientPaymentForm" id="clientPaymentForm" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">>
<div>
<fieldset>
<input id="name" type="text" required placeholder="Client Name" ng-model="client.name">
...
...
<input type="hidden" name="amount" ng-value="order.total">
...
...
<a class="orderButton" href="javascript:{}" onclick="document.getElementById('clientPaymentForm').submit(); return false;">Order Now</a>
</fieldset>
</div>
</form>
Controller:
$scope.processOrder = function(){
//Order calculation happens here to update order.total value and can only happen after click click Order Now to place the order...
};
I guess this is a bit late, but what you want to use is the ng-click directive which will allow you to call functions defined directly on the scope.
Assuming that you've defined $scope.processOrder, change your a tag to the following:
<a class="orderButton" ng-click="processOrder()">Order Now</a>
And everything should work as hoped.
Alternatively, you could use ng-submit on the form to have it work when you press the "Enter" or "Return" key, as in:
<form name="clientPaymentForm" id="clientPaymentForm" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" ng-click="processOrder()">.

Loop through all instances of ng-form inside form for validations

I created a plunker.
I generate input elements dynamically when "+ Name" button is clicked, each inside ng-form.
How can I grab all the instances of ng-form for validating each individual ng-form? So that the "+ Name" button remains disabled if the fields are not valid or a new ng-form is created?
Edit More details about the form structure
There are other fields in the form which I removed for brevity reasons. It is basically a long form, something like
<form name="myForm">
<input name="one" />
.
.
<div ng-repeat....>
<ng-form>
<input name="schoolName" />
</ng-form>
</div>
<button>+ Name</button> <!-- I cannot check for myForm.$valid here-->
<!-- since the person might not filled the rest of the below fields -->
<!-- hence the need to grab "each" "ng-form" -->
<input name="some" />
<input name="other" />
</form>
1) http://docs.angularjs.org/api/ng.directive:form say:
In angular forms can be nested. This means that the outer form is
valid when all of the child forms are valid as well.
Therefore your main form will be valid only when all children forms will be valid.
2) if you want / need to have references to children FormControllers in main form you need to use custom directives.
I update your code: http://plnkr.co/edit/Vvz6VXJUj4lF0NR9gjjL
script.js contains 2 directives: "mainForm" and "childForm" which show you how they can interact
"Results" section in index.html show when "mainForm" become valid. And as you can see it confirms what Angular.js documentation says

Resources