0

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Rajesh
  • 30
  • 7
  • 1
    Your query is not secure. Please post your Joomla questions at [joomla.se] Stack Exchange. – mickmackusa Aug 18 '19 at 02:15
  • 1
    Would it not be simpler to write: `return array_merge(parent::getOptions(), $db->setQuery($query)->loadColumn());` ? Also, you are working with just one table, so there is no need to declare the table alias `a`. Let's see the form html to see if you have a `name="country"` field. You know that `$country` _is not working_ because you echo'ed it from inside `getoptions()`? – mickmackusa Aug 18 '19 at 02:25
  • If you write `JFactory::getApplication()->enqueueMessage($this->form->getValue('country'), 'info');` inside of the `getOptions()` method, what do you get? – mickmackusa Aug 18 '19 at 02:46

0 Answers0