Check raw value of checkbox during validation with wpcf7_validate_checkbox
-
Hi, I’m trying to do some custom validation of a series of exclusive checkboxes using the
wpcf7_validate_checkbox
filter, however the challenge is that I am using the pipe character method to have a different display value from the value which is sent via the email. For example, here is the checkbox shortcode:[checkbox myoptions use_label_element "This is option One|1" "This is option Two|2" "This is option Three|3"]
So the resulting Checkbox will show the “This is option X” text but when the email is sent the default value will be the numeric value, e.g. “1”.
The issue is that I want to create custom validation on the checkboxes to see which option has been selected, but when the form is submitted it is the “This is option X” text which is the $_POST value sent whereas I really want to do a comparison on the numeric value.
So my question is if there anyway to access the right-hand numeric value during the
wpcf7_validate_checkbox
validation? Here’s a simplified version of my validation code:if ( 'myoptions' == $tag->name ) {
if ( $_POST[ 'myoptions' ] == '2' ) {
$result->invalidate( $tag, "Option 2 selected" );
}
}So basically I want to be able to check if option 1, 2 or 3 was selected, but I am only ably to check if the posted value equals the text, e.g.
if ( $_POST[ 'myoptions' ] == 'This is option Two' )
.The problem with matching the text is if that the text is changed in the form then I would have to update the validation code to match, also in my case the text labels are quite lengthy so the difference between matching a long string and a single digit is significant.
(Side note: this issue would be avoidable if it was possible to set separate value and label text values on checkboxes, but they currently can only be the same text. If not for this restriction in CF7 I would set the label to the text and the value of the checkbox to 1, 2, 3 etc)
- The topic ‘Check raw value of checkbox during validation with wpcf7_validate_checkbox’ is closed to new replies.