Skip to content

Commit c8edde3

Browse files
authored
Merge pull request #388 from dotkernel/form-changes
Form updates
2 parents f46355c + 83a1e06 commit c8edde3

File tree

11 files changed

+119
-74
lines changed

11 files changed

+119
-74
lines changed

src/Admin/src/Form/CreateAdminForm.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Laminas\Form\Element\Select;
1414
use Laminas\Form\Element\Submit;
1515
use Laminas\Form\Element\Text;
16+
use Laminas\Form\Exception\ExceptionInterface;
1617
use Laminas\Session\Container;
1718

1819
/**
@@ -24,6 +25,7 @@ class CreateAdminForm extends AbstractForm
2425
{
2526
/**
2627
* @param array<non-empty-string, mixed> $options
28+
* @throws ExceptionInterface
2729
*/
2830
public function __construct(?string $name = null, array $options = [])
2931
{
@@ -41,16 +43,20 @@ public function __construct(?string $name = null, array $options = [])
4143

4244
/**
4345
* @phpstan-param SelectDataType[] $roles
46+
* @throws ExceptionInterface
4447
*/
45-
public function setRoles(array $roles): self
48+
public function setRoles(array $roles): static
4649
{
47-
return $this->add(
48-
(new MultiCheckbox('roles'))
49-
->setLabel('Select at least one role')
50-
->setValueOptions($roles)
51-
);
50+
$checkbox = new MultiCheckbox('roles');
51+
$checkbox->setLabel('Select at least one role');
52+
$checkbox->setValueOptions($roles);
53+
$this->add($checkbox);
54+
return $this;
5255
}
5356

57+
/**
58+
* @throws ExceptionInterface
59+
*/
5460
public function init(): void
5561
{
5662
$this->add(
@@ -76,12 +82,13 @@ public function init(): void
7682
(new Text('lastName'))
7783
->setLabel('Lastname')
7884
);
79-
$this->add(
80-
(new Select('status'))
81-
->setLabel('Account status')
82-
->setValueOptions(AdminStatusEnum::toArray())
83-
->setAttribute('required', true)
84-
);
85+
86+
$select = new Select('status');
87+
$select->setLabel('Account status');
88+
$select->setValueOptions(AdminStatusEnum::toArray());
89+
$select->setAttribute('required', true);
90+
$this->add($select);
91+
8592
$this->add(
8693
(new Csrf('adminCreateCsrf'))
8794
->setOptions([

src/Admin/src/Form/EditAdminForm.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Laminas\Form\Element\Select;
1414
use Laminas\Form\Element\Submit;
1515
use Laminas\Form\Element\Text;
16+
use Laminas\Form\Exception\ExceptionInterface;
1617
use Laminas\Session\Container;
1718

1819
/**
@@ -24,6 +25,7 @@ class EditAdminForm extends AbstractForm
2425
{
2526
/**
2627
* @param array<non-empty-string, mixed> $options
28+
* @throws ExceptionInterface
2729
*/
2830
public function __construct(?string $name = null, array $options = [])
2931
{
@@ -41,16 +43,20 @@ public function __construct(?string $name = null, array $options = [])
4143

4244
/**
4345
* @phpstan-param SelectDataType[] $roles
46+
* @throws ExceptionInterface
4447
*/
45-
public function setRoles(array $roles): self
48+
public function setRoles(array $roles): static
4649
{
47-
return $this->add(
48-
(new MultiCheckbox('roles'))
49-
->setLabel('Select at least one role')
50-
->setValueOptions($roles)
51-
);
50+
$checkbox = new MultiCheckbox('roles');
51+
$checkbox->setLabel('Select at least one role');
52+
$checkbox->setValueOptions($roles);
53+
$this->add($checkbox);
54+
return $this;
5255
}
5356

57+
/**
58+
* @throws ExceptionInterface
59+
*/
5460
public function init(): void
5561
{
5662
$this->add(
@@ -69,12 +75,13 @@ public function init(): void
6975
(new Text('lastName'))
7076
->setLabel('Lastname')
7177
);
72-
$this->add(
73-
(new Select('status'))
74-
->setLabel('Account status')
75-
->setValueOptions(AdminStatusEnum::toArray())
76-
->setAttribute('required', true)
77-
);
78+
79+
$select = new Select('status');
80+
$select->setLabel('Account status');
81+
$select->setValueOptions(AdminStatusEnum::toArray());
82+
$select->setAttribute('required', true);
83+
$this->add($select);
84+
7885
$this->add(
7986
(new Csrf('adminEditCsrf'))
8087
->setOptions([

src/Admin/src/Handler/Admin/GetEditAdminFormHandler.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Fig\Http\Message\StatusCodeInterface;
1616
use Laminas\Diactoros\Response\EmptyResponse;
1717
use Laminas\Diactoros\Response\HtmlResponse;
18+
use Laminas\Form\Exception\ExceptionInterface;
1819
use Mezzio\Router\RouterInterface;
1920
use Mezzio\Template\TemplateRendererInterface;
2021
use Psr\Http\Message\ResponseInterface;
@@ -46,6 +47,9 @@ public function __construct(
4647
) {
4748
}
4849

50+
/**
51+
* @throws ExceptionInterface
52+
*/
4953
public function handle(ServerRequestInterface $request): ResponseInterface
5054
{
5155
try {
@@ -59,7 +63,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5963
/** @var AdminRole[] $adminRoles */
6064
$adminRoles = $this->adminRoleService->getAdminRoleRepository()->findAll();
6165
$adminRoles = array_map(
62-
/** @return SelectDataType */
66+
/** @return SelectDataType */
6367
fn (AdminRole $adminRole): array => [
6468
'label' => $adminRole->getName()->value,
6569
'value' => $adminRole->getUuid()->toString(),
@@ -68,12 +72,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6872
$adminRoles
6973
);
7074

71-
$this->editAdminForm
72-
->setAttribute(
73-
'action',
74-
$this->router->generateUri('admin::edit-admin', ['uuid' => $admin->getUuid()->toString()])
75-
)
76-
->bind($admin)
75+
$this->editAdminForm->setAttribute(
76+
'action',
77+
$this->router->generateUri('admin::edit-admin', ['uuid' => $admin->getUuid()->toString()])
78+
);
79+
$this->editAdminForm->bind($admin)
7780
->setRoles($adminRoles);
7881

7982
return new HtmlResponse(

src/Admin/src/Handler/Admin/PostEditAdminHandler.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Fig\Http\Message\StatusCodeInterface;
2121
use Laminas\Diactoros\Response\EmptyResponse;
2222
use Laminas\Diactoros\Response\HtmlResponse;
23+
use Laminas\Form\Exception\ExceptionInterface;
2324
use Mezzio\Router\RouterInterface;
2425
use Mezzio\Template\TemplateRendererInterface;
2526
use Psr\Http\Message\ResponseInterface;
@@ -55,6 +56,9 @@ public function __construct(
5556
) {
5657
}
5758

59+
/**
60+
* @throws ExceptionInterface
61+
*/
5862
public function handle(ServerRequestInterface $request): ResponseInterface
5963
{
6064
try {
@@ -68,7 +72,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6872
/** @var AdminRole[] $adminRoles */
6973
$adminRoles = $this->adminRoleService->getAdminRoleRepository()->findAll();
7074
$adminRoles = array_map(
71-
/** @return SelectDataType */
75+
/** @return SelectDataType */
7276
fn (AdminRole $adminRole): array => [
7377
'label' => $adminRole->getName()->value,
7478
'value' => $adminRole->getUuid()->toString(),
@@ -77,12 +81,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7781
$adminRoles
7882
);
7983

80-
$this->editAdminForm
81-
->setAttribute(
82-
'action',
83-
$this->router->generateUri('admin::edit-admin', ['uuid' => $admin->getUuid()->toString()])
84-
)
85-
->setRoles($adminRoles);
84+
$this->editAdminForm->setAttribute(
85+
'action',
86+
$this->router->generateUri('admin::edit-admin', ['uuid' => $admin->getUuid()->toString()])
87+
);
88+
$this->editAdminForm->setRoles($adminRoles);
8689

8790
try {
8891
/** @var iterable<array<string, string|string[]>> $data */

src/Admin/src/Service/AdminService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function saveAdmin(array $data, ?Admin $admin = null): Admin
107107
if (array_key_exists('identity', $data) && $data['identity'] !== null && ! $admin->hasIdentity()) {
108108
$admin->setIdentity($data['identity']);
109109
}
110-
if (array_key_exists('password', $data) && $data['password'] !== null) {
110+
if (array_key_exists('password', $data) && $data['password'] !== null && $data['password'] !== '') {
111111
$admin->usePassword($data['password']);
112112
}
113113
if (array_key_exists('firstName', $data) && $data['firstName'] !== null) {

src/User/src/Form/CreateUserForm.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Laminas\Form\Element\Select;
1515
use Laminas\Form\Element\Submit;
1616
use Laminas\Form\Element\Text;
17+
use Laminas\Form\Exception\ExceptionInterface;
1718
use Laminas\Form\Fieldset;
1819
use Laminas\Session\Container;
1920

@@ -26,6 +27,7 @@ class CreateUserForm extends AbstractForm
2627
{
2728
/**
2829
* @param array<non-empty-string, mixed> $options
30+
* @throws ExceptionInterface
2931
*/
3032
public function __construct(?string $name = null, array $options = [])
3133
{
@@ -43,16 +45,20 @@ public function __construct(?string $name = null, array $options = [])
4345

4446
/**
4547
* @phpstan-param SelectDataType[] $roles
48+
* @throws ExceptionInterface
4649
*/
47-
public function setRoles(array $roles): self
50+
public function setRoles(array $roles): static
4851
{
49-
return $this->add(
50-
(new MultiCheckbox('roles'))
51-
->setLabel('Select at least one role')
52-
->setValueOptions($roles)
53-
);
52+
$checkbox = new MultiCheckbox('roles');
53+
$checkbox->setLabel('Select at least one role');
54+
$checkbox->setValueOptions($roles);
55+
$this->add($checkbox);
56+
return $this;
5457
}
5558

59+
/**
60+
* @throws ExceptionInterface
61+
*/
5662
public function init(): void
5763
{
5864
$this
@@ -68,11 +74,6 @@ public function init(): void
6874
(new Password('passwordConfirm'))
6975
->setLabel('Password confirm')
7076
->setAttribute('required', true)
71-
)->add(
72-
(new Select('status'))
73-
->setLabel('Account status')
74-
->setValueOptions(UserStatusEnum::toArray())
75-
->setAttribute('required', true)
7677
)->add(
7778
(new Csrf('userCreateCsrf'))
7879
->setOptions([
@@ -100,5 +101,11 @@ public function init(): void
100101
->setAttribute('required', true)
101102
)
102103
);
104+
105+
$select = new Select('status');
106+
$select->setLabel('Account status');
107+
$select->setValueOptions(UserStatusEnum::toArray());
108+
$select->setAttribute('required', true);
109+
$this->add($select);
103110
}
104111
}

src/User/src/Form/EditUserForm.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Laminas\Form\Element\Select;
1515
use Laminas\Form\Element\Submit;
1616
use Laminas\Form\Element\Text;
17+
use Laminas\Form\Exception\ExceptionInterface;
1718
use Laminas\Form\Fieldset;
1819
use Laminas\Session\Container;
1920

@@ -26,6 +27,7 @@ class EditUserForm extends AbstractForm
2627
{
2728
/**
2829
* @param array<non-empty-string, mixed> $options
30+
* @throws ExceptionInterface
2931
*/
3032
public function __construct(?string $name = null, array $options = [])
3133
{
@@ -43,16 +45,20 @@ public function __construct(?string $name = null, array $options = [])
4345

4446
/**
4547
* @phpstan-param SelectDataType[] $roles
48+
* @throws ExceptionInterface
4649
*/
47-
public function setRoles(array $roles): self
50+
public function setRoles(array $roles): static
4851
{
49-
return $this->add(
50-
(new MultiCheckbox('roles'))
51-
->setLabel('Select at least one role')
52-
->setValueOptions($roles)
53-
);
52+
$checkbox = new MultiCheckbox('roles');
53+
$checkbox->setLabel('Select at least one role');
54+
$checkbox->setValueOptions($roles);
55+
$this->add($checkbox);
56+
return $this;
5457
}
5558

59+
/**
60+
* @throws ExceptionInterface
61+
*/
5662
public function init(): void
5763
{
5864
$this
@@ -62,11 +68,6 @@ public function init(): void
6268
)->add(
6369
(new Password('passwordConfirm'))
6470
->setLabel('Password confirm')
65-
)->add(
66-
(new Select('status'))
67-
->setLabel('Account status')
68-
->setValueOptions(UserStatusEnum::toArray())
69-
->setAttribute('required', true)
7071
)->add(
7172
(new Csrf('userEditCsrf'))
7273
->setOptions([
@@ -93,5 +94,11 @@ public function init(): void
9394
->setLabel('Email')
9495
)
9596
);
97+
98+
$select = new Select('status');
99+
$select->setLabel('Account status');
100+
$select->setValueOptions(UserStatusEnum::toArray());
101+
$select->setAttribute('required', true);
102+
$this->add($select);
96103
}
97104
}

src/User/src/Handler/GetEditUserFormHandler.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Fig\Http\Message\StatusCodeInterface;
1818
use Laminas\Diactoros\Response\EmptyResponse;
1919
use Laminas\Diactoros\Response\HtmlResponse;
20+
use Laminas\Form\Exception\ExceptionInterface;
2021
use Mezzio\Router\RouterInterface;
2122
use Mezzio\Template\TemplateRendererInterface;
2223
use Psr\Http\Message\ResponseInterface;
@@ -51,6 +52,9 @@ public function __construct(
5152
) {
5253
}
5354

55+
/**
56+
* @throws ExceptionInterface
57+
*/
5458
public function handle(ServerRequestInterface $request): ResponseInterface
5559
{
5660
try {
@@ -64,7 +68,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6468
/** @var UserRole[] $userRoles */
6569
$userRoles = $this->userRoleService->getUserRoleRepository()->findAll();
6670
$userRoles = array_map(
67-
/** @return SelectDataType */
71+
/** @return SelectDataType */
6872
fn (UserRole $userRole): array => [
6973
'label' => $userRole->getName()->value,
7074
'value' => $userRole->getUuid()->toString(),
@@ -84,8 +88,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface
8488
->setAttribute(
8589
'action',
8690
$this->router->generateUri('user::edit-user', ['uuid' => $user->getUuid()->toString()])
87-
)
88-
->bind($user)
91+
);
92+
$this->editUserForm->bind($user)
8993
->setRoles($userRoles);
9094

9195
return new HtmlResponse(

0 commit comments

Comments
 (0)