Skip to content

Commit 57b0b19

Browse files
committed
Check if actual checkbox value equals checkedValue
1 parent 07f2fea commit 57b0b19

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/FormElement/CheckboxElement.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,28 @@ public function renderUnwrapped()
121121

122122
return (new HiddenElement($this->getValueOfNameAttribute(), ['value' => $this->getUncheckedValue()])) . $html;
123123
}
124+
125+
/**
126+
* Validate the element using all registered validators
127+
*
128+
* @return $this
129+
*/
130+
public function validate()
131+
{
132+
$this->ensureAssembled();
133+
134+
if (! $this->hasValue() || $this->getValue() !== $this->getCheckedValue()) {
135+
if ($this->isRequired()) {
136+
$this->setMessages([$this->translate('This field is required.')]);
137+
$this->valid = false;
138+
} else {
139+
$this->valid = true;
140+
}
141+
} else {
142+
$this->valid = $this->getValidators()->isValid($this->getValue());
143+
$this->setMessages($this->getValidators()->getMessages());
144+
}
145+
146+
return $this;
147+
}
124148
}

0 commit comments

Comments
 (0)