Example #1
0
 public function configure()
 {
     unset($this['city_name']);
     //Narrow down the valid options for some field validators
     $regionsQuery = null;
     if ($this->getOption('country_id')) {
         $regionsQuery = RegionTable::getInstance()->getRegionsByCountryIdQuery($this->getOption('country_id'));
     }
     $this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 'add_empty' => false, 'query' => $regionsQuery));
     if ($this->isNew()) {
         $this->widgetSchema['region_id']->setAttribute('disabled', 'disabled');
     }
     $this->embedRelation('Region');
 }
Example #2
0
 /**
  * Run action from JQuery POST while chosing the region in the select HTML field
  * for offices creation and edition.
  *
  * @param sfWebRequest with the chosen region
  */
 public function executeChosenregion(sfWebRequest $request)
 {
     $regionId = $request->getParameter('regionId');
     //set content type HTTP field  with the right value (we are going to use a JSON response)
     $this->getResponse()->setContentType('application/json');
     //Never trust data coming from user
     if (!isset($regionId)) {
         //Incorrect data from user.
         //TODO: JSON error
         return $this->renderText(json_encode(""));
     } else {
         $region = RegionTable::getInstance()->findOneById($regionId);
         if (!isset($region)) {
             //Incorrect data from user.
             //TODO: JSON error
             return $this->renderText(json_encode(""));
         }
     }
     $citiesJSON = array();
     //Retrieve Doctrine_Collection
     $cities = CityTable::getInstance()->findByRegionId($region->getId());
     //Using Doctrine_Collection_Iterator
     $iterator = $cities->getIterator();
     while ($city = $iterator->current()) {
         $citiesJSON[$city->getId()] = $city->getCityName();
         $iterator->next();
     }
     //Bypass completely the view layer and set the response code directly from this action.
     //In this way the user may know if the data were updated
     return $this->renderText(json_encode($citiesJSON));
 }