Skip to content

Commit d026efc

Browse files
author
Andrei
committed
email required and name not required in user registration
1 parent 49ed6ef commit d026efc

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/User/src/Entity/UserDetail.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class UserDetail extends AbstractEntity
2727
* @ORM\Column(name="firstName", type="string", length=191, nullable=true)
2828
* @var string|null $firstName
2929
*/
30-
protected ?string $firstName;
30+
protected ?string $firstName = null;
3131

3232
/**
3333
* @ORM\Column(name="lastName", type="string", length=191, nullable=true)
3434
* @var string|null $lastName
3535
*/
36-
protected ?string $lastName;
36+
protected ?string $lastName = null;
3737

3838
/**
3939
* @ORM\Column(name="email", type="string", length=191, nullable=true)
@@ -80,7 +80,7 @@ public function getFirstName(): ?string
8080
* @param $firstName
8181
* @return $this
8282
*/
83-
public function setFirstName($firstName): self
83+
public function setFirstName(?string $firstName): self
8484
{
8585
$this->firstName = $firstName;
8686

@@ -99,7 +99,7 @@ public function getLastName(): ?string
9999
* @param $lastName
100100
* @return $this
101101
*/
102-
public function setLastName($lastName): self
102+
public function setLastName(?string $lastName): self
103103
{
104104
$this->lastName = $lastName;
105105

src/User/src/Form/InputFilter/CreateDetailInputFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getInputFilter(): InputFilterInterface
4444
]
4545
])->add([
4646
'name' => 'email',
47-
'required' => false,
47+
'required' => true,
4848
'filters' => [
4949
['name' => StringTrim::class],
5050
['name' => StripTags::class]

src/User/src/Service/UserService.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,22 @@ public function createUser(array $data = []): User
111111
if ($this->exists($data['identity'])) {
112112
throw new ORMException(Message::DUPLICATE_IDENTITY);
113113
}
114+
if ($this->emailExists($data['detail']['email'])) {
115+
throw new ORMException(Message::DUPLICATE_EMAIL);
116+
}
114117
$user = new User();
115118
$user->setPassword(password_hash($data['password'], PASSWORD_DEFAULT))->setIdentity($data['identity']);
116119

117120
$detail = new UserDetail();
118-
$detail->setUser($user)->setFirstName($data['detail']['firstName'])->setLastName($data['detail']['lastName']);
121+
$detail->setUser($user);
122+
123+
if (!empty($data['detail']['firstName'])) {
124+
$detail->setFirstName($data['detail']['firstName']);
125+
}
126+
127+
if (!empty($data['detail']['lastName'])) {
128+
$detail->setLastName($data['detail']['lastName']);
129+
}
119130

120131
if (!empty($data['detail']['email']) && !$this->emailExists($data['detail']['email'])) {
121132
$detail->setEmail($data['detail']['email']);

0 commit comments

Comments
 (0)