2012-07-23 56 views
2

我更新的Symfony2到2.1,当我试图提交表单我得到错误:从表单类型级的选择约束需要一个有效的回调

The Choice constraint expects a valid callback

源代码:

$builder->add('type', 'choice', 
        array(
         'expanded' => true, 
         'multiple' => false, 
         'choice_list' => new TypeChoices(), 
         'required' => true, 
        ) 
       ) 

TypeChoices类:

class TypeChoices implements ChoiceListInterface { 

    public static $choices = array(
     'full-time' => 'Full time', 
     'part-time' => 'Part time', 
     'freelance' => 'Freelance', 
    ); 

    public static function getChoiceNameByValue($value) 
    { 
     return self::$choices[$value]; 
    } 

    public function getChoices() 
    { 
     return self::$choices; 
    } 

    public static function getTypeChoicesKeys() 
    { 
     return array_keys(self::$choices); 
    } 

    public static function getPreferredChoiceKey() 
    { 
     return 'full-time'; 
    } 
} 

有人能给我任何建议吗?

+0

选择列表的'实施'似乎已经改变了。你看过['upgrade-2.1.md'](https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md#other-bc-breaks)吗? – gilden 2012-07-23 14:13:53

回答

0

也许你可以尽量延长SimpleChoiceList类,这种方式:

选择列表代码:

class TypeChoices extends SimpleChoiceList 
{ 
    public static $choices = array(
     'full-time' => 'Full time', 
     'part-time' => 'Part time', 
     'freelance' => 'Freelance', 
    ); 

    /** 
    * Constructor. 
    * 
    * @param array $preferredChoices Preffered choices in the list. 
    */ 
    public function __construct(array $preferredChoices = array()) // PASS MORE ARGUMENT IF NEEDED 
    { 
     parent::__construct(
      static::$choices, 
      $preferredChoices 
     ); 
    } 
} 

表格类型代码:

->add('type', 'choice', array(
    'choice_list' => new TypeChoices(), 
    ... 
))