Textfield::validate_field( mixed $value, awsmug\Torro_Forms\DB_Objects\Elements\Element $element, awsmug\Torro_Forms\DB_Objects\Submissions\Submission $submission )

Validates a field value for an element.

Description

See also

Parameters

$value

(mixed) (Required) The value to validate. It is already unslashed when it arrives here.

$element

(awsmug\Torro_Forms\DB_Objects\Elements\Element) (Required) Element to validate the field value for.

$submission

(awsmug\Torro_Forms\DB_Objects\Submissions\Submission) (Required) Submission the value belongs to.

Return

(mixed|WP_Error) Validated value, or error object on failure.

Source

File: src/db-objects/elements/element-types/base/textfield.php

	public function validate_field( $value, $element, $submission ) {
		$settings = $this->get_settings( $element );

		$value = trim( (string) $value );

		if ( ! empty( $settings['required'] ) && 'no' !== $settings['required'] && empty( $value ) ) {
			return $this->create_error( 'value_required', __( 'You must enter something here.', 'torro-forms' ), $value );
		}

		if ( ! empty( $settings['min_length'] ) && strlen( $value ) < (int) $settings['min_length'] ) {
			return $this->create_error( 'value_too_short', __( 'The value you entered is too short.', 'torro-forms' ), $value );
		}

		if ( ! empty( $settings['max_length'] ) && strlen( $value ) > (int) $settings['max_length'] ) {
			return $this->create_error( 'value_too_long', __( 'The value you entered is too long.', 'torro-forms' ), $value );
		}

		if ( ! empty( $settings['input_type'] ) ) {
			$input_type = $this->get_input_type( $settings['input_type'] );
			if ( $input_type ) {
				$status = true;
				if ( isset( $input_type['callback'] ) && $input_type['callback'] && is_callable( $input_type['callback'] ) ) {
					$status = call_user_func( $input_type['callback'], $value );
				} elseif ( isset( $input_type['pattern'] ) && $input_type['pattern'] ) {
					$status = preg_match( '/' . $input_type['pattern'] . '/i', $value );
				}

				if ( ! $status ) {
					$message = ! empty( $input_type['error_message'] ) ? $input_type['error_message'] : __( 'The value you entered is invalid.', 'torro-forms' );

					return $this->create_error( 'value_invalid', $message, $value );
				}
			}
		}

		return $value;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.