piotr712
piotr712

Reputation: 29

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

Upvotes: 1

Views: 941

Answers (3)

AlTak
AlTak

Reputation: 497

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

Upvotes: 1

Artem  Zhuravlev
Artem Zhuravlev

Reputation: 786

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.

Upvotes: 1

Emmanuel HD
Emmanuel HD

Reputation: 191

try this:

//..

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

..//

Upvotes: 1

Related Questions