Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to validate survey question results on server via Ajax requests #137

Closed
andrewtelnov opened this issue Nov 28, 2016 · 1 comment

Comments

@andrewtelnov
Copy link
Member

Create an API that will allow easily to call server code to validate question results.

@andrewtelnov andrewtelnov changed the title Validate survey question results on server via Ajax requests Add ability to validate survey question results on server via Ajax requests Nov 28, 2016
@andrewtelnov
Copy link
Member Author

onServerValidateQuestions callback is added into survey class. Here is the example of using:

<script>
    var json = {
        questions: [{ type: "text", name: "country" }]
    };
    var survey = new Survey.Survey(json);
    //assign call to onServerValidateQuestions callback
    survey.onServerValidateQuestions = function (survey, options) {
        //options.data contains the data for the current page.
        var countryName = options.data["country"];
        //If the question is empty then do nothing
        if (!countryName) options.complete();
        //call the ajax method
        $.ajax({
            url: "http://services.groupkt.com/country/get/all"
        }).then(function (data) {
            var found = false;
            var countries = data.RestResponse.result;
            for (var i = 0; i < countries.length; i++) {
                if (countries[i].name == countryName) {
                    found = true; break;
                }
            }
            //if the country is unknown, add the error
            if (!found) options.errors["country"] = "The country name is unknown.";
            //tell survey that we are done with the server validation
            options.complete();
        });
    }
    survey.render("dxsurvey");
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant