Reputation: 81
I have a form with Sonata Admin Bundle with a date, to set the birthday of the user we want to add. Here goes MemberAdmin.php
:
/**
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
*
* @return void
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('username')
->add('name')
->add('surname')
->add('birthdate', 'birthday', array('format' => 'yyyy-MM-dd'))
// ...
}
And my problem is when I send the form, I obtain Error: Call to a member function format() on a non-object
... But if I do print_r($birthdate)
in the Entity class it shows me the DateTime
object ...
Here are the interesting Entity parts:
/**
* @var date
*
* @ORM\Column(name="birthdate", type="date", nullable=true, options={"default" = "1990-01-01 00:00:00"})
* @Assert\DateTime()
*/
private $birthdate;
/**
* Set birthdate
*
* @param \DateTime $birthdate
* @return Membre
*/
public function setBirthdate($birthdate)
{
$this->birthdate = $birthdate;
return $this;
}
/**
* Get birthdate
*
* @return \DateTime
*/
public function getBirthdate()
{
return $this->birthdate;
}
My problem, currently, is that I don't know what I should do, I just want the date, no time, no anything else, i don't know if the column should be date (I work with PostgreSQL). What should I use for the types of my variables, I feel lost here, no simple Date
possible ??
I tried to figure out from where it could come, but when I change too much I end up with: This form should not contain extra fields
directly in the form, or even Incorrect value
, but the field is a valid date ...
Thanks for your help !!
Upvotes: 0
Views: 1533
Reputation: 81
As @rande said, modifying vendors files is not the way to go, it provided an easy temp workaround for a local private app. As it is not dedicated to stay like that, I took care of the issue once I had more time. Sorry for the delay to come back to you guys.
I played around, tried with multiple setups, it took me time to figure it out, but I finally came to the conclusion that the issue... was caused by another date, that I was generating wrong in the constructor one line above.
Also, thanks to all of you, that guided me on the right path!
Upvotes: 0
Reputation: 666
@wr0ng.name you should never overwrite vendor code. NEVER.
There is something wrong with your mapping somewhere. You can use doctrine's commands to check your entity.
Upvotes: 0
Reputation: 630
Change your field type to sonata_type_date_picker and test if the error message persist.
->add('birthdate', 'sonata_type_date_picker', array(
'format' => 'dd/MM/yyyy',
'widget' => 'single_text',
'label' => 'Birthdate',
))
Upvotes: 2
Reputation: 1842
From manual (sonata-project.org) :
If no type is set, the Admin class will use the one set in the doctrine mapping definition.
So, you can try this:
->add('birthdate', null, array('format' => 'yyyy-MM-dd'));
Upvotes: 0