简体   繁体   中英

Sonata Admin Bundle choice validate error

I am try edit or add product by Sonata Admin Bundle but validator always reject a "condition" field because "The value you selected is not a valid choice."

Admin class

protected function configureFormFields(FormMapper $formMapper)
{
        $formMapper
        ->add('name', 'text', array('label' => 'Nazwa'))
        ->add('condition', 'choice', array(
                'choices' => Product::getConditions(), 
                'label' => 'Stan',  
        ));
}

Entity

/**
 * @Assert\Choice(callback = "getConditions")
 * @ORM\Column(type="string", length=10)
 */
protected $condition;

public static function getConditions()
{
    return array('new', 'used');
}

try this:

//..

->add('condition', 'entity', array(
                'class' => YourAppBundle:YourEntityProduct
                'label' => 'Stan',
      ));

..//

Doctrine is expecting to get string but you pass integer to it as value of your selected field.
That's what you pass:

 return array(
    0 => 'new',
    1 => 'used'
);

That's what you need(for example):

 return array(
    '0' => 'new',
    '1' => 'used'
);

Error is triggered by length validation on field.

Sonata uses values as labels to display and Keys as values (passed to model). To get what you want your array should be like array('new' => 'new', 'used' => 'used');

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