public function configure() { $this->useFields(array('city_id', 'office_street_address', 'office_zip')); //Narrow down the valid options for some field validators $citiesQuery = null; if ($this->getOption('region_id')) { $citiesQuery = CityTable::getInstance()->getCitiesByRegionIdQuery($this->getOption('region_id')); } $this->widgetSchema['longitude'] = new sfWidgetFormInputFloat(); $this->widgetSchema['latitude'] = new sfWidgetFormInputFloat(); $this->widgetSchema['city_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('City'), 'add_empty' => false, 'query' => $citiesQuery)); $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('City'), 'required' => true)); if ($this->isNew()) { $this->widgetSchema['city_id']->setAttribute('disabled', 'disabled'); } $this->validatorSchema['longitude'] = new sfValidatorNumber(array('max' => 180, 'min' => -180, 'required' => true, 'trim' => true), array('invalid' => 'Wrong Longitude', 'required' => 'The longitude field is required', 'max' => 'Longitude "%value%" must not exceed the %max% value', 'min' => 'Longitude "%value%" must be equal or higher than %min%')); $this->validatorSchema['latitude'] = new sfValidatorNumber(array('max' => 90, 'min' => -90, 'required' => true, 'trim' => true), array('invalid' => 'Wrong Latitude', 'required' => 'The latitude field is required', 'max' => 'Latitude "%value%" must not exceed the %max% value', 'min' => 'Latitude "%value%" must be equal or higher than %min%')); $this->widgetSchema->setLabels(array('city_id' => 'City: ', 'longitude' => 'Longitude (180 to -180): ', 'latitude' => 'Latitude (90 to -90): ', 'office_street_address' => 'Address: ', 'office_zip' => 'ZIP:')); $this->embedRelation('City', null, array('country_id' => $this->getOption('country_id'))); //In the future the companies could pay for improvements in their accounts (premium accounts) //1 premium company could avoid to other companies on the same GPS point. This validator should check if that GPS point is being used by //another company and if that company is premium or not. $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(new sfValidatorDoctrineUnique(array('model' => $this->getModelName(), 'column' => array('office_gps')))))); $this->validatorSchema->setOption('allow_extra_fields', false); $this->validatorSchema->setOption('filter_extra_fields', true); //i18n (Internationalization) //See apps/companyfront/modules/office/i18n/office_form.es.xml file $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('office_form'); }
/** * 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)); }