Skip to content

Commit 8b5a43e

Browse files
committed
add a agree terms checkbox
1 parent d262c2c commit 8b5a43e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Console\Command\Command;
3333
use Symfony\Component\Console\Input\InputInterface;
3434
use Symfony\Component\Form\AbstractType;
35+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
3536
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
3637
use Symfony\Component\Routing\RouterInterface;
3738
use Symfony\Component\Validator\Validation;
@@ -311,6 +312,17 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
311312
'max' => 4096,
312313
]),
313314
],
315+
EOF
316+
],
317+
'agreeTerms' => [
318+
'type' => CheckboxType::class,
319+
'options_code' => <<<EOF
320+
'mapped' => false,
321+
'constraints' => [
322+
new IsTrue([
323+
'message' => 'You should agree to our terms.',
324+
]),
325+
],
314326
EOF
315327
],
316328
];
@@ -322,6 +334,7 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
322334
[
323335
'Symfony\Component\Validator\Constraints\NotBlank',
324336
'Symfony\Component\Validator\Constraints\Length',
337+
'Symfony\Component\Validator\Constraints\IsTrue',
325338
]
326339
);
327340

src/Resources/skeleton/registration/twig_template.tpl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{ form_start(registrationForm) }}
77
{{ form_row(registrationForm.<?= $username_field ?>) }}
88
{{ form_row(registrationForm.plainPassword) }}
9+
{{ form_row(registrationForm.agreeTerms) }}
910

1011
<button class="btn">Register</button>
1112
{{ form_end(registrationForm) }}

tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Doctrine\ORM\EntityManager;
66
use App\Entity\User;
77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8-
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
98

109
class RegistrationFormTest extends WebTestCase
1110
{
@@ -24,6 +23,7 @@ public function testRegistrationSuccessful()
2423
$form = $crawler->selectButton('Register')->form();
2524
$form['registration_form[email]'] = '[email protected]';
2625
$form['registration_form[plainPassword]'] = '1234yaaay';
26+
$form['registration_form[agreeTerms]'] = true;
2727
$client->submit($form);
2828

2929
$this->assertSame(302, $client->getResponse()->getStatusCode());
@@ -63,5 +63,9 @@ public function testRegistrationValidationError()
6363
'Your password should be at least 6 characters',
6464
$client->getResponse()->getContent()
6565
);
66+
$this->assertContains(
67+
'You should agree to our terms.',
68+
$client->getResponse()->getContent()
69+
);
6670
}
6771
}

0 commit comments

Comments
 (0)