POST后如何从扩展表单类型中捕获/获取字段

时间:2014-12-08 16:38:22

标签: php symfony

我有(FormType2)作为FormType1的扩展表单类型。出现FormType2的新字段,也是FormType1的新字段,意味着我的Extends Type工作正常...但是当我发布表单时。无法检索FormType2的字段...我收到此错误消息:

属性“nychaCode”和方法“getNychaCode()”,“isNychaCode()”,“hasNychaCode()”,“__ get()”都不存在,并且在类“Bundle \ Model \ DiscountCode”中具有公共访问权限”

我如何解决这个问题?感谢。

我的AbstractTypeExtension

namespace MyBundle\Form\Type;

use MyBundle\Model\DiscountCodeNYCHA;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

class DiscountCodeNYCHAType extends AbstractTypeExtension{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(DiscountCodeNYCHA::ATTRIBUTE_NYCHA_CODE, null, array('label' => 'NYCHA Tenant Account Number', 'attr' => array('autocomplete' => 'off'));
        $builder->add(DiscountCodeNYCHA::ATTRIBUTE_BIRTH_YEAR, null, array('label' => 'Birth Year', 'attr' => array('autocomplete' => 'off'));
        $builder->add(DiscountCodeNYCHA::ATTRIBUTE_BIRTH_DAY, null, array('label' => 'Birth Day', 'attr' => array('autocomplete' => 'off'));
    }

    public function getExtendedType()
    {
        return 'edDiscountCode';
    }
}

Model DiscountCodeNYCHA

namespace MyBundle\Model;


use Bundle\Model\DiscountCode;

class DiscountCodeNYCHA extends DiscountCode {

    const ATTRIBUTE_NYCHA_CODE = 'nychaCode';
    const ATTRIBUTE_BIRTH_YEAR = 'birthYear';
    const ATTRIBUTE_BIRTH_DAY = 'birthDay';

    private $nychaCode;
    private $birthYear;
    private $birthDay;

    /**
     * @return mixed
     */
    public function getNychaCode()
    {
        return $this->nychaCode;
    }

    /**
     * @param mixed $nychaCode
     */
    public function setNychaCode($nychaCode)
    {
        $this->nychaCode = $nychaCode;
    }

    /**
     * @return mixed
     */
    public function getBirthDay()
    {
        return $this->birthDay;
    }

    /**
     * @param mixed $birthDay
     */
    public function setBirthDay($birthDay)
    {
        $this->birthDay = $birthDay;
    }

    /**
     * @return mixed
     */
    public function getBirthYear()
    {
        return $this->birthYear;
    }

    /**
     * @param mixed $birthYear
     */
    public function setBirthYear($birthYear)
    {
        $this->birthYear = $birthYear;
    }

    public function isEmpty(){
        if(is_null($this->nychaCode) && is_null($this->birthYear) && is_null($this->birthDay)){
            return true;
        }
    }
}

我的FormType

use Bundle\Model\DiscountCode;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DiscountCodeType extends AbstractType {

    function __construct() {
    }


    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add(DiscountCode::ATTRIBUTE_FIRST_TWO_LETTERS_FIRST_NAME, null, array('label'=>'First two letters of first name','attr' => array('autocomplete' => 'off')));
        $builder->add(DiscountCode::ATTRIBUTE_FIRST_TWO_LETTERS_LAST_NAME, null, array('label'=>'First two letters of last name','attr' => array('autocomplete' => 'off')));
        $builder->add(DiscountCode::ATTRIBUTE_LAST_FOUR_DIGITS_SSN, null, array('label'=>'Last four digits of Social Security Number','attr' => array('autocomplete' => 'off')));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        parent::setDefaultOptions($resolver);

        $resolver->setDefaults([
            'data_class' => get_class(new DiscountCode()),
            'translation_domain' => 'DiscountCodeType',
        ]);
    }

    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    public function getName() {
        return 'edDiscountCode';
    }
}

Model DiscountCode:

namespace Bundle\Model;

class DiscountCode {
    const ATTRIBUTE_FIRST_TWO_LETTERS_FIRST_NAME = 'firstTwoLettersFirstName'; // First two letters of first name
    const ATTRIBUTE_FIRST_TWO_LETTERS_LAST_NAME = 'firstTwoLettersLastName'; // First two letters of last name
    const ATTRIBUTE_LAST_FOUR_DIGITS_SSN = 'lastFourDigitsSsn'; // Last four digits of Social Security Number

    private $firstTwoLettersFirstName;
    private $firstTwoLettersLastName;
    private $lastFourDigitsSsn;

    /**
     * @return mixed
     */
    public function getFirstTwoLettersFirstName()
    {
        return $this->firstTwoLettersFirstName;
    }

    /**
     * @param mixed $firstTwoLettersFirstName
     */
    public function setFirstTwoLettersFirstName($firstTwoLettersFirstName)
    {
        $this->firstTwoLettersFirstName = $firstTwoLettersFirstName;
    }

    /**
     * @return mixed
     */
    public function getFirstTwoLettersLastName()
    {
        return $this->firstTwoLettersLastName;
    }

    /**
     * @param mixed $firstTwoLettersLastName
     */
    public function setFirstTwoLettersLastName($firstTwoLettersLastName)
    {
        $this->firstTwoLettersLastName = $firstTwoLettersLastName;
    }

    /**
     * @return mixed
     */
    public function getLastFourDigitsSsn()
    {
        return $this->lastFourDigitsSsn;
    }

    /**
     * @param mixed $lastFourDigitsSsn
     */
    public function setLastFourDigitsSsn($lastFourDigitsSsn)
    {
        $this->lastFourDigitsSsn = $lastFourDigitsSsn;
    }

    public function isEmpty(){
        if(is_null($this->firstTwoLettersFirstName) && is_null($this->firstTwoLettersLastName) && is_null($this->lastFourDigitsSsn)){
            return true;
        }
    }
}

更新:我只是将DiscountCodeNYCHA的setter和getter更新为公共访问,受到保护。

1 个答案:

答案 0 :(得分:1)

问题是您没有告诉扩展表单类型使用扩展模型类。由于您未覆盖 setDefaultOptions ,因此它使用您在DiscountCodeType中的定义,其中 data_class 设置为DiscountType。

将此添加到 DiscountCodeNYCHAType:

public function setDefaultOptions(OptionsResolverInterface $resolver) {

    $resolver->setDefaults([
        'data_class' => get_class(new DiscountCodeNYCHA()),
        'translation_domain' => 'DiscountCodeType',
    ]);
}

此外,在 DiscountCodeType 中,您无需调用

parent::setDefaultOptions($resolver);

因为您正在扩展 Symfony \ Component \ Form \ AbstractType ,其中每个函数都有一个空的定义。