I added an eventlistener in my FORMTYPE to adapte the form regarding to the user. The form is indeed adapted and displayed like I intended, but when I submit the form I got the following error:
FatalErrorException: Error: Call to a member function getAuthor() on array in /Applications/MAMP/htdocs/Symfony/src/Site/BlogBundle/Form/ArticleType.php line 47
I use indeed the getAuthor() fonction in my ArticleType to make sure the current user is the author of the article displayed by the form.
Here is my ArticleTYPE
class Article extends AbstractType
{
// I PASS VARIABLE $userId FROM CONTROLLER THROUGH createForm()
private $userId;
public function __construct($userId)
{
$this->userId = $userId;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->remove('ArticlePhrases')
->remove('articletags');
$dynamicFieldCreatorFunc = function (FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
// ADAPT THE FORM REGARDING CURRENT USER
if($this->userId == $data->getAuthor()->getId()) // LINE 47
{
$form->add('statut')
}
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, $dynamicFieldCreatorFunc);
$builder->addEventListener(FormEvents::PRE_SUBMIT, $dynamicFieldCreatorFunc);
}
(...)
}
here is my controller (absract)
$userId= $this->getUser()->getId();
$form_article = $this->createForm(new ArticleType($userId), $articles);
Aucun commentaire:
Enregistrer un commentaire