Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

acf-text-field maxlength check failing #132

Open
flameoftheforest opened this issue Mar 19, 2019 · 0 comments
Open

acf-text-field maxlength check failing #132

flameoftheforest opened this issue Mar 19, 2019 · 0 comments

Comments

@flameoftheforest
Copy link

flameoftheforest commented Mar 19, 2019

Hi,

On publishing, the class-acf-field-text.php/acf-field-text::validate_field checks the $field['maxlength'].

It is not able to pre-filter the value through acf-qtranslate.

The acf-field-text also does not have a hook to interface with this.

Here's my workaround which I put in my theme/functions.php

add_filter('acf/validate_value', 'qtranslate_interfaced_validation', 10, 4);
function qtranslate_interfaced_validation($valid, $value, $field, $input) {
	// class-acf-field-text.php/acf-field-text::validate_field checks the $field['maxlength']
	// but is not able to pre-filter it's value through acf-qtranslate
	// The acf-field-text also does not have a hook to interface with this.
	//
	// Here's a workaround:
	// If $valid is 'Value must not exceed'-prefixed
	// and $field['maxlength'] exists, we can be rather certain tat acf-field-text::validate_field has
	// failed check.
	// Perform the translation extraction and redo the check again.
	//
	// ///////////////////////////////////////////////////////////////////////////////////////
	$prefix = 'Value must not exceed';
	if (!function_exists('qtranxf_getLanguage')) return $valid;
	if (!array_key_exists('maxlength', $field)) return $valid;
	if (!is_string($valid)) return $valid;
	if (!(substr($valid, 0, strlen($prefix)) === $prefix)) return $valid;
	$active_language = apply_filters('acf_qtranslate_get_active_language', qtrans_getLanguage());
	$value = explode("[:{$active_language}]", $value)[1];
	$value = explode("[:", $value)[0];
	if (strlen($value) > $field['maxlength']) {
		return sprintf( __('Value must not exceed %d characters', 'acf'), $field['maxlength'] );
	}
	return true;
}

It would be really great if you can address this in acf-qtranslate please.

Thanks!
Bryan
Hillsong International

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant