0

I'm trying to validate attachment field, below code is working but when i use async false

attachment:{
                required:{
                    depends: function () {
                        var res=false;
                        $.ajax({
                                    type: "POST",
                                    url: baseUrl+"status",
                                    async: false,
                                    cache: false,
                                    dataType: "json",
                                    success: function (result) {  res=result; }
                                });
                                return res;
                    }
                }

this function is called two times but when i remove async:false then validation is not working because res value is returned before the ajax call is executed.

Any solution. Thanks

5
  • Do you need to return res as you have already assigned res=result ? Commented Oct 11, 2022 at 6:34
  • It's entirely unclear why you have ajax inside of a depends inside of required; nor was it explained why getting called multiple times is a problem. Where is the relevant HTML and the description about what you are trying to validate with this arrangement? Is there a reason why this cannot be done with the remote method instead? Commented Oct 12, 2022 at 1:52
  • It appears as if you might misunderstand what depends is used for. Your field is required depending on some condition. Example: a text field is only required when a checkbox is checked. What purpose is being served by the ajax here? Commented Oct 12, 2022 at 1:57
  • @Sparky I want required true or false based on value return from server that's why i called ajax can you please provide some link for the same Commented Oct 12, 2022 at 12:29
  • Please review the official documentation for the plugin, which clearly defines how to use the remote method. Commented Oct 12, 2022 at 21:08

1 Answer 1

0

you can define a bool variable and set is true when one time ajax called

$isAjaxSended = false;
attachment:{
            required:{
                depends: function () {
                    var res=false;
                    if(!isAjaxSended){
                              $.ajax({
                                type: "POST",
                                url: baseUrl+"status",
                                async: false,
                                cache: false,
                                dataType: "json",
                                success: function (result) {isAjaxSended=true;  res=result; }
                              });
                            }
                            return res;
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.