Skip to content

Commit

Permalink
Merge pull request #1700 from tomudding/fix/text-field-not-required-s…
Browse files Browse the repository at this point in the history
…ign-ups

Add validator to `Text` type sign-up list field
  • Loading branch information
tomudding authored Sep 9, 2023
2 parents 777948a + 0f862b1 commit 3b94070
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
55 changes: 55 additions & 0 deletions module/Activity/src/Form/Element/ValidatedText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Activity\Form\Element;

use Laminas\Filter\StringTrim;
use Laminas\Form\Element\Text;
use Laminas\InputFilter\InputProviderInterface;
use Laminas\Validator\NotEmpty;
use Laminas\Validator\ValidatorInterface;

class ValidatedText extends Text implements InputProviderInterface
{
protected ?ValidatorInterface $validator = null;

/**
* Get primary validator.
*/
protected function getValidator(): ValidatorInterface
{
if (null === $this->validator) {
$this->validator = new NotEmpty(NotEmpty::STRING);
}

return $this->validator;
}

/**
* Provide default input rules for this element
*
* Attaches a not empty validator and a string trim filter.
*
* @inheritDoc
*/
public function getInputSpecification(): array
{
$spec = [
'required' => true,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [
$this->getValidator(),
],
];

$name = $this->getName();
if (null !== $name) {
$spec['name'] = $name;
}

return $spec;
}
}
3 changes: 2 additions & 1 deletion module/Activity/src/Form/Signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Activity\Form;

use Activity\Form\Element\ValidatedText;
use Activity\Model\SignupField as SignupFieldModel;
use Activity\Model\SignupList as SignupListModel;
use Laminas\Captcha\Image as ImageCaptcha;
Expand Down Expand Up @@ -135,7 +136,7 @@ protected function createSignupFieldElementArray(SignupFieldModel $field): array

switch ($field->getType()) {
case 0: //'Text'
$result['type'] = 'Text';
$result['type'] = ValidatedText::class;
break;
case 1: //'Yes/No'
$result['type'] = Radio::class;
Expand Down
3 changes: 2 additions & 1 deletion module/Activity/view/partial/signupForm.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function formElementRender(
}

$result = '<div class="form-group' . (count($element->getMessages()) > 0 ? ' has-error' : '') . '">';
$result .= '<label for="' . $element->getAttribute('id') . '" class="control-label">' . $label . '</label>';
$result .= '<label for="' . $element->getAttribute('id') . '" class="control-label label-required">' . $label . '</label>';

if (in_array($elementType, ['Radio'])) {
$context->formRadio()->setSeparator('</div><div class="radio">');
Expand All @@ -78,6 +78,7 @@ function formElementRender(

<?php
$form->setAttribute('action', $submitUrl);
$form->setAttribute('class', 'form-signup');
$form->prepare();
echo $this->form()->openTag($form);
?>
Expand Down
2 changes: 1 addition & 1 deletion public/css/gewis-theme.css

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions public/scss/modules/_activity-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
@import 'form';
}

.form-signup {
@import 'form';
}

.container > section {
padding: 0;
}

0 comments on commit 3b94070

Please sign in to comment.