Support Forum
Support › Meta Box Group › Custom Sanitization CB for Cloneable Group but only targeting a few fields
Hi,
I'm still a little vague on the complete functionality of custom sanitization for cloneable groups, can someone please help?
1. In the example below will it work for all cloned items?
2. in the example below, there are many other fields in the group - will the other fields I am not targeting still be sanitized by core MB santization routines for field types?
3. What is the best way I can mark fields that I want to use for this specific validation so I don't need to use their ID, I assume I can add a custom key or something like that?
4. Can I prevent saving rather than sending back '' so that the user at least knows they are doing something wrong, preferably without js?
function my_subfield_validation( $group ) {
if ( $group['my_key_id'] === 'evil text' ) {
$group['my_key_id'] = '';
}
return $group;
}
5. The reason for this question is that I allow admins to enter css unit attributes with unit amount - eg, 20px, 20em, 0, clamp, calc to save on fields and make it more powerful. I have a very specific function for this that i want to call from the callback or use in the callback for this exact field usage but I still want all other fields in the group to undergo core MB sanitization. Perhaps there's a more ideal field type for this then text?
thanks very much in advance.
Hello Dave,
Let me answer your questions.
1. You can follow this topic to understand how to use sanitize for cloneable group
https://support.metabox.io/topic/custom-sanitization-cloneable-group-php-error/#post-39508
2, 5. No, by default, subfields in a group field are not sanitized. It is noted in the documentation
https://docs.metabox.io/sanitization/#default-sanitize-callbacks
3. No, the field ID in a group field is mandatory to sanitize the value.
4. No. If you want to validate a field without reloading the page, please use the Validation feature
https://docs.metabox.io/validation/
Hi Peter, thanks so much for responding. I read that doco page but missed the bit about group fields not being sanitized. Nearly 70% of all fields I create are in groups, I never knew they weren't sanitized.
Could you please comment on the following in relation to group fields and cloneable group fields:
1. I'd like to use the html pattern with a regex to validate but entering the same regex accross many fields and groups is a pain and if it needs updating in future, that's going to be a problem. Is there a way I can apply the regex globally for the pattern attribute but still use mb builder?
2. Since I need to put sanitize callback at group level, is there a way that I can detect field type in a php group sanitize callback?
3. Similar to my question above (#2) but for non generic field sanitizing, is there a way I could mark fields (perhaps using custom attribute or something like that?), so that my php group callback can detect these fields and call a specific custom sanitize function for these exact fields?
Just trying to cut back on repetitive code and maintenance, as referring to fields by name is really cumbersome...
Thanks in advance...
Cheers.
Hello,
1. If you use the builder, there isn't an option to apply the validation globally. I suggest you use the filter rwmb_normalize_field
to add a setting to all fields.
https://docs.metabox.io/filters/rwmb-normalize-field/
2. You can use the function rwmb_get_field_settings()
to get all settings of the group field.
https://docs.metabox.io/functions/rwmb-get-field-settings/
3. Similar to #1, you can add a specific prefix to the field ID (like abcd_fieldID) and check this (abcd) to add a setting to those fields. It would require some custom code to do so.
Hi Peter,
Thanks for these suggestions.
I think I'll need to experiment with these methods and see how I go.
If I use a prefix to ID via any method, I assume that any reference to that field will always require the prefix to address it right?
Cheers,
Yes, if you want to apply a function to some specific fields, using the field prefix is a better way to do so.