dozent_form_validate( array $config = array() )

Dozent provides a great approaches to validate your incoming data.


Description

This function provides a convenient way to validate incoming HTTP requests with a variety of powerful validation rules.

Example usage:

   $config = apply_filters( 'dozent_user_signup_form_validation_rules', [
       [
           'field' => 'first_name',
           'label' => __( 'First Name', 'dozent' ),
           'rules' => 'required',
       ],
       [
           'field' => 'last_name',
           'label' => __( 'Last Name', 'dozent' ),
           'rules' => 'required',
       ],
       [
           'field' => 'email',
           'label' => __( 'E-Mail', 'dozent' ),
           'rules' => 'required|email',
           'errors' => [
               'email' => __( 'You must provide a valid %s.', 'dozent'),
           ],
       ],
       [
           'field' => 'user_login',
           'label' => __( 'User Login', 'dozent' ),
           'rules' => 'required',
       ],
       [
           'field' => 'password',
           'label' => __( 'Password', 'dozent' ),
           'rules' => 'required|confirm',
       ],
       [
           'field' => 'password_confirmation',
           'label' => __( 'Password Confirmation', 'dozent' ),
           'rules' => 'required',
       ],
   ] );

   $validator = dozent_form_validate( $config );

//If validator fail, stop script

if ( ! $validator->success ){ return; }

Available Rules

required The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true: The value is null. The value is an empty string. The value is an empty array or empty Countable object. The value is an uploaded file with no path.

email The field under validation must be formatted as an E-Mail address.

confirm The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.


Parameters

$config

(array) (Optional) Rules config

Default value: array()


Return

(object)


Source

File: includes/form-functions.php

    function dozent_form_validate( $config = [] ) {
        $response = [
            'success' => true,
            'errors'  => [],
        ];

        $errors = [];

        if ( dozent_count( $config ) ) {

            foreach ( $config as $item ) {

                $field = dozent_array_get( 'field', $item );
                $label = dozent_array_get( 'label', $item );
                $rules = dozent_array_get( 'rules', $item );
                $rules = array_filter( explode( '|', $rules ) );

                foreach ( $rules as $rule ) {

                    $field_value         = dozent_input_array_field( $field );
                    $given_error_message = dozent_array_get( 'errors.' . $rule, $item );
                    $validate            = true;

                    $default_error_msg = '';

                    switch ( $rule ) {
                        case 'required' :
                            if ( ! $field_value ) {
                                $validate          = false;
                                $default_error_msg = sprintf( __( '%s field is required', 'dozent' ), $label );
                            }

                            break;

                        case 'email' :

                            if ( ! filter_var( $field_value, FILTER_VALIDATE_EMAIL ) ) {
                                $validate          = false;
                                $default_error_msg = sprintf( __( 'A valid %s is required', 'dozent' ), $label );
                            }

                            break;
                        case 'confirm' :

                            if ( $field_value !== dozent_input_old( $field . '_confirmation' ) ) {
                                $validate = false;

                                $default_error_msg = sprintf( __( '%1$s field does not matched with confirm %1$s field',
                                    'dozent' ), $label );
                            }

                            break;
                    }

                    /**
                     * Set Error Message
                     */
                    if ( ! $validate ) {
                        if ( $given_error_message ) {
                            $error_message = sprintf( $given_error_message, $label );
                        } else {
                            $error_message = $default_error_msg;
                        }
                        $errors[ $field ][ $rule ] = $error_message;
                    }

                }

            }

        }

        if ( dozent_count( $errors ) ) {

            $response = [
                'success' => false,
                'errors'  => $errors,
            ];

            add_filter( 'dozent_form_errors', function () use ( $errors ) {
                return $errors;
            } );

        }

        return (object) $response;
    }


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.