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

Ensure matching fields match one another.


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_matching_fields( $posted_data, $fields ) {

		$err      = new WP_Error();
		$err_data = array();

		$matches = array();
		foreach ( $fields as $field ) {

			// Field doesn't have a match to check or it was already checked by it's match.
			if ( empty( $field['match'] ) || in_array( $field['id'], $matches, true ) ) {
				continue;
			}

			$field_name = isset( $field['label'] ) ? $field['label'] : $field['name'];

			$name        = $field['name'];
			$match_field = LLMS_Forms::instance()->get_field_by( $fields, 'id', $field['match'] );
			if ( ! $match_field ) {
				continue;
			}

			$match = $match_field['name'];

			$val   = isset( $posted_data[ $name ] ) ? $posted_data[ $name ] : '';
			$match = isset( $posted_data[ $match ] ) ? $posted_data[ $match ] : '';

			if ( $val !== $match ) {

				$match_name = isset( $match_field['label'] ) ? $match_field['label'] : $match_field['name'];
				$err->add( 'llms-form-field-not-matched', sprintf( __( '%1$s must match %2$s.', 'lifterlms' ), $field_name, $match_name ) );
				$err_data[] = array( $field, $match_field );

			}

			// Fields reference each other so we only need to check the pair one time.
			$matches[] = $match_field['id'];

		}

		if ( $err->errors ) {
			$err->add_data( $err_data, 'llms-form-field-not-matched' );
			return $err;
		}

		return true;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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