Voilà ma page formulaire :
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
| <form action="{{ path('news_ajouter') }}" method="post" {{ form_enctype(form) }}>
{{ form_label(form.auteur, 'Votre nom') }}
{{ form_widget(form.auteur) }}
{{ form_errors(form.auteur) }}
{{ form_label(form.titre, 'Titre') }}
{{ form_widget(form.titre) }}
{{ form_errors(form.titre) }}
{{ form_label(form.contenu, 'Contenu la news') }}
{{ form_widget(form.contenu) }}
{{ form_errors(form.contenu) }}
{{ form_label(form.image.url, 'Chemin vers l\'image') }}
{{ form_widget(form.image.url) }}
{{ form_errors(form.image.url) }}
{{ form_label(form.image.alt, 'Nom de votre image') }}
{{ form_widget(form.image.alt) }}
{{ form_errors(form.image.alt) }}
{{ form_label(form.image.file, 'Choisisssez votre image') }}
{{ form_widget(form.image.file) }}
{{ form_errors(form.image.file) }}
{# Le champs date est masquer #}
{{ form_widget(form.date) }}
<br/>
{{ form_rest(form) }}
<input type="submit" /> |
Si je supprime la ligne ->add('date' ...), il me dit :
Method "date" for object "Symfony\Component\Form\FormView" does not exist in FeriaNewsBundle:News:ajouter.html.twig at line 12
J'ai trouvé sur un forum qu'il fallait utiliser la propriété 'hidden' du genre :
1 2 3 4 5
| public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('date', 'hidden');
} |
Mais ca m'affiche cette erreur :
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class DateTime. You can avoid this error by setting the "data_class" option to "DateTime" or by adding a view transformer that transforms an instance of class DateTime to scalar, array or an instance of \ArrayAccess.
Comment je peux faire ?
J'ai une autre question. Actuellement j'affiche chaque champs de mon formulaire manuellement. Cependant si je ne met pas {{ form_rest(form) }} je ne peux pas valider la formulaire, il se passe rien.
Y a-t-il des champs cacher qui permettent la validation ? J'ai cru entendre parler de jeton ou de CSFR ou un truc comme ça. Mais les explications du net sont pas évidentes :/
Merci
Partager