Skip to content

Commit

Permalink
Limit contact.username and .contact_address length (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov authored Jul 5, 2024
1 parent cc71c7d commit ff43ea3
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions library/Notifications/Web/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ipl\Stdlib\Filter;
use ipl\Validator\CallbackValidator;
use ipl\Validator\EmailAddressValidator;
use ipl\Validator\StringLengthValidator;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Compat\CompatForm;

Expand Down Expand Up @@ -95,20 +96,25 @@ protected function assemble()
'username',
[
'label' => $this->translate('Username'),
'validators' => [new CallbackValidator(function ($value, $validator) {
$contact = Contact::on($this->db)->filter(Filter::equal('username', $value));
if ($this->contactId) {
$contact->filter(Filter::unequal('id', $this->contactId));
}

if ($contact->first() !== null) {
$validator->addMessage($this->translate('A contact with the same username already exists.'));

return false;
}

return true;
})]
'validators' => [
new StringLengthValidator(['max' => 254]),
new CallbackValidator(function ($value, $validator) {
$contact = Contact::on($this->db)->filter(Filter::equal('username', $value));
if ($this->contactId) {
$contact->filter(Filter::unequal('id', $this->contactId));
}

if ($contact->first() !== null) {
$validator->addMessage($this->translate(
'A contact with the same username already exists.'
));

return false;
}

return true;
})
]
]
)->addElement(
'select',
Expand Down Expand Up @@ -283,7 +289,11 @@ private function addAddressElements(): void
$this->addElement($address);

foreach ($plugins as $type => $label) {
$element = $this->createElement('text', $type, ['label' => $label]);
$element = $this->createElement('text', $type, [
'label' => $label,
'validators' => [new StringLengthValidator(['max' => 255])]
]);

if ($type === 'email') {
$element->addAttributes(['validators' => [new EmailAddressValidator()]]);
}
Expand Down

0 comments on commit ff43ea3

Please sign in to comment.