Reputation: 423
Hi everyone i have problem. I need to take data from Select field filled by entity class.
This is my entity: It's dictionary with months and first and last day.
Entity
namespace accountant\ReportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Calendar
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="month", type="string", length=255)
*/
private $month;
}
I load fixtures to this table to db and create formBuilder:
FormType
class ReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'property' => 'month',
'expanded' => false,
'multiple' => false
));
}
}
Now I want to get data from selected month to, but $data
is null
:
Controller
/**
* @Route("/report", name="report")
* @Template()
*/
public function indexAction(Request $request)
{
$form = $this->createForm(new ReportFormType());
$form->handleRequest($request);
$data = $form->getData();
var_dump($data); // $data is null!
return array('form' => $form->createView());
}
TWIG
<form method="get" action="{{ path('report') }}">
<div class="form-group">
{{ form_row(form.month, {'label': 'Select Month:', 'attr': {'class': 'form-control'}}) }}
</div>
<div class="form-group">
<input type="submit" value="Show Report" class="btn btn-warning btn-block"/>
</div>
</form>
UPDATE 1
I add 'data_class' but i still get NULL.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'choice_label' => 'month',
'expanded' => false,
'multiple' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Calendar::class
));
}
This is my Controller now.
public function indexAction(Request $request)
{
$form = $this->createForm(new ReportFormType());
$form->handleRequest($request);
$data = $form->get('month')->getData();
// i check data
var_dump($data);
return array(
'form' => $form->createView());
}
UPDATE 2
I solved my problem i change my form in template for this:
<form method="post" action="{{ path('report') }}" novalidate="novalidate">
{{ form_errors(form) }}
<div class="form-group">
{{ form_row(form.month, {'label': 'Select month', 'attr': {'class': 'form-control'}}) }}
{{ form_rest(form) }}
</div>
<div class="form-group">
<input type="submit" value="Show report" class="btn btn-warning btn-block"/>
</div>
I change method to post
and add form_rest
Upvotes: 1
Views: 4003
Reputation: 17964
change your form like :
use accountant\ReportBundle\Entity\Calendar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'property' => 'month',
'expanded' => false,
'multiple' => false
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class'=> Calendar::class,
));
}
}
Keep in mind to have first letter of namespace sections in CAPS (Accountant
instead of accountant
).
According to OP's comment, you may get month
data in your controller:
Symfony 2
public function indexAction(Request $request)
{
// ...
$data = $form->get('month')->getData();
// ...
}
Symfony 3 & 4
public function indexAction(Request $request)
{
// ...
$data = $form['month']->getData();
// ...
}
Upvotes: 1