1000hz / bootstrap-validator

A user-friendly HTML5 form validation jQuery plugin for Bootstrap 3
http://1000hz.github.io/bootstrap-validator
MIT License
2.38k stars 1.07k forks source link

Display error if ajax request status is OK. #628

Closed MysteriousNothing closed 6 years ago

MysteriousNothing commented 6 years ago

Hello, I have a problem creating custom validation and returning the error if ajax request status is 'ok'. Basically I need display error if ajax request returns status == ok. This means a customer with this personal ID. exists and this should disable submit button and dispaly error message.

I been trying to get this work for days.

Thank you in advance! :) Here is my current code:

$('form').validator({
        custom: {
            customer: function($el) {
                var input_value = $el.val();
                if(input_value.length => 10){                    
                $.ajax({
                    type: 'GET',
                    url: '/users/'+input_value,
                    dataType: 'json',
                    success: function(data){
                        if(data.status == 'ok'){
                            //validation
                            $("#user_login").attr("type", "hidden");
                            $("#user_email").attr("type", "hidden");
                            $("#user_password").attr("type", "hidden");
                            $("#user_password_confirmation").attr("type", "hidden");

                            $('form').validator('update');
                            $('form').validator('validate');

                            // if exists return here, it does nothing and does not display error.
                            return 'This customer exits'
                        else
                            $(".nahtavad").prop('disabled', false);
                        }                        
                        }else {
                            $(".nahtavad").prop('disabled', false);
                        }
                    },
                    error: function (err) {
                        $("input[type='submit']").prop('disabled', false);
                        $(".nahtavad").prop('disabled', false);
                    }
                });
                }
            }
        }
});

HTML <input class="form-control isikukood hide-controls" id="isikukood" data-error="can't be blank" pattern="^[A-z0-9-]+" data-pattern-error="These characters are not allowed!" required="required" data-customer="" data-customer-error="This user already exits!" name="user[personal_code]" type="text">

MysteriousNothing commented 6 years ago

check with jQuery if #user_login is hidden and return error solves the problem.