Server side validation

I am trying to move from version 5 to 7 but it is pain in the ass as the documentation is poor for new versions and lots of functionalities are missing.
So quick question:
I have a form which I send to server. Validation on client side is done, but server responses that one of the fields has, for example, duplicated value. How to set the field as not validated and add errorMessage to it?

Hello, klysiak!

This demo helped me a lot. The interaction between the form and the backend is there, too.

Hi Labirintami,

Please enlight me and tell me where there is any interaction between form and server side except sending form to server in the example?
All fields validation is done only on client side…

This does not answer my question.

send() method returns the Promise object:
https://docs.dhtmlx.com/suite/form__api__form_send_method.html
so you may try to catch the “rejected” form request, and inform the user about the probable fail reason.
Unfortunaetly it is not aivlabale to indicate the form control as “validation fail” without actual (client-side only) validation failing, so I can suggest you to clear(modify) the input value and inform the user about the “error” using the dhtmlxPopUp or the dhtmlxMessage.

I’ve discovered a way to do this, if it helps anyone. Below is my validation rule for my input. My use case is that I want to see if a job number is already in use. First it checks if the value is a boolean (true/false) as that’s what I pass in the callback from my server side validation. Validation messages are set accordingly, and if the value is a string and passes the regular expression test for a valid job number format, it sends the data to the server to try and look up job data for the given job number. If it comes back with no data, I call the validation again with the boolean true to set the style and message. If the ajax returns an error code, I check for a 404 error (my API returns 404 when a job isn’t found) and validate true for that, or set a special error message and validate false. If valid job data is returned, that means the job number is already in use, so I validate false to set the style and message on the input.

validation:function(value){
    let inp = form.getItem("JobNumber");
    if (typeof value === "boolean"){
        inp.setProperties({errorMessage:"Job Number already in use",successMessage:"Job Number is valid and available"});
        return value;
    } else {
        inp.setProperties({errorMessage:"Invalid job number format",successMessage:"Job Number is valid"});
        if (RegExp("^[0-9]{6}[a-zA-Z]+[0-9]*$").test(value)){
            dhx.ajax.get("/api/jobdata/read_one.php?JobNumber=" + value).then(function(data){
                form.getItem("JobNumber").validate(false,(!data.id ? true : false));
            }).catch(function(error){
                let inp = form.getItem("JobNumber");
                inp.setProperties({errorMessage:"Error during validation. Notify IT."});
                inp.validate(false,(error.status && parseInt(error.status) === 404));
            });
        }
        return RegExp("^[0-9]{6}[a-zA-Z]+[0-9]*$").test(value);
    }
}