1
votes

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');
}
3
Edited: * @ORM\Column(name="product_condition", type="string", length=10) - piotr712
Last edit don't solve main problem. - piotr712

3 Answers

1
votes

try this:

//..

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

..//

1
votes

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.

1
votes

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');