Clear Form_CustomValidate errors from Startup Script change event?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
SilentNight
User
Posts: 171

Clear Form_CustomValidate errors from Startup Script change event?

Post 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?


arbei
User
Posts: 9286

Post by arbei »

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


SilentNight
User
Posts: 171

Post 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.


arbei
User
Posts: 9286

Post 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
}

SilentNight
User
Posts: 171

Post 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.


arbei
User
Posts: 9286

Post by arbei »

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


SilentNight
User
Posts: 171

Post 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.


mobhar
User
Posts: 11660

Post 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');
});

Post Reply