Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a bug in my angularjs form. I want to change the class of an element when the input from the angularjs form is valid and change it back if its invalid.

I got this code:

<li data-ng-class="{ success: userFormRegistration.email.$valid, danger: userFormRegistration.email.$invalid }" class="danger">valid email</li>

Now this is the input of the email:

<input type="email" name="email" data-ng-model="userFormRegistration.email" class="form-control ng-pristine ng-invalid ng-invalid-required ng-invalid-email" placeholder="Email" required="">

In the beginning the class added is danger but when i enter a valid email it is deleting the class but not adding the success class..

Here is the angularjs code:

// app.js
            // create angular app
            var FormApp = angular.module('FormApp', []); ;


            // create angular controller
            /*FormApp.controller('registrationFormController', function ($scope, $http) {*/
            function registrationFormController($scope, $http) {
                // create a blank object to hold our form information
                // $scope will allow this to pass between controller and view
                $scope.userFormRegistration = {};

                // function to submit the form after all validation has occurred            
                $scope.submitForm = function (form) {
                    // check to make sure the form is completely valid
                    if (form.$valid) {
                        $scope.userFormRegistration = { actionname: "registration", email: $scope.userFormRegistration.email, password: $scope.userFormRegistration.password }
                        // start ajax call
                        $http({
                            method: 'POST',
                            url: 'Registration.php',
                            data: $.param($scope.userFormRegistration),  // pass in data as strings
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded'}  // set the headers so angular passing info as form data (not request payload)
                        })
                            .success(function (data) {
                                console.log(data);
                                if (!data.success) {
                                    // if not successful, bind errors to error variables
                                    $scope.errorRegistrationEmail = data.errors.email2;
                                    $scope.errorRegistrationPassword = data.errors.password;
                                    $scope.errorRegistrationActionname = data.errors.actionname;
                                    $scope.registrationMessage = data.message;
                                }
                                else {
                                    // if successful, bind success message to message
                                    $scope.message = data.message;
                                }
                            });
                        //end ajax call
                    }
                    else {
                        if (!$scope.userFormRegistration.email.$valid) {
                            var error = { message: "please enter valid email" };
                            $scope.errorRegistrationMessage = error.message;
                        }
                        else if (!$scope.userFormRegistration.password.$valid) {
                            var error = { message: "please enter valid password " };
                            $scope.errorRegistrationMessage = error.message;
                        }
                    }
                };
            };

Your advice please.

share|improve this question
    
you need to specify condition when and what you want to show in class when input is success or danger – Ubiquitous Developers Nov 24 '15 at 5:13
    
Are you certain the email address you are filling in is valid? Are you reusing the name somewhere else in the form? – Carlos G. Nov 24 '15 at 5:36
up vote 0 down vote accepted

The $valid and $invalid properties come from the form and not from the value of the input. You need a form element with a name, then also a name into the input, and then use formName.inputName.$valid or $invalid.

share|improve this answer
    
Thank you alot! the problem was that the form not was an object...=] – Rafael Nov 25 '15 at 21:56
    
Great that it worked! – Leandro Zubrezki Nov 25 '15 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.