简体   繁体   中英

Simple date with Sonata Admin Bundle

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 !!

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',

          ))

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'));

Edit

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!

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM