Home › Forums › Quform WordPress › Set value if field empty (unanswered) at submit?
- This topic has 11 replies, 2 voices, and was last updated 6 years, 9 months ago by
katw.
- AuthorPosts
- July 25, 2018 at 11:19 am #26374
katw
ParticipantI have a radio button field called “status” with multiple options.
This is not required, and “Validate submitted value” is enabled.
On submit, if the field is empty I want to populate the field with a value.
Is this possible?
I didn’t want use the default value option as I didn’t want to influence the users answer.
As no answer is an answer in itself.
The value I want to set (if field empty) is coincidentally one of the option values.
August 1, 2018 at 10:21 am #26459Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
August 2, 2018 at 7:39 am #26479katw
ParticipantDoes the
post_set_form_values
hook run after the form has successfully passed validation?
So I can trust the data within the field?
Thanks
August 2, 2018 at 8:43 am #26482katw
ParticipantQuestion 2.
I assume QuForm doesn’t validate the value you assign to the field?
OR
Does it still honour the “Validate submitted value” setting on the form field advanced settings?
If YES = still checked THEN {
Can this new value be added to the option value array?
So that the change is accepted AND EditEntry mode now shows the expanded set of radio button options for editing the entry?
}
If NO = not checked THEN {
What happens with Edit Entry mode?
Will a new radio button option be added to the existing set and be selected?
}
WHY? I may like to add a new option value to the values defined to make it stand out as a “non answer”.
August 2, 2018 at 11:46 am #26486Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
August 2, 2018 at 12:57 pm #26497katw
ParticipantI have confirmed from testing the post_set_form_values hook is subject to validation.
So setting a field value outside those options prescribed will result in an error “Not a valid value” on submit.
So this hook requires one of the values prescribed if “Validate submitted value” is active in field options.
I will experiment with your suggestions. Thanks Ally
Question 3:
What does the parameter
'id' => 0
do in your addOptions() function?Does it define position order? Is it a unique id reference?
I had a quick look at the function reference in Multi.php but couldn’t see how ID was applied there.
- This reply was modified 6 years, 9 months ago by
katw. Reason: Add followup query
August 2, 2018 at 1:32 pm #26499Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
August 3, 2018 at 12:59 am #26503katw
ParticipantThanks good to know.
What happens if you are adding multiples? Is the ID treated as a string?
So you could use “00”, “000” for any extra options you want to create?
Or would it be better to assert an ID in high number range?
August 3, 2018 at 1:18 am #26504katw
ParticipantI re-activated “Validate submitted value” on the radio group field AND added your function to change the $validator:
add_filter('quform_entry_pre_process_1', function (array $result, Quform_Form $form) { $propstatus = $form->getElement('quform_1_31');//property status radio group if ($propstatus) { $validator = $propstatus->getValidator('inArray'); if ($validator) { $haystack = $validator->config('haystack'); $haystack[] = 'Query';// <---- NEW VALUE as set in quform_post_set_form_values_1(function) $validator->setConfig('haystack', $haystack); } } return $result; }, 10, 2);
On submit I get the validation fail, outside range notice
"This value is not valid"
The
quform_entry_pre_process_X
hook isn’t executed.BTW I changed your “InArray” string to “inArray”. May be semantics or may be important ?
Note I am using the
quform_post_set_form_values
hook to set the out-of-range option value.August 3, 2018 at 3:27 am #26509katw
ParticipantOn further investigation the wrong hook is being used to coerce the validator a submit.
BEFORE:
quform_entry_pre_process_#
.This is not run at form submission/processing but used with edit entry form.
AFTER: Changing the hook to
quform_pre_process_#
solved the issue and allowed the form to be submitted with an option value outside prescribed range.Am I correct I need to repeat this code and process, this time using the hook you gave
quform_entry_pre_process_#
for entries to be editable when value is outside range?- This reply was modified 6 years, 9 months ago by
katw. Reason: Solution and query on edit view
August 6, 2018 at 12:58 pm #26544Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
August 6, 2018 at 1:42 pm #26548katw
ParticipantYes thanks. All good. Close ticket
- This reply was modified 6 years, 9 months ago by
- AuthorPosts
- You must be logged in to reply to this topic.