Skip to content

Commit f4c3617

Browse files
committed
validator is working
1 parent 1b85360 commit f4c3617

File tree

8 files changed

+78
-16
lines changed

8 files changed

+78
-16
lines changed

assets/js/components/tables/tag/TagTable.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const TagTable = () => {
3737
if (!context.tags) context.read();
3838
}, [context]);
3939

40-
console.log(message);
41-
4240
return (
4341
<TableContainer component={Paper}>
4442
<Table size="small">
@@ -49,7 +47,7 @@ const TagTable = () => {
4947
</TableCell>
5048
</TableRow>
5149
<TableRow>
52-
<TableCell>{message.level === 'error' ? message.text.join('\n') : 'Tag Name'}</TableCell>
50+
<TableCell>Tag Name</TableCell>
5351
<TableCell align="right">Actions</TableCell>
5452
</TableRow>
5553
</TableHead>

assets/js/contexts/TagContext.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TagContextProvider extends React.Component {
1010
tags: null,
1111
isLoading: false,
1212
message: {
13-
text: '',
13+
text: null,
1414
level: null,
1515
},
1616

@@ -29,11 +29,11 @@ class TagContextProvider extends React.Component {
2929
this.setState({isLoading: true});
3030
try {
3131
const r = await axios.post('/api/tag/create', data);
32-
const {createdTag, message} = r.data;
32+
const createdTag = r.data.tag, message = r.data.message;
3333

34-
if (message.level === 'error') {
34+
if (message !== undefined && message.level === 'error') {
3535
this.setState({
36-
message: message.text,
36+
message: message,
3737
isLoading: false,
3838
});
3939
} else {
@@ -42,8 +42,8 @@ class TagContextProvider extends React.Component {
4242
isLoading: false,
4343
});
4444
}
45-
4645
} catch (e) {
46+
console.error(e);
4747
this.setState({
4848
errors: e,
4949
isLoading: false,
@@ -82,16 +82,21 @@ class TagContextProvider extends React.Component {
8282
*/
8383
async delete(data) {
8484
try {
85+
if (this.state.isLoading) return;
86+
87+
this.setState({isLoading: true});
8588
const r = await axios.delete('/api/tag/delete/' + data.id);
8689
const newTags = [...this.state.tags].filter(tag => tag.id !== data.id);
8790

8891
this.setState({
89-
tags: newTags,
92+
tags: newTags,
93+
isLoading: false,
9094
});
9195

9296
} catch (e) {
9397
this.setState({
94-
error: e,
98+
error: e,
99+
isLoading: false,
95100
});
96101
}
97102
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# As of Symfony 5.1, deprecations are logged in the dedicated "deprecation" channel when it exists
2+
#monolog:
3+
# channels: [deprecation]
4+
# handlers:
5+
# deprecation:
6+
# type: stream
7+
# channels: [deprecation]
8+
# path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"

src/Controller/TagController.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace App\Controller;
44

5-
use App\Controller\Services\Message;
65
use App\Entity\Tag;
76
use App\Repository\TagRepository;
87
use Doctrine\ORM\EntityManagerInterface;
98
use Exception;
9+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1111
use Symfony\Component\HttpFoundation\JsonResponse;
1212
use Symfony\Component\HttpFoundation\Request;
@@ -66,11 +66,17 @@ public function create(Request $request)
6666
$tag = new Tag();
6767
$tag->setName($content->name);
6868

69-
$tagNameError = $this->validator->validateProperty($tag, 'name');
69+
$tagNameErrors = $this->validator->validate($tag);
7070

7171
$errors = [];
72-
if (sizeof($tagNameError) > 0) {
73-
$errors[] = $tagNameError[0]->getMessage();
72+
if (sizeof($tagNameErrors) > 0) {
73+
74+
// enable this to generate an array of all errors instead of just one
75+
// foreach ($tagNameErrors as $tagNameError) {
76+
// $errors[] = $tagNameError->getMessage();
77+
// }
78+
79+
$errors[] = $tagNameErrors[0]->getMessage();
7480

7581
return $this->json(
7682
['message' => ['text' => $errors, 'level' => 'error']]

src/Entity/Tag.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use App\Repository\TagRepository;
66
use Doctrine\ORM\Mapping as ORM;
7+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
78
use Symfony\Component\Serializer\Encoder\JsonEncoder;
89
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
910
use Symfony\Component\Serializer\Serializer;
1011
use Symfony\Component\Validator\Constraints as Assert;
1112

1213
/**
1314
* @ORM\Entity(repositoryClass=TagRepository::class)
15+
* @UniqueEntity(message="The tag name must be unique.", fields={"name"})
1416
*/
1517
class Tag
1618
{
@@ -22,7 +24,7 @@ class Tag
2224
private $id;
2325

2426
/**
25-
* @ORM\Column(type="string", length=255, nullable=false)
27+
* @ORM\Column(type="string", name="name", length=255, nullable=false, unique=true)
2628
* @Assert\NotBlank(message="The tag name cannot be blank.")
2729
* @Assert\Length(
2830
* min = 2,
@@ -60,4 +62,5 @@ public function normalize()
6062

6163
return $serializer->normalize($this, 'json');
6264
}
65+
6366
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Auto-generated Migration: Please modify to your needs!
1212
*/
13-
final class Version20200722212009 extends AbstractMigration
13+
final class Version20200723224647 extends AbstractMigration
1414
{
1515
public function getDescription() : string
1616
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20200723225030 extends AbstractMigration
14+
{
15+
public function getDescription() : string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema) : void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('CREATE UNIQUE INDEX UNIQ_389B7835E237E06 ON tag (name)');
24+
}
25+
26+
public function down(Schema $schema) : void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('DROP INDEX UNIQ_389B7835E237E06 ON tag');
30+
}
31+
}

tests/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Dotenv\Dotenv;
4+
5+
require dirname(__DIR__).'/vendor/autoload.php';
6+
7+
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
8+
require dirname(__DIR__).'/config/bootstrap.php';
9+
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
10+
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
11+
}

0 commit comments

Comments
 (0)