-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleType.php
More file actions
72 lines (65 loc) · 2.5 KB
/
ArticleType.php
File metadata and controls
72 lines (65 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace App\Form;
use App\Entity\Article;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Validator\Constraints as Assert;
class ArticleType extends AbstractType
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'required' => true,
'label' => $this->translator->trans('name', [], 'article'),
'empty_data' => ''
])
->add('description', TextareaType::class, [
'required' => true,
'label' => $this->translator->trans('description', [], 'article'),
'attr' => ['rows' => 5, 'cols' => 40],
'empty_data' => ''
])
->add('priceHT', MoneyType::class, [
'required' => true,
'label' => $this->translator->trans('price_HT', [], 'article'),
'html5' => true,
'scale' => 2,
'attr' => [
'step' => '0.01', // Permet d'utiliser des centimes dans le champ
],
'empty_data' => ''
])
->add('image', FileType::class, [
'required' => (!$builder->getData()->getId()) ? true : false,
'label' => $this->translator->trans('image', [], 'article'),
'constraints' => [
new Assert\Image([
'maxSize' => '2M',
'mimeTypes' => ['image/jpeg', 'image/png', 'image/gif'],
'mimeTypesMessage' => $this->translator->trans('article.image.format', [], 'validation'),
]),
],
'empty_data' => '',
'mapped' => false
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Article::class,
]);
}
}