Textfield::filter_json( array $data, awsmug\Torro_Forms\DB_Objects\Elements\Element $element, awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null $submission = null )

Filters the array representation of a given element of this type.

Description

See also

Parameters

$data

(array) (Required) Element data to filter.

$element

(awsmug\Torro_Forms\DB_Objects\Elements\Element) (Required) The element object to get the data for.

$submission

(awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null) (Optional) Submission to get the values from, if available.

Default value: null

Return

(array) Array including all information for the element type.

Source

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

	public function filter_json( $data, $element, $submission = null ) {
		$settings = $this->get_settings( $element );

		$input_type = ! empty( $settings['input_type'] ) ? $settings['input_type'] : 'text';
		$input_type = $this->get_input_type( $input_type );
		if ( $input_type && isset( $input_type['html_field_type'] ) ) {
			$input_type = $input_type['html_field_type'];
		} else {
			$input_type = 'text';
		}

		$data['input_attrs']['type'] = $input_type;

		$data = parent::filter_json( $data, $element, $submission );

		$limits_text = '';
		if ( ! empty( $settings['min_length'] ) && ! empty( $settings['max_length'] ) ) {
			/* translators: 1: minimum length, 2: maximum length */
			$limits_text = sprintf( __( 'Between %1$s and %2$s characters are required.', 'torro-forms' ), number_format_i18n( $settings['min_length'] ), number_format_i18n( $settings['max_length'] ) );
		} elseif ( ! empty( $settings['min_length'] ) ) {
			/* translators: %s: minimum length */
			$limits_text = sprintf( __( 'At least %s characters are required.', 'torro-forms' ), number_format_i18n( $settings['min_length'] ) );
		} elseif ( ! empty( $settings['max_length'] ) ) {
			/* translators: %s: maximum length */
			$limits_text = sprintf( __( 'A maximum of %s characters are allowed.', 'torro-forms' ), number_format_i18n( $settings['max_length'] ) );
		}

		if ( ! empty( $limits_text ) ) {
			if ( ! empty( $data['description'] ) ) {
				$data['description'] .= '<br>';
			} else {
				$data['description'] = '';
				$data['input_attrs']['aria-describedby'] = $data['description_attrs']['id'];
			}

			$data['description'] .= $limits_text;
		}

		return $data;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.