Support Forum
Support › Meta Box Group › Custom Sanitization in Group subfields
Hi,
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?
Hi,
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;
}
Ok 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.
Hi,
$value, $field, $old_value, $object_id
. You can create more conditions to check the field ID as well.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-plugin
Ok, 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 this
function 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.
Hi,
No, the validation callback only accepts 4 parameters. You cannot add your own parameters to pass the multiple inputs.