MVC: Add allowEmpty option to UniqueConstraint

This is useful in cases where "empty" has special meaning, but is allowed
to occur multiple times, while any set value still has to adhere
to the unique constraint.
This commit is contained in:
Stephan de Wit 2023-08-22 08:50:45 +02:00
parent 720216f10e
commit 0b6317accb

View File

@ -45,9 +45,18 @@ class UniqueConstraint extends BaseConstraint
*/
public function validate($validator, $attribute): bool
{
$allowEmpty = ($this->getOption('allowEmpty') === 'Y') ? true : false;
if ($allowEmpty && !empty($this->getOptionValueList('addFields'))) {
throw new \Exception('UniqueConstraint allowEmpty and addFields cannot be used in tandem');
}
$node = $this->getOption('node');
$fieldSeparator = chr(10) . chr(0);
if ($node) {
if ($allowEmpty && empty((string)$node)) {
return true;
}
$containerNode = $node;
$nodeName = $node->getInternalXMLTagName();
$parentNode = $node->getParentNode();