@@ -766,6 +766,55 @@ public function testOverrideChoices()
766
766
$ this ->assertSame ('2 ' , $ field ->getViewData ());
767
767
}
768
768
769
+ public function testOverrideChoicesValues ()
770
+ {
771
+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
772
+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
773
+
774
+ $ this ->persist (array ($ entity1 , $ entity2 ));
775
+
776
+ $ field = $ this ->factory ->createNamed ('name ' , 'Symfony\Bridge\Doctrine\Form\Type\EntityType ' , null , array (
777
+ 'em ' => 'default ' ,
778
+ 'class ' => self ::SINGLE_IDENT_CLASS ,
779
+ 'choice_label ' => 'name ' ,
780
+ 'choice_value ' => 'name ' ,
781
+ ));
782
+
783
+ $ field ->submit ('Bar ' );
784
+
785
+ $ this ->assertEquals (array ('Foo ' => new ChoiceView ($ entity1 , 'Foo ' , 'Foo ' ), 'Bar ' => new ChoiceView ($ entity2 , 'Bar ' , 'Bar ' )), $ field ->createView ()->vars ['choices ' ]);
786
+ $ this ->assertTrue ($ field ->isSynchronized (), 'Field should be synchronized. ' );
787
+ $ this ->assertSame ($ entity2 , $ field ->getData (), 'Entity should be loaded by custom value. ' );
788
+ $ this ->assertSame ('Bar ' , $ field ->getViewData ());
789
+ }
790
+
791
+ public function testOverrideChoicesValuesWithCallable ()
792
+ {
793
+ $ entity1 = new GroupableEntity (1 , 'Foo ' , 'BazGroup ' );
794
+ $ entity2 = new GroupableEntity (2 , 'Bar ' , 'BooGroup ' );
795
+
796
+ $ this ->persist (array ($ entity1 , $ entity2 ));
797
+
798
+ $ field = $ this ->factory ->createNamed ('name ' , 'Symfony\Bridge\Doctrine\Form\Type\EntityType ' , null , array (
799
+ 'em ' => 'default ' ,
800
+ 'class ' => self ::ITEM_GROUP_CLASS ,
801
+ 'choice_label ' => 'name ' ,
802
+ 'choice_value ' => function (GroupableEntity $ entity ) {
803
+ return $ entity ->groupName .'/ ' .$ entity ->name ;
804
+ },
805
+ ));
806
+
807
+ $ field ->submit ('BooGroup/Bar ' );
808
+
809
+ $ this ->assertEquals (array (
810
+ 'BazGroup/Foo ' => new ChoiceView ($ entity1 , 'BazGroup/Foo ' , 'Foo ' ),
811
+ 'BooGroup/Bar ' => new ChoiceView ($ entity2 , 'BooGroup/Bar ' , 'Bar ' ),
812
+ ), $ field ->createView ()->vars ['choices ' ]);
813
+ $ this ->assertTrue ($ field ->isSynchronized (), 'Field should be synchronized. ' );
814
+ $ this ->assertSame ($ entity2 , $ field ->getData (), 'Entity should be loaded by custom value. ' );
815
+ $ this ->assertSame ('BooGroup/Bar ' , $ field ->getViewData ());
816
+ }
817
+
769
818
public function testGroupByChoices ()
770
819
{
771
820
$ item1 = new GroupableEntity (1 , 'Foo ' , 'Group1 ' );
0 commit comments