1

I'm using bootstrapvalidator and it works fine with the following.

$('.login-form').bootstrapValidator({
    message: 'This value is not valid',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        password: {
            validators: {
                notEmpty: {
                    message: 'The password is required and cannot be empty'
                }
            }
        },
        email: {
            validators: {
                notEmpty: {
                    message: 'The email is required and cannot be empty'
                },
                emailAddress: {
                    message: 'The input is not a valid email address'
                }
            }
        }
    }
});

My submit action submits an ajax request to the server which answers for example in json

errors: {user: 'Username does not exist', password: 'wrong password'}

How can I manually trigger these server errors on the bootstrapvalidator form input fields?

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
user391986
  • 29,536
  • 39
  • 126
  • 205
  • what do you exactly mean by "manually triggering" the errors? – benomatis Oct 26 '14 at 19:56
  • I really mean showing the server errors on the input fields – user391986 Oct 26 '14 at 19:57
  • you mean triggering while you type instead of after post? maybe it's just me, but i don't get it, sorry... – benomatis Oct 26 '14 at 20:03
  • As the user types and submits, it validates the JS validation. Now the server presses submit and the ajax request is sent but the server returns more specific errors for each input if there are any. How do I display these errors on each input in the same way that bootstrap validator script does? – user391986 Oct 26 '14 at 20:05
  • the reason I don't understand it is because if you use the plugin (as you said you do) it normally displays the error messages already as you type, or so it shows on its documentation page... is that not working for you? – benomatis Oct 26 '14 at 20:12
  • maybe if you'd share _why_ you need it, it would make more sense to me...? – benomatis Oct 26 '14 at 20:15
  • ok here is the scenario when a user registers. The 'username' input passes all validators on the front end and the form is submitted to the server. The server responds 'sorry username already exists please choose another'. Now i want to display that error in the same manner. – user391986 Oct 26 '14 at 20:26
  • think you're looking for [remote validator](http://bootstrapvalidator.com/validators/remote/) – benomatis Oct 26 '14 at 21:10

1 Answers1

1

Use the remote validator as follows:

$('.login-form').bootstrapValidator({
    fields: {
        username: {
            message: 'The username is not valid',
            validators: {
                // The validator will create an Ajax request
                // sending { username: 'its value' } to the back-end
                remote: {
                    message: 'The username is not available',
                    url: '/path/to/backend/'
                }
            }
        }
    }
});
benomatis
  • 5,536
  • 7
  • 36
  • 59