powermail icon indicating copy to clipboard operation
powermail copied to clipboard

Error by PrefillMultiFieldViewHelper - false values after backend validation errors

Open GANDJUBAS opened this issue 5 years ago • 2 comments

Problem: We have some radio fields with some values (e.g. 0,1,2,3). Value 2 is preselected. In the GUI we select radio with value 1 and submit our form. Our backend validator notes some errors cause other fields are incorrect. We see error description and value 2 is optically selected by our radios again. In html code we can see that both option (1 and 2) are cheched.

Reason: Boolean value "false" is unhandled in the function buildSelectedValue. Option can not be unchecked if some prefill or other setting exists. It overwrite this.

My Solution:

protected function buildSelectedValue()
{
	$selected = $this->getFromMail();

	if ($selected === null) {
		$selected = $this->getFromMarker();
	}
	if ($selected === null) {
		$selected = $this->getFromRawMarker();
	}
	if ($selected === null) {
		$selected = $this->getFromFieldUid();
	}
	if ($selected === null) {
		$selected = $this->getFromOldPowermailFieldUid();
	}
	if ($selected === null) {
		$selected = $this->getFromFrontendUser();
	}
	if ($selected === null) {
		$selected = $this->getFromPrefillValue();
	}
	if ($selected === null) {
		$selected = $this->getFromTypoScriptContentObject();
	}
	if ($selected === null) {
		$selected = $this->getFromTypoScriptRaw();
	}
	if ($selected === null) {
		$selected = $this->getFromSession();
	}
	$this->setSelected($selected ?? false);
}

...

protected function getFromMail()
{
	$selected = null;
	if ($this->getMail() !== null && $this->getMail()->getAnswers()) {
		$selected = false;
		foreach ($this->getMail()->getAnswers() as $answer) {
			if ($answer->getField() === $this->getField()) {
				$values = $answer->getValue();
				foreach ((array)$values as $value) {
					if ($value === $this->options[$this->index]['value'] ||
						$value === $this->options[$this->index]['label']
					) {
						return true;
					}
				}
			}
		}
	}
	return $selected;
}

...

protected function getFromMarker()
{
	$selected = null;
	if (isset($this->variables['field'][$this->getMarker()])) {
		$selected = false;
		if (is_array($this->variables['field'][$this->getMarker()])) {
			foreach (array_keys($this->variables['field'][$this->getMarker()]) as $key) {
				if ($this->variables['field'][$this->getMarker()][$key] === $this->options[$this->index]['value'] ||
					$this->variables['field'][$this->getMarker()][$key] === $this->options[$this->index]['label']
				) {
					return true;
				}
			}
		} else {
			if ($this->variables['field'][$this->getMarker()] === $this->options[$this->index]['value'] ||
				$this->variables['field'][$this->getMarker()] === $this->options[$this->index]['label']
			) {
				return true;
			}
		}
	}
	return $selected;
}

//and so on

PS: PrefillFieldViewHelper has the same Problem. If we have some prefill value by text field an we try to save empty string we get out prefill value by validation error again.

GANDJUBAS avatar May 10 '19 09:05 GANDJUBAS

Thanks for your findings and your explanation. :-)

Patch is coming.

mschwemer avatar Dec 04 '20 13:12 mschwemer

We need a better solution for this ... therefore I reverted the commit.

mschwemer avatar Jan 25 '21 16:01 mschwemer