nette / forms

📝 Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.

Home Page:https://doc.nette.org/forms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validator::validateEqual returns always true if control value is empty array

woytam opened this issue · comments

Version: master branch

Bug Description

When using addConditionOn() with MultichoiceControl and the value ($multichoicecontrol->getValue()) is empty array [], condition is always true.

Steps To Reproduce

$form->addCheckboxList('multi', 'Label', ['key' => ''])
	->setValue([]);

$form->addText('textName')
	->addConditionOn($form['multi'], $form::EQUAL, ['key'])
		->setRequired();

textName is always required.

Expected Behavior

textName required only if key from checkboxList is selected.

Possible Solution

The problem is in validator.php, where if control value ($value = $checkboxList->getValue()) returns empty array, it is considered as equal.
I have found problematic code in

foreach ((is_array($value) ? $value : [$value]) as $val) {

where foreach evaluates each return value. But in case of $value is empty array, foreach never occurs.