symfony - How To get list of cities of selected country if country is not an entity using Dynamic Generation for Submitted Forms -
i have entity travel , dispaly list of cities of selected country in add form.
i have applied example in documentation : [http://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms][1]
in example , sport entity , has method called getavailablepositions() in example country not entity , countrytype::class.
if have in example in documentation must instantiate un entity country don't have one
$formmodifier = function (forminterface $form, country $country = null) { // country doesn't exist , wrong $cities = null === $country ? array() : $country->getcities(); $form->add('city', entitytype::class, array( 'class' => 'appbundle\entity\city', 'choices' => $cities, 'choice_label' => 'name', 'label' => 'city', 'multiple' => false, 'expanded' => false, 'required' => false, )); };
this entity city
class city { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="name", type="string", length=255) * */ private $name; /** * @var string * * @orm\column(name="country", type="string", length=5) */ private $country;
this citytype
//citytype $builder ->add('name', texttype::class, array( 'label' => 'name', )) ->add('country', countrytype::class, array( 'label' => 'country', 'placeholder' => 'country', )) ;
[1]:
as seen here countrype::class uses default
symfony\component\intl\intl::getregionbundle()->getcountrynames()
so if watch intl component here see can retrieve specific country
$country = intl::getregionbundle()->getcountryname('gb');
so, said : do not want create entity "country" own logic create static method "getcitiesbycountry" in entity city call repository retrieve cities of country (where country ''). above mentioned because if "country" string remember countrytype::class return 2 letters (this ones should persist in each city global, not full name), , use above methods according logic translate before render. let me know if disagree
Comments
Post a Comment