I need to check if a form is valid in a controller.
View:
<form novalidate="" name="createBusinessForm" ng-submit="setBusinessInformation()" class="css-form"> <!-- fields --> </form>
In my controller:
.controller( 'BusinessCtrl', function ($scope, $http, $location, Business, BusinessService, UserService, Photo) { if ($scope.createBusinessForm.$valid) { $scope.informationStatus = true; } ...
I'm getting this error:
TypeError: Cannot read property '$valid' of undefined
The form instance can optionally be published into the scope using the name attribute. So to check form validity, you can check value of $scope. yourformname. $valid property of scope.
ng-controller directive tells AngularJS what controller to use with this view. AngularJS application mainly relies on controllers to control the flow of data in the application.
$dirty means the user has changed the input value, $invalid means the address itself is invalid. Therefore the error is only shown if the user has actively changed the input value to either an empty or invalid value.
Sets the form to its pristine state. This method sets the form's $pristine state to true, the $dirty state to false, removes the ng-dirty class and adds the ng-pristine class. Additionally, it sets the $submitted state to false.
Try this
in view:
<form name="formName" ng-submit="submitForm(formName)"> <!-- fields --> </form>
in controller:
$scope.submitForm = function(form){ if(form.$valid) { // Code here if valid } };
or
in view:
<form name="formName" ng-submit="submitForm(formName.$valid)"> <!-- fields --> </form>
in controller:
$scope.submitForm = function(formValid){ if(formValid) { // Code here if valid } };
I have updated the controller to:
.controller('BusinessCtrl', function ($scope, $http, $location, Business, BusinessService, UserService, Photo) { $scope.$watch('createBusinessForm.$valid', function(newVal) { //$scope.valid = newVal; $scope.informationStatus = true; }); ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With