LLMS_Form_Validator::validate_field_number( string $posted_value, array $field )

Validate a number field


Description Description

Ensures the posted valued is numeric and, where applicable, ensures that the number falls within minimum and maximum value requirements.


Top ↑

Parameters Parameters

$posted_value

(string) (Required) User-submitted (dirty) value.

$field

(array) (Required) The LLMS_Form_Field settings array.


Top ↑

Return Return

(WP_Error|boolean) Returns true for a valid submission, otherwise an error.


Top ↑

Source Source

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

	protected function validate_field_number( $posted_value, $field ) {

		$temp_value = str_replace( ',', '', $posted_value );
		if ( ! is_numeric( $temp_value ) ) {
			// Translators: %1$s field label or name; %2$s = user submitted value.
			return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" is not valid number.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value ) );
		} elseif ( isset( $field['attributes'] ) ) {
			if ( ( ! empty( $field['attributes']['min'] ) || ( isset( $field['attributes']['min'] ) && '0' === $field['attributes']['min'] ) ) && $temp_value < $field['attributes']['min'] ) {
				// Translators: %1$s = field label or name; %2$s = user submitted value; %3$d = minimum allowed number.
				return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" must be greater than or equal to %3$d.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value, $field['attributes']['min'] ) );
			} elseif ( ( ! empty( $field['attributes']['max'] ) || ( isset( $field['attributes']['max'] ) && '0' === $field['attributes']['max'] ) ) && $temp_value > $field['attributes']['max'] ) {
				// Translators: %1$s = field label or name; %2$s = user submitted value; %3$d = maximum allowed number.
				return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" must be less than or equal to %3$d.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value, $field['attributes']['max'] ) );
			}
		}

		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.