2012-07-23 57 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(), 
    ... 
))