BP_XProfile_Field::admin_validate()
Validate form field data on sumbission.
Description
Return
(boolean)
Source
File: bp-xprofile/classes/class-bp-xprofile-field.php
public static function admin_validate() {
global $message;
// Check field name.
if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) {
$message = esc_html__( 'Profile fields must have a name.', 'buddyboss' );
return false;
}
// Check field requirement.
if ( ! isset( $_POST['required'] ) ) {
$message = esc_html__( 'Profile field requirement is missing.', 'buddyboss' );
return false;
}
// Check field type.
if ( empty( $_POST['fieldtype'] ) ) {
$message = esc_html__( 'Profile field type is missing.', 'buddyboss' );
return false;
}
// Check that field is of valid type.
if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) {
$message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddyboss' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' );
return false;
}
// Get field type so we can check for and lavidate any field options.
$field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] );
// Field type requires options.
if ( true === $field_type->supports_options ) {
// Build the field option key.
$option_name = sanitize_key( $_POST['fieldtype'] ) . '_option';
// Check for missing or malformed options.
if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) {
$message = esc_html__( 'These field options are invalid.', 'buddyboss' );
return false;
}
// Trim out empty field options.
$field_values = array_values( $_POST[ $option_name ] );
$field_options = array_map( 'sanitize_text_field', $field_values );
$field_count = count( $field_options );
// Check for missing or malformed options.
if ( 0 === $field_count ) {
$message = sprintf( esc_html__( '%s require at least one option.', 'buddyboss' ), $field_type->name );
return false;
}
// If only one option exists, it cannot be an empty string.
if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) {
$message = sprintf( esc_html__( '%s require at least one option.', 'buddyboss' ), $field_type->name );
return false;
}
}
return true;
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 2.2.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.