Is a custom validator only called if field=required?

Home Forums Quform WordPress Is a custom validator only called if field=required?

This topic is: resolved
  • This topic has 5 replies, 2 voices, and was last updated 7 years ago by katw.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #25422
    katw
    Participant

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

    #25425
    katw
    Participant

    It 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

    #25467
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #25477
    katw
    Participant

    Does 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
    #25483
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #25487
    katw
    Participant

    Thanks so much, you are a legend.

    CLOSE TICKET

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy