Custom Sanitization in Group subfields
- This topic has 5 replies, 2 voices, and was last updated 3 years, 1 month ago by
Long Nguyen.
-
AuthorPosts
-
March 4, 2022 at 12:33 AM #34280
Catharina von Hobe
ParticipantHi,
I want to custom sanitize a Textfield and check for a valid URL, but still leave the possibility to use hash-links.So Inputs have to look like this "https"/"http" or "#something".
I have this Sanitization Code:
function linkValidation($value) { $functInput = $value; $isHttps = strpos($functInput, 'https'); $isHttp = strpos($functInput, 'http'); $isHash = strpos($functInput, '#'); if($isHttps === 0 || $isHttp === 0 || $isHash === 0){ $isAny = true; } else{$isAny = false; } if($isAny == false): $value = ''; endif; return $value; }
And the Text Field in the Group is set to be required, so when i empty out the input with this sanitization, there sould be an error message and preventing you from saving the changes.
(I have set the custom Sanitization in the mb Field Editor under Advanced for "Custom sanitize callback" to "linkValidation")
This seems to works fine with normal fields, but for group subfields it doesnt seem to.
And I noticed that subfields in groups inside Custom mb Blocks even ignore the requirement of that field, at least I'm not getting an error message for leaving the field blank.
Do I miss an option or setting in the group I need to change, or is it maybe a bug in the group functionality?
March 5, 2022 at 10:14 AM #34323Long Nguyen
ModeratorHi,
The sanitization callback should be added under
group
field settings, not subfield settings. Like this'fields' => [ [ 'name' => __( 'Group', 'your-text-domain' ), 'id' => $prefix . 'group_lotc08c5jb', 'type' => 'group', 'sanitize_callback' => 'linkValidation', // here 'fields' => [ [ 'name' => __( 'Text', 'your-text-domain' ), 'id' => $prefix . 'text_2t5ekhiu05p', 'type' => 'text', ], [ 'name' => __( 'Textarea', 'your-text-domain' ), 'id' => $prefix . 'textarea_y3kcunwdt1', 'type' => 'textarea', ], ], ], ],
Then you can access the subfield value via element key
field_id
function linkValidation($value) { $functInput = $value['text_2t5ekhiu05p']; $isHttps = strpos($functInput, 'https'); $isHttp = strpos($functInput, 'http'); $isHash = strpos($functInput, '#'); if($isHttps === 0 || $isHttp === 0 || $isHash === 0){ $isAny = true; } else{$isAny = false; } if($isAny == false): $value['text_2t5ekhiu05p'] = ''; endif; return $value; }
March 7, 2022 at 9:06 PM #34367Catharina von Hobe
ParticipantOk Cool this works, thanks.
But two more things.
When I want to use this function several times with different MetaBox fields and groups, in this case, I would have to copy the fundtion for every group ID.function linkValidationFront($value) $functInput = $value['frontpage-head_button-link']; ... if($isAny == false): $value['frontpage-head_button-link'] = ''; endif; return $value; } function linkValidationPageBlock($value) $functInput = $value['subpage-block_button-link']; ... if($isAny == false): $value['subpage-block_button-link'] = ''; endif; return $value; }
Can I make this dynamic again, so it catches the field ID/prefix from the Fields and maybe with a second function the ID from the field that should be validated?
Secondly,
I can save the input in wordpress even on false inputs.
Is there a way to send an error Message and prevent the User from Saving like the required status in wordpress when the validation Filter returns a false input?I thought, emptying out that field would trigger the required status of that field, but it seems that this process is applyed after the completion of the saving process.
March 8, 2022 at 1:19 PM #34389Long Nguyen
ModeratorHi,
- The custom sanitize callback also supports passing 4 parameters
$value, $field, $old_value, $object_id
. You can create more conditions to check the field ID as well. - Sanitization does not support this. You can use the JS validation to prevent saving data if not passing the validation.
function linkValidation( $value, $field, $old_value, $object_id ) { if( $field['id'] == 'field-a') { $value = 'a'; } if( $field['id'] == 'field-b') { $value = 'b'; } return $value; }
Read more on the documentation
https://docs.metabox.io/sanitization/#custom-sanitize-callback
https://docs.metabox.io/validation/#advanced-validation-with-jquery-validation-pluginMarch 9, 2022 at 5:04 PM #34425Catharina von Hobe
ParticipantOk, that helps but I think its not the best way.
Can I add my own values to these 4 parameters or am I limited to theses?
So the function could look like thisfunction linkValidation( $value, $field, $old_value, $object_id, $input_to_sanitize ) { if(!epmty()){ $functInput = $value[$input_to_sanitize]; } else{ $functInput = $value } ... return $value; }
And how could I enter the additional Input if this is possible?
Or $input_to_sanitize as an array for multiple inputs.
March 10, 2022 at 8:54 PM #34453Long Nguyen
ModeratorHi,
No, the validation callback only accepts 4 parameters. You cannot add your own parameters to pass the multiple inputs.
- The custom sanitize callback also supports passing 4 parameters
-
AuthorPosts
- You must be logged in to reply to this topic.