custom form field 'province/state' for dynamic dropdown in Joomla

435 views Asked by At

I am trying to create a custom form field naming as city. On select of country field, city field should populate options of all the provinces listed under the selected country.

Just exactly as the Joomla documentation.

I am following https://docs.joomla.org/Creating_a_custom_form_field_type documentation.

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldCity extends JFormFieldList {

    protected $type = 'City';

    public function getOptions() {
        $app = JFactory::getApplication();

        //country is the dynamic value which is being used in the view
        $country = $app->input->get('country');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('a.cityname')
              ->from('`#__tablename` AS a')
              ->where('a.country = "'.$country.'" ');
        $rows = $db->setQuery($query)->loadObjectlist();
        foreach($rows as $row){
            $cities[] = $row->cityname;
        }
        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $cities);
        return $options;
    }
}

However, I can see $country = $app->input->get('country'); is not working.

At view, I have a country field.

0

There are 0 answers