The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Customise error on calculated number field
I have a number field that calculates a total based on saved values of other fields. I can use the number range to restrict the allowed value of this total but if it falls outside of this the error produced is 'Please select a lower number', can this be customised and if so, how?
Thanks
Andy
March 14, 2017 at 11:38 am
Can you provide an example of this calculation in pseudocode or the live site with details?
March 14, 2017 at 11:54 am
It is a number field [total] that is set to calculate the value for the field, the calculation is very simple.
= field A + field B + field C
I have set the number range on this field to be a maximum of 50 so it will not allow the form to be submitted if above this. The issue is that if the number is above 50, it produces an error saying 'Please select a lower number'. I can't find any reference to this error so that I can change it.
Hope that makes sense.
March 14, 2017 at 12:03 pm
You will need something in JS/JQuery along the lines of this:
<script type="text/javascript">
jQuery(document).ready(function(jQuery){
jQuery('#field_key').change(function(){
var val1 = jQuery("#field_key").val();
if (val1 > 50)
{jQuery("#field_key").val('50');}
jQuery("#field_KEY").change();
});
});
</script>
What his should do is look to see when the total field has changed. If the value is greater than 50, make the value stay at 50, otherwise change the value appropriately. You'll need to insert your field key in the above (e.g., field_j8sdf).
March 15, 2017 at 3:50 am
Thanks for the reply. I do not want to stop the total from going above 50, I already prevent the form from being submitted by using the range option for the number field. The issue is that when the total is above 50, the error message produced is 'Please select a lower number'. I just want to know if it is possible to change this error message.
Thanks
Andy
March 15, 2017 at 6:21 pm
Ah...
Give this a whirl:
add_filter('frm_validate_entry', 'validate_my_form', 20, 2);
function validate_my_form($errors, $values){
if($values['form_id'] == 45 && $_POST['item_meta'][125] > 50){//Change 45 to the ID of your form and 125 to the ID of your number field
$errors['my_error'] = 'You are not allowed to submit this form.';//Change this to your error
}
return $errors;
}
Discussion closed.