-
Notifications
You must be signed in to change notification settings - Fork 3
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
MiaGordon91
wants to merge
9
commits into
main
Choose a base branch
from
DDLS-426
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+301
−17
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
00d199c
inspect user input and throw helpful error if not valid
MiaGordon91 5c704e8
add unit tests for validation against incorrect data input
MiaGordon91 d98f65a
add unit tests for validation against incorrect data input
MiaGordon91 a4ad418
Merge branch 'main' of github.com:ministryofjustice/opg-digideps into…
MiaGordon91 5d3f530
create custom symfony constraint and register as a service
MiaGordon91 d7e39e9
update validation to run across multiple properties, drop unnecessary…
MiaGordon91 fc658c6
add range validation for start date and supporting unit tests
MiaGordon91 8b66974
Merge branch 'main' into DDLS-426
MiaGordon91 f049eeb
create clearer naming convention, add unit tests for custom constrain…
MiaGordon91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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