LLMS_Form_Validator::validate_field( mixed $posted_value, array $field )
Validate a posted value
Parameters Parameters
- $posted_value
-
(mixed) (Required) Posted data.
- $field
-
(array) (Required) LifterLMS Form Field settings array.
Return Return
(WP_Error|true)
Source Source
File: includes/forms/class-llms-form-validator.php
public function validate_field( $posted_value, $field ) {
// Validate field by type.
$type_map = array(
'email' => array( $this, 'validate_field_email' ),
'number' => array( $this, 'validate_field_number' ),
'tel' => array( $this, 'validate_field_tel' ),
'url' => array( $this, 'validate_field_url' ),
);
// Turn the submitted value into array, so to unify validation of scalar and array posted values.
$to_validate = is_array( $posted_value ) ? $posted_value : array( $posted_value );
foreach ( $to_validate as $value ) {
$valid = isset( $type_map[ $field['type'] ] ) ? call_user_func( $type_map[ $field['type'] ], $value, $field ) : true;
if ( is_wp_error( $valid ) ) { // Return as soon as a field is not valid.
return $valid;
}
// HTML Attribute Validations.
if ( ! empty( $field['attributes']['minlength'] ) ) {
$valid = $this->validate_field_attribute_minlength( $value, $field['attributes']['minlength'], $field );
if ( is_wp_error( $valid ) ) {
return $valid;
}
}
}
// Perform special validations for special field types (scalar by their nature).
$extra_map = array(
'llms_voucher' => array( $this, 'validate_field_voucher' ),
'password_current' => array( $this, 'validate_field_current_password' ),
'user_email' => array( $this, 'validate_field_user_email' ),
'user_login' => array( $this, 'validate_field_user_login' ),
);
$valid = isset( $extra_map[ $field['id'] ] ) ? call_user_func( $extra_map[ $field['id'] ], $posted_value ) : true;
if ( is_wp_error( $valid ) ) {
return $valid;
}
return true;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |