Display field value for an EntityType of an EntityType

In the twig template for creating a new address (AddressType form), I want to display (not edit) in an disabled selectmenu the Country for the selected Postcode.

Is there a way to access the country name field value for the selected postcode within the AddressType?

Or is this a case of triggering getCountry via javascript upon selecting a postcode?

Address

/**
 * @ORMColumn(type="string", length=255)
 */
private $line1;
/**
 * @ORMManyToOne(targetEntity="AppEntitySettingPostcode", inversedBy="addresses")
 * @ORMJoinColumn(nullable=false)
 */
private $postcode;

Postcode

/**
 * @ORMColumn(type="string", length=255)
 */
private $value;
/**
 * @ORMManyToOne(targetEntity="AppEntitySettingCountry", inversedBy="postcodes")
 * @ORMJoinColumn(nullable=false)
 */
private $country;
/**
 * @ORMOneToMany(targetEntity="AppEntityAddress", mappedBy="postcode", orphanRemoval=true)
 */
private $addresses;

Country

/**
 * @ORMColumn(type="string", length=255)
 */
private $name;
/**
 * @ORMColumn(type="string", length=2)
 */
private $areaNo;
/**
 * @ORMOneToMany(targetEntity="AppEntitySettingPostcode", mappedBy="county", orphanRemoval=true)
 */
private $postcodes;

AddressType

$builder
    ->add('line1', null, ['label' => 'Address Line 1'])
    ->add('postcode', EntityType::class, [
        'class'        => Postcode::class,
        'choice_label' => 'postcode',
        'required'   => true,
        'placeholder' => '',
    ]);

PostcodeType

$builder
    ->add('value', null, ['label' => 'Postcode'])
    ->add('country', EntityType::class, [
        'class'        => Country::class,
        'choice_label' => 'name',
        'required'   => true,
        'placeholder' => '',
    ]);

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *