i have a form with a datetyoe field
$builder->add('dataSpesa',DateType::class,array(
'data' => new \DateTime("now"),
'label'=>false,
'widget'=>'single_text',
'attr' => array('style' => 'width: 100%;margin-top:10px;',
'class' => 'js-datepicker'
)
))
now i want to use this form in edit form and use use the save datetime .. i try in this way but it don't work and i'm sure is the best way
if($options['data']->getDataSpesa()){
$data_spesa=$options['data']['dataSpesa']->getData(); ->>> syntax error DON'T UNDERSTAND CORRECT WAY TO HAVE DATETIME VALUE
}else{
$data_spesa=new \DateTime("now");
}
->add('dataSpesa',DateType::class,array(
'label'=>false,
'data'=>$data_spesa, -> CORRECT WAY???
'widget'=>'single_text',
'attr' => array('style' => 'width: 100%;margin-top:10px;',
'class' => 'js-datepicker'
)
))
To better understand .. I use this form in 2 controller. In home page
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// crea nuovo
$voce_costo = new VoceCosto();
$form = $this->createForm(VoceCostoType::class, $voce_costo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isvalid()) {
return $this->render('default/add/vocecosto.html.twig', array(
'form' => $form->CreateView()
));
}
in this i want the date is set by default as current date
In the other controller (edit controller)
/**
* @Route("/edit/{id_vocecosto}", name="edit_voce")
*/
public function EditvoceCostoAction(Request $request,$id_vocecosto)
{
// crea nuovo
$voce_costo = $this->getDoctrine()
->getRepository('AppBundle:VoceCosto')
->find($id_vocecosto);
$form = $this->createForm(VoceCostoType::class, $voce_costo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isvalid()) {
}
return $this->render('default/edit/voce_costo.html.twig', array(
'form' => $form->CreateView()
));
}
here i want that value is the data saved in database