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( '/' );
}
});
};
}
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.
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.
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
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