/ Published in: PHP
i use this link : http://www.symfony-project.org/more-with-symfony/1_4/fr/05-Custom-Widgets-and-Validators
, but this one is lighest and don't require jquery
, but this one is lighest and don't require jquery
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class sfWidgetFormGeocoder extends sfWidgetForm { { 'address' => '', 'lon' => '1.913899', 'lat' => '47.900486' )); $this->addOption('div.class', 'geocoder-widget'); $this->addOption('map.height', '300px'); $this->addOption('map.width', '500px'); $this->addOption('map.style', ""); $this->addOption('lookup.name', ""); $this->addOption('template.html', ' <div id="{div.id}" class="{div.class}"> {input.search} <input type="submit" value="{input.lookup.name}" id="{input.lookup.id}" /> <br /> {input.lon} {input.lat} <div id="{map.id}" style="width:{map.width};height:{map.height};{map.style}"></div> </div> '); $this->addOption('template.javascript', ' <script type="text/javascript"> (function() { var $submit = document.getElementById("{input.lookup.id}"); var $address = document.getElementById("{input.address.id}"); var $lat = document.getElementById("{input.lat.id}"); var $lon = document.getElementById("{input.lon.id}"); var map = new GMap2(document.getElementById("{map.id}")); var center = new GLatLng( $lat.value||47.900486, $lon.value||1.913899); var marker = new GMarker(center, {draggable: true}); var geocoder = new GClientGeocoder(); map.addControl(new GSmallMapControl()); map.addControl(new GMapTypeControl()); map.setCenter(center, 5); map.addOverlay(marker); setPosition(center); GEvent.addListener(marker, "dragend", function() { var point = marker.getPoint(); map.panTo(point); setPosition(point); }); GEvent.addListener(map, "moveend", function() { var point = map.getCenter() marker.setLatLng(point); setPosition(point); }); $submit.onclick = function(){showAddress($address.value)}; function showAddress() { if(!arguments[0]) return false; var address = arguments[0]; if (geocoder) { geocoder.getLatLng(address, function(point) { if (!point) { alert(address + " not found"); }else{ setPosition(point); map.setCenter(point, 14); } }); } } function setPosition(){ $lat.value = arguments[0].lat(); $lon.value = arguments[0].lng(); } })(); </script> '); } public function getJavascripts() { } { // define main template variables '{div.id}' => $this->generateId($name), '{div.class}' => $this->getOption('div.class'), '{map.id}' => $this->generateId($name.'[map]'), '{map.style}' => $this->getOption('map.style'), '{map.height}' => $this->getOption('map.height'), '{map.width}' => $this->getOption('map.width'), '{input.lookup.id}' => $this->generateId($name.'[lookup]'), '{input.lookup.name}' => $this->getOption('lookup.name'), '{input.address.id}' => $this->generateId($name.'[address]'), '{input.lat.id}' => $this->generateId($name.'[lat]'), '{input.lon.id}' => $this->generateId($name.'[lon]'), ); // v���©rifie si la valeur est valide // d���©finit le widget pour le champ adresse $template_vars['{input.search}'] = $address->render($name.'[address]', $value['address']); // d���©finit les widgets pour les champs : longitude et latitude $hidden = new sfWidgetFormInputText; $template_vars['{input.lon}'] = $hidden->render($name.'[lon', $value['lon']); $template_vars['{input.lat}'] = $hidden->render($name.'[lat]', $value['lat']); // assemble le mod���¨le avec les valeurs $this->getOption('template.html').$this->getOption('template.javascript'), $template_vars ); } } class sfValidatorGeocoder extends sfValidatorBase { protected function doClean($value) { { throw new sfValidatorError($this, 'invalid'); } try { $address = new sfValidatorString(array( 'min_length' => 10, 'max_length' => 255, 'required' => true )); $value['address'] = $address->clean(isset($value['address']) ? $value['address'] : null); } catch(sfValidatorError $e) { throw new sfValidatorError($this, 'invalid'); } return $value; } }