LLMS_Form_Validator::validate_fields( array $posted_data, array[] $fields )

Validate submitted field values.


Parameters Parameters

$posted_data

(array) (Required) Array of posted data.

$fields

(array[]) (Required) Array of LifterLMS Form Fields.


Top ↑

Return Return

(WP_Error|true)


Top ↑

Source Source

File: includes/forms/class-llms-form-validator.php

	public function validate_fields( $posted_data, $fields ) {

		if ( empty( $posted_data ) && ! empty( $fields ) ) {
			return new WP_Error( 'llms-form-no-input', __( 'Cannot validate a form with no user input.', 'lifterlms' ) );
		}

		$err      = new WP_Error();
		$err_data = array();
		foreach ( $fields as $field ) {

			if ( empty( $field['name'] ) || empty( $posted_data[ $field['name'] ] ) ) {
				continue;
			}

			$valid = $this->validate_field( $posted_data[ $field['name'] ], $field );
			if ( is_wp_error( $valid ) ) {
				$err->add( $valid->get_error_code(), $valid->get_error_message() );
				$err_data[ $field['name'] ] = $field;
			}
		}

		if ( $err->errors ) {
			$err->add_data( $err_data );
			return $err;
		}

		return true;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.1.0 Don't validate form with no user input only if the form is not empty itself (e.g. contains only invisible fields).
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.