acf_field_flexible_content::validate_value()publicACF 5.0.0

validate_value

description

@type    function

{} Это метод класса: acf_field_flexible_content{}

Хуков нет.

Возвращает

$post_id. (int)

Использование

$acf_field_flexible_content = new acf_field_flexible_content();
$acf_field_flexible_content->validate_value( $valid, $value, $field, $input );
$valid (обязательный)
-
$value (обязательный)
-
$field (обязательный)
-
$input (обязательный)
-

Список изменений

С версии 5.0.0 Введена.

Код acf_field_flexible_content::validate_value() ACF 5.10.2

function validate_value( $valid, $value, $field, $input ) {

	// vars
	$count = 0;

	// check if is value (may be empty string)
	if ( is_array( $value ) ) {

		// remove acfcloneindex
		if ( isset( $value['acfcloneindex'] ) ) {
			unset( $value['acfcloneindex'] );
		}

		// count
		$count = count( $value );
	}

	// validate required
	if ( $field['required'] && ! $count ) {
		$valid = false;
	}

	// validate min
	$min = (int) $field['min'];
	if ( $min && $count < $min ) {

		// vars
		$error      = __( 'This field requires at least {min} {label} {identifier}', 'acf' );
		$identifier = _n( 'layout', 'layouts', $min );

		// replace
		$error = str_replace( '{min}', $min, $error );
		$error = str_replace( '{label}', '', $error );
		$error = str_replace( '{identifier}', $identifier, $error );

		// return
		return $error;
	}

	// find layouts
	$layouts = array();
	foreach ( array_keys( $field['layouts'] ) as $i ) {

		// vars
		$layout = $field['layouts'][ $i ];

		// add count
		$layout['count'] = 0;

		// append
		$layouts[ $layout['name'] ] = $layout;
	}

	// validate value
	if ( $count ) {

		// loop rows
		foreach ( $value as $i => $row ) {

			// get layout
			$l = $row['acf_fc_layout'];

			// bail if layout doesn't exist
			if ( ! isset( $layouts[ $l ] ) ) {
				continue;
			}

			// increase count
			$layouts[ $l ]['count']++;

			// bail if no sub fields
			if ( empty( $layouts[ $l ]['sub_fields'] ) ) {
				continue;
			}

			// loop sub fields
			foreach ( $layouts[ $l ]['sub_fields'] as $sub_field ) {

				// get sub field key
				$k = $sub_field['key'];

				// bail if no value
				if ( ! isset( $value[ $i ][ $k ] ) ) {
					continue;
				}

				// validate
				acf_validate_value( $value[ $i ][ $k ], $sub_field, "{$input}[{$i}][{$k}]" );
			}
			// end loop sub fields

		}
		// end loop rows
	}

	// validate layouts
	foreach ( $layouts as $layout ) {

		// validate min / max
		$min   = (int) $layout['min'];
		$count = $layout['count'];
		$label = $layout['label'];

		if ( $min && $count < $min ) {

			// vars
			$error      = __( 'This field requires at least {min} {label} {identifier}', 'acf' );
			$identifier = _n( 'layout', 'layouts', $min );

			// replace
			$error = str_replace( '{min}', $min, $error );
			$error = str_replace( '{label}', '"' . $label . '"', $error );
			$error = str_replace( '{identifier}', $identifier, $error );

			// return
			return $error;
		}
	}

	// return
	return $valid;
}