Issue
I try add fields with form events in symfony but fields is add automatically when refresh page ... scenario is when I fill input email fields body is display ...
code PostType:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class PostType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('titl')->add('email')->add('phone');
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event){
$post = $event->getData();
$form = $event->getForm();
if(!empty($post->getEmail))
{
$form->add('body');
}
});
$builder->add('save', SubmitType::class);
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Post'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_post';
}
}
Solution
as it says in comment you have to use Javascript to do this, for example make an ajax request to submit the form and to apply, always in javascript, the HTML response.
There is a great video tutorial for this (in french) https://www.youtube.com/watch?v=F0Z-D3MSjA0&t=1504s
Answered By - qdequippe
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.