Symfony - 添加另一个DateType :: class,array('widget'=>'single_text')时出错

时间:2017-09-27 08:54:55

标签: php symfony fosuserbundle

请参阅我的User.php中有3个datetime类型实体

<?php

namespace UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
     */
    protected $firstName;

    /**
     * @ORM\Column(name="middle_name", type="string", length=255, nullable=true)
     */
    protected $middleName;

    /**
     * @ORM\Column(name="last_name", type="string", length=255, nullable=true)
     */
    protected $lastName;

    /**
     * @ORM\Column(name="position", type="string", length=255, nullable=true)
     */
    protected $position;

    /**
     * @ORM\Column(name="gender", type="string", length=255, nullable=true)
     */
    protected $gender;

    /**
     * @ORM\Column(name="birth_date", type="datetime", nullable=true)
     */
    protected $birthDate;

    /**
     * @ORM\Column(name="address", type="string", length=255, nullable=true)
     */
    protected $address;

    /**
     * @ORM\Column(name="school", type="string", length=255, nullable=true)
     */
    protected $school;

    /**
     * @ORM\Column(name="hired_date", type="datetime", nullable=true)
     */
    protected $hiredDate;

    /**
     * @ORM\Column(name="end_date", type="datetime", nullable=true)
     */
    protected $endDate;

    // Change the targetEntity path if you want to create the group

    /**
     * @ORM\ManyToMany(targetEntity="UserBundle\Entity\Group")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * @return String
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * @return String
     */
    public function getMiddleName()
    {
        return $this->middleName;
    }

    /**
     * @return String
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * @return String
     */
    public function getPosition()
    {
        return $this->position;
    }

    /**
     * @return String
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * @return String
     */
    public function getBirthDate()
    {
        return $this->birthDate;
    }

    /**
     * @return String
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @return String
     */
    public function getSchool()
    {
        return $this->school;
    }

    /**
     * @return String
     */
    public function getHiredDate()
    {
        return $this->hiredDate;
    }

    /**
     * @return String
     */
    public function getEndDate()
    {
        return $this->endDate;
    }

    /**
     * @param String $firstName
     * @return User
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * @param String $middleName
     * @return User
     */
    public function setMiddleName($middleName)
    {
        $this->middleName = $middleName;

        return $this;
    }

    /**
     * @param String $lastName
     * @return User
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * @param String $position
     * @return User
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * @param String $gender
     * @return User
     */
    public function setGender($gender)
    {
        $this->gender = $gender;

        return $this;
    }

    /**
     * @param String $birthDate
     * @return User
     */
    public function setBirthDate($birthDate)
    {
        $this->birthDate = $birthDate;

        return $this;
    }

    /**
     * @param String $address
     * @return User
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * @param String $school
     * @return User
     */
    public function setSchool($school)
    {
        $this->school = $school;

        return $this;
    }

    /**
     * @param String $hiredDate
     * @return User
     */
    public function setHiredDate($hiredDate)
    {
        $this->hiredDate = $hiredDate;

        return $this;
    }

    /**
     * @param String $endDate
     * @return User
     */
    public function setEndDate($endDate)
    {
        $this->endDate = $endDate;

        return $this;
    }
}

问题是当我尝试在我的ProfileFormType.php中添加这些日期时,只有birthDate被添加为小部件,但其他两个日期(hiredDate&amp; endDate)抛出这个错误。

Unable to transform value for property path "hiredDate": datefmt_format: invalid PHP type for date: U_ILLEGAL_ARGUMENT_ERROR

与生日后的endDate相同

Unable to transform value for property path "endDate": datefmt_format: invalid PHP type for date: U_ILLEGAL_ARGUMENT_ERROR 这是我的ProfileFormType.php

<?php

namespace UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType;

class ProfileFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', 'text', array('label' => 'form.firstName', 'translation_domain' => 'FOSUserBundle'))
            ->add('middleName', 'text', array('label' => 'form.middleName', 'translation_domain' => 'FOSUserBundle'))
            ->add('lastName', 'text', array('label' => 'form.lastName', 'translation_domain' => 'FOSUserBundle'))
            ->add('position', 'text', array('label' => 'form.position', 'translation_domain' => 'FOSUserBundle'))
            ->add('gender', 'text', array('label' => 'form.gender', 'translation_domain' => 'FOSUserBundle'))
            ->add('address', 'text', array('label' => 'form.address', 'translation_domain' => 'FOSUserBundle'))
            ->add('school', 'text', array('label' => 'form.school', 'translation_domain' => 'FOSUserBundle'))
            ->add('birthDate', DateType::class, array(
                'widget' => 'single_text',
                'label' => 'form.birthDate',
                'translation_domain' => 'FOSUserBundle'
                ))
            ->add('hiredDate', DateType::class, array(
                'widget' => 'single_text',
                'label' => 'form.hiredDate',
                'translation_domain' => 'FOSUserBundle'
                ))
            ->add('endDate', DateType::class, array(
                'widget' => 'single_text',
                'label' => 'form.endDate',
                'translation_domain' => 'FOSUserBundle'
                ))
        ;
    }

    public function getParent()
    {
        return 'fos_user_profile';
    }

    public function getName()
    {
        return 'user_profile';
    }

}

请帮我解决我的代码有什么问题?

0 个答案:

没有答案