@@ -1771,4 +1771,108 @@ public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
17711771 $ this ->assertSame ('Foo ' , $ view ['entity_two ' ]->vars ['choices ' ]['Foo ' ]->value );
17721772 $ this ->assertSame ('Bar ' , $ view ['entity_two ' ]->vars ['choices ' ]['Bar ' ]->value );
17731773 }
1774+
1775+ public function testEmptyChoicesWhenLazy ()
1776+ {
1777+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
1778+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
1779+ $ this ->persist ([$ entity1 , $ entity2 ]);
1780+
1781+ $ view = $ this ->factory ->create (FormTypeTest::TESTED_TYPE )
1782+ ->add ('entity_one ' , self ::TESTED_TYPE , [
1783+ 'em ' => 'default ' ,
1784+ 'class ' => self ::SINGLE_IDENT_CLASS ,
1785+ 'extra_lazy ' => true ,
1786+ ])
1787+ ->createView ()
1788+ ;
1789+
1790+ $ this ->assertCount (0 , $ view ['entity_one ' ]->vars ['choices ' ]);
1791+ }
1792+
1793+ public function testLoadedChoicesWhenLazyAndBoundData ()
1794+ {
1795+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
1796+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
1797+ $ this ->persist ([$ entity1 , $ entity2 ]);
1798+
1799+ $ view = $ this ->factory ->create (FormTypeTest::TESTED_TYPE , ['entity_one ' => $ entity1 ])
1800+ ->add ('entity_one ' , self ::TESTED_TYPE , [
1801+ 'em ' => 'default ' ,
1802+ 'class ' => self ::SINGLE_IDENT_CLASS ,
1803+ 'extra_lazy ' => true ,
1804+ ])
1805+ ->createView ()
1806+ ;
1807+
1808+ $ this ->assertCount (1 , $ view ['entity_one ' ]->vars ['choices ' ]);
1809+ $ this ->assertSame ('1 ' , $ view ['entity_one ' ]->vars ['choices ' ][1 ]->value );
1810+ }
1811+
1812+ public function testLoadedChoicesWhenLazyAndSubmittedData ()
1813+ {
1814+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
1815+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
1816+ $ this ->persist ([$ entity1 , $ entity2 ]);
1817+
1818+ $ view = $ this ->factory ->create (FormTypeTest::TESTED_TYPE )
1819+ ->add ('entity_one ' , self ::TESTED_TYPE , [
1820+ 'em ' => 'default ' ,
1821+ 'class ' => self ::SINGLE_IDENT_CLASS ,
1822+ 'extra_lazy ' => true ,
1823+ ])
1824+ ->submit (['entity_one ' => '2 ' ])
1825+ ->createView ()
1826+ ;
1827+
1828+ $ this ->assertCount (1 , $ view ['entity_one ' ]->vars ['choices ' ]);
1829+ $ this ->assertSame ('2 ' , $ view ['entity_one ' ]->vars ['choices ' ][2 ]->value );
1830+ }
1831+
1832+ public function testEmptyChoicesWhenLazyAndEmptyDataIsSubmitted ()
1833+ {
1834+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
1835+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
1836+ $ this ->persist ([$ entity1 , $ entity2 ]);
1837+
1838+ $ view = $ this ->factory ->create (FormTypeTest::TESTED_TYPE , ['entity_one ' => $ entity1 ])
1839+ ->add ('entity_one ' , self ::TESTED_TYPE , [
1840+ 'em ' => 'default ' ,
1841+ 'class ' => self ::SINGLE_IDENT_CLASS ,
1842+ 'extra_lazy ' => true ,
1843+ ])
1844+ ->submit ([])
1845+ ->createView ()
1846+ ;
1847+
1848+ $ this ->assertCount (0 , $ view ['entity_one ' ]->vars ['choices ' ]);
1849+ }
1850+
1851+ public function testErrorOnSubmitInvalidValuesWhenLazyAndCustomQueryBuilder ()
1852+ {
1853+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
1854+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
1855+ $ this ->persist ([$ entity1 , $ entity2 ]);
1856+ $ qb = $ this ->em
1857+ ->createQueryBuilder ()
1858+ ->select ('e ' )
1859+ ->from (self ::SINGLE_IDENT_CLASS , 'e ' )
1860+ ->where ('e.id = 2 ' )
1861+ ;
1862+
1863+ $ form = $ this ->factory ->create (FormTypeTest::TESTED_TYPE , ['entity_one ' => $ entity2 ])
1864+ ->add ('entity_one ' , self ::TESTED_TYPE , [
1865+ 'em ' => 'default ' ,
1866+ 'class ' => self ::SINGLE_IDENT_CLASS ,
1867+ 'query_builder ' => $ qb ,
1868+ 'extra_lazy ' => true ,
1869+ ])
1870+ ->submit (['entity_one ' => '1 ' ])
1871+ ;
1872+ $ view = $ form ->createView ();
1873+
1874+ $ this ->assertCount (0 , $ view ['entity_one ' ]->vars ['choices ' ]);
1875+ $ this ->assertCount (1 , $ errors = $ form ->getErrors (true ));
1876+ $ this ->assertSame ('The selected choice is invalid. ' , $ errors ->current ()->getMessage ());
1877+ }
17741878}
0 commit comments