Home › Forums › Quform WordPress › Is a custom validator only called if field=required?
- This topic has 5 replies, 2 voices, and was last updated 7 years ago by
katw.
- AuthorPosts
- May 2, 2018 at 12:30 pm #25422
katw
ParticipantI have created a custom validator for a radio button group with ID 1_52
The radio button group has YES and NO (or not set) as choices.
It is not required (unless a hidden field ID 1_62 has a value <> 0).
The hidden field has a default value 0 and has other numbers pushed into the field by client side javascript.
I created a custom validator for the radio group, testing for a value in the hidden field. It should block submission and return an error if the hidden field value <> 0.
The validator:
add_filter('quform_element_valid_1_52', function ($valid, $value, $element) { $form = $element->getForm(); $errorValue = $form->getValue('quform_1_62'); if ($errorValue > 0) { $element->addError('Oops, we need your agreement'); $valid = false; } return $valid; }, 10, 3);
This technique has worked for a couple of TEXT fields I validated in a similar fashion.
But isn’t working here.
I never see the validation message when I know the value ‘1’ is in the hidden field (viewed via developer tools plugin). And it isn’t blocking submission when the custom validator should have returned FALSE..
The only difference with this field (and the ones that worked) is:
– it’s not marked as a mandatory field and,
– it’s a radio button group not single text field.Is the reference for the field wrong? ie “quform_element_valid_1_52” as it’s a radio group?
Appreciate your advice as I am so close to finishing this form.
May 2, 2018 at 1:34 pm #25425katw
ParticipantIt appears the custom validator ISN’T executed if the radio group field which the validator is attached to:
– IS NOT REQUIRED and
– has NO VALUE (empty).But this is the precise condition where I want the validator running. No radio button group value is handled within the custom validator… but it appears empty results in no custom validation.
hmmm
May 2, 2018 at 6:57 pm #25467Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
May 2, 2018 at 11:48 pm #25477katw
ParticipantDoes the addValidator function hold its state? persist after validation? Or reset?
As I need it to apply for that validation action only and reset ready for next ‘try’ at validation.
The user may after validation fail do change different in the form thereby changing the ‘required’ condition requirement.
Just need to know if I must reset validation … removeValidator(‘required’) then test if it’s needed.
Thanks
- This reply was modified 7 years ago by
katw. Reason: Wanted to explain purpose of question better
May 3, 2018 at 9:54 am #25483Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
May 3, 2018 at 2:03 pm #25487katw
ParticipantThanks so much, you are a legend.
CLOSE TICKET
- This reply was modified 7 years ago by
- AuthorPosts
- You must be logged in to reply to this topic.