Page 1 of 1

Clear Form_CustomValidate errors from Startup Script change event?

Posted: Sun Jan 29, 2023 5:38 am
by SilentNight

Hello,
I have a custom template for my order add pages that dynamically adjusts to show/hide relevant fields based on the order type selector at the top. To do so it uses the change event withing Client Scripts -> Table-Specific -> Add/Copy Page -> Startup Script

$("select[name='x_OrderType']").change(function() {
//Some code here to show/hide fields when this selection is changed
}
$("input[name='x_OrderType]").trigger('change');

I also have code in Form_CustomValidate that validates some fields before submission.

I discovered an edge case that prevents users from saving the order if they trigger a Form_CustomValidate error and then attempt to change the order type without first clearing the error. If that field is hidden using the code in Startup Script the error is hidden along with it before the error is cleared preventing the user from saving the order.

So is there a way to clear all validation errors within the Startup Script change event above?


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Sun Jan 29, 2023 12:08 pm
by arbei

You may post the code of your Form_CustomValidate (client side) for discussion.


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Mon Jan 30, 2023 7:41 am
by SilentNight

For example in client side Form_CustomValidate:

function (fobj) { // DO NOT CHANGE THIS LINE! (except for adding "async" keyword)!
    var row = this.value;
    if (isNaN(parseFloat(row["Freight"])) && !isFinite(row["Freight"]) || parseFloat(row["Freight"]) < 0) {
        return this.addCustomError("Freight", "Freight must be a positive number with no currency symbol.");
    }
    return true;
}

And client side Startup Script:

$("select[name='x_OrderType]").change(function() {
    if ($(this).fields("OrderType").value() == 1) {
        $(this).fields("Freight").value("0");
        $(this).fields("Freight").visible(false);
        $(this).fields("Freight").readonly(true);
        $(this).fields("Freight").disabled(true);
    }

If a user enters a negative number or number with currency symbol an error that will appear next to the Freight field when saving the form. If the user then changes the order type before clicking on the freight field to clear the error then the field will hide but the error won't be cleared, preventing the order from saving.


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Mon Jan 30, 2023 9:45 am
by arbei

Before you hide the field, you may check if the field has error first by, e.g. (in your "change" handler under Startup Script)

if (!ew.forms.get(this).fields["Freight"].error) { // The field has no errors
// hide the field
}

Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Mon Jan 30, 2023 10:43 am
by SilentNight

Sadly this solution won't work as the freight field is not applicable and should not be shown for some order types. Instead I must find a way to clear any validation errors when the order type is changed.


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Mon Jan 30, 2023 11:56 am
by arbei

Then you simply call ew.forms.get(this).fields["Freight"].clearErrors().


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Mon Jan 30, 2023 1:35 pm
by SilentNight

Thank you! Your suggestion is most likely the correct way to do what I want to do, however in my case I have many other fields I would also need to do this for. I could probably loop through all inputs and clear all errors but luckily I've stumbled across a somewhat hacky way to do it. It won't work for everyone's use cases especially if you use click events on input boxes for something else but since I don't the following appears to work great:

// Within the order type change event in client side startup script
// For checkboxes trigger click event twice
$("input[name='x_BillingSame']").trigger('click');
$("input[name='x_BillingSame']").trigger('click');

// Loop through text inputs
$('input[type=text]').each(function(){
	$(this).trigger('click');
 })

This simply triggers click events for a checkbox I have along with all text inputs including the Freight one I was asking for help with. Simulating a click appears to remove the errors. This has the added benefit of clearing validation errors contained in detail tables.


Re: Clear Form_CustomValidate errors from Startup Script change event?

Posted: Thu Feb 02, 2023 12:34 am
by mobhar

This is great. For Select/Select2 control, then you may simply use:

// Loop through Select/Select2 control
$('select').each(function(){
    $(this).trigger('click');
});