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

DDLS-426 : Prevent user from inputting invalid year during registration #1781

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
11 changes: 9 additions & 2 deletions client/app/src/Form/ClientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type as FormTypes;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -30,19 +31,25 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('address5', FormTypes\TextType::class)
->add('postcode', FormTypes\TextType::class)
->add('country', FormTypes\CountryType::class, [
'preferred_choices' => ['GB'],
'placeholder' => 'country.defaultOption',
'preferred_choices' => ['GB'],
'placeholder' => 'country.defaultOption',
])
->add('phone', FormTypes\TextType::class)
->add('id', FormTypes\HiddenType::class)
->add('save', FormTypes\SubmitType::class);

// strip tags to prevent XSS as the name is often displayed around mixed with translation with the twig "raw" filter
// only allow user to enter a four digit year
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$data['firstname'] = strip_tags($data['firstname']);
$data['lastname'] = strip_tags($data['lastname']);
$event->setData($data);

if (!preg_match('/^\d{4}$/', $data['courtDate']['year'])) {
Copy link
Contributor

@Raffers Raffers Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this to some form validation rather than here. Also should complete a check that's not in the future. Maybe also too far in the past, not sure there is any valid cases in the 1800's

$form = $event->getForm();
$form->addError(new FormError('Please enter a valid four-digit year.'));
}
});
}

Expand Down
32 changes: 21 additions & 11 deletions client/app/src/Form/Report/ReportType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type as FormTypes;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ReportType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', FormTypes\HiddenType::class)
->add('startDate', FormTypes\DateType::class, ['widget' => 'text',
'input' => 'datetime',
'format' => 'yyyy-MM-dd',
'invalid_message' => 'report.startDate.invalidMessage', ])
->add('id', FormTypes\HiddenType::class)
->add('startDate', FormTypes\DateType::class, ['widget' => 'text',
'input' => 'datetime',
'format' => 'yyyy-MM-dd',
'invalid_message' => 'report.startDate.invalidMessage', ])
->add('endDate', FormTypes\DateType::class, ['widget' => 'text',
'input' => 'datetime',
'format' => 'yyyy-MM-dd',
'invalid_message' => 'report.endDate.invalidMessage',
])
->add('save', FormTypes\SubmitType::class);

->add('endDate', FormTypes\DateType::class, ['widget' => 'text',
'input' => 'datetime',
'format' => 'yyyy-MM-dd',
'invalid_message' => 'report.endDate.invalidMessage',
])
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();

->add('save', FormTypes\SubmitType::class);
if (!preg_match('/^\d{4}$/', $data['startDate']['year']) || !preg_match('/^\d{4}$/', $data['endDate']['year'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, as well

$form = $event->getForm();
$form->addError(new FormError('Please enter a valid four-digit year.'));
}
});
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
62 changes: 62 additions & 0 deletions client/app/tests/phpunit/Form/ClientTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Form;

use Symfony\Component\Form\Test\TypeTestCase;

class ClientTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$courtDate = [
'year' => '2024',
'month' => '01',
'day' => '01',
];

$formData = [
'firstname' => 'Mel',
'lastname' => 'Jones',
'caseNumber' => '0000000',
'courtDate' => $courtDate,
'address' => '10 Downing Street',
'phone' => '0123456789',
];

$form = $this->factory->create(ClientType::class);

$form->submit($formData);

$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isValid());
}

public function testSubmitInvalidYear()
{
$courtDate = [
'year' => '20',
'month' => '01',
'day' => '01',
];

$formData = [
'firstname' => 'Mel',
'lastname' => 'Jones',
'caseNumber' => '0000000',
'courtDate' => $courtDate,
'address' => '10 Downing Street',
'phone' => '0123456789',
];

$form = $this->factory->create(ClientType::class);

$form->submit($formData);
$errors = $form->getErrors();

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());

$this->assertCount(1, $errors);
$this->assertSame('Please enter a valid four-digit year.', $errors[0]->getMessage());
}
}
69 changes: 69 additions & 0 deletions client/app/tests/phpunit/Form/ReportTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Form;

use App\Form\Report\ReportType;
use Symfony\Component\Form\Test\TypeTestCase;

class ReportTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$startDate = [
'year' => '2022',
'month' => '01',
'day' => '02',
];

$endDate = [
'year' => '2023',
'month' => '01',
'day' => '02',
];

$formData = [
'id' => 1,
'startDate' => $startDate,
'endDate' => $endDate,
];

$form = $this->factory->create(ReportType::class);

$form->submit($formData);

$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isValid());
}

public function testSubmitInvalidData()
{
$startDate = [
'year' => '22',
'month' => '01',
'day' => '02',
];

$endDate = [
'year' => '23',
'month' => '01',
'day' => '02',
];

$formData = [
'id' => 1,
'startDate' => $startDate,
'endDate' => $endDate,
];

$form = $this->factory->create(ReportType::class);

$form->submit($formData);
$errors = $form->getErrors();

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());

$this->assertCount(1, $errors);
$this->assertSame('Please enter a valid four-digit year.', $errors[0]->getMessage());
}
}
Loading