Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit contact.username and .contact_address length #221

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading