I have my in ProductsForm.php:
namespace Products\Form;
use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
class ProductsForm extends Form {
public function __construct(AdapterInterface $dbAdapter) {
$this->adapter = $dbAdapter;
parent::__construct('products');
$this->add(array(
'name' => 'state',
'type' => 'select',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'empty_option' => 'Select...',
'value_options' => $this->getStatesForSelect()
),
));
$this->add(array(
'name' => 'city',
'type' => 'select',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'empty_option' => 'Select...',
'value_options' => $this->getCitiesForSelect()
),
));
}
public function getStatesForSelect() {
$dbAdapter = $this->adapter;
$table = new TableGateway('states', $dbAdapter);
$result = $table->select(function (Select $select) {
$select->order('name DESC');
});
foreach ($result as $res) {
$selectData[$res['id']] = $res['name'];
}
return $selectData;
}
public function getCitiesForSelect($state_id) {
??? :(
select from cities where state_id = $state_id ?
}
}
I want to execute the getCitiesForSelect() only when the user select the "state"... and then fill it with values from database based on the state.id value
How can I do that?