Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-side validation for form

The default validation for the form I have works as expected. But when a user types in a valid email address and a password of three characters minimum, that doesn't mean the login credentials are valid.

So my question is:

How can I set the model email and password to invalid after server-side validation, so the input-fields get the class ng-invalid instead of ng-valid.

My current code

function IndexCtrl( $scope, $http )
{
  $scope.form = {};
  $scope.submitLogin = function ()
  {
    $http.post( '/api/auth/login', $scope.form ).success( function( data )
    {
      if ( !data.success )
      {
        $scope.form.errors = [ data ];

        // here I also want to mark the models 'email' and 'password' as invalid, so they both get the class 'ng-invalid'
      }
      else
      {
        $location.path( '/' );
      }
    });
  };
}
like image 734
ndequeker Avatar asked Aug 28 '12 09:08

ndequeker


People also ask

How is validation done on server-side?

In the Server Side Validation, the input submitted by the user is being sent to the server and validated using one of server side scripting languages such as ASP.Net, PHP etc. After the validation process on the Server Side, the feedback is sent back to the client by a new dynamically generated web page.

Which validation is better client-side or server-side?

Server-side validation is slower than client-side input validation. However, server-side input validation is more reliable than client-side input validation. Thus, it's safe to say that client-side data validation improves user experience while server-side input validation improves security.


1 Answers

Tosh shimayama gave the right answer. $setValidity is a method from the NgModelController and takes two parameters: validationErrorKey and isValid.

More information on $setValidity

Change the validity state, and notifies the form when the control changes validity. (i.e. it does not notify form if given validator is already marked as invalid).

Source and further information AngularJS: NgModelController

like image 62
ndequeker Avatar answered Nov 15 '22 16:11

ndequeker