BP_XProfile_Field_Type::is_valid( string|array $values )
Check the given string against the registered formats for this field type.
Description
This method doesn’t support chaining.
Parameters
- $values
-
(Required) Value to check against the registered formats.
Return
(bool) True if the value validates
Source
File: bp-xprofile/classes/class-bp-xprofile-field-type.php
public function is_valid( $values ) {
$validated = false;
// Some types of field (e.g. multi-selectbox) may have multiple values to check.
foreach ( (array) $values as $value ) {
// Validate the $value against the type's accepted format(s).
foreach ( $this->validation_regex as $format ) {
if ( 1 === preg_match( $format, $value ) ) {
$validated = true;
continue;
} else {
$validated = false;
}
}
}
// Handle field types with accepts_null_value set if $values is an empty array.
if ( ( false === $validated ) && is_array( $values ) && empty( $values ) && $this->accepts_null_value ) {
$validated = true;
}
// If there's a whitelist set, make sure that each value is a whitelisted value.
if ( ( true === $validated ) && ! empty( $values ) && ! empty( $this->validation_whitelist ) ) {
foreach ( (array) $values as $value ) {
if ( ! in_array( $value, $this->validation_whitelist, true ) ) {
$validated = false;
break;
}
}
}
/**
* Filters whether or not field type is a valid format.
*
* @since BuddyPress 2.0.0
*
* @param bool $validated Whether or not the field type is valid.
* @param string|array $values Value to check against the registered formats.
* @param BP_XProfile_Field_Type $this Current instance of the BP_XProfile_Field_Type class.
*/
return (bool) apply_filters( 'bp_xprofile_field_type_is_valid', $validated, $values, $this );
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 2.0.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.