Commits

wbraganca committed 940bdda

Bugfix quando um cep eh invalido

Comments (0)

Files changed (4)

  * @package   extensions
  * @author	  Wanderson Bragança <wanderson.wbc@gmail.com>
  * @link	  https://bitbucket.org/wbraganca/correios
- * @version   1.0.2
+ * @version   1.0.3
  */
 class BuscaPorCep extends CWidget
 {
 		$fieldsMap = CJSON::encode($fieldsMap);
 		$postalCodeID = CHtml::activeID($this->model, $this->attribute);
 		$url = Yii::app()->createAbsoluteUrl($this->url);
+		$errorMsgID = $postalCodeID . '_em_';
 		$script = <<<EOF
 jQuery("{$this->target}").on("click", function(){
 	var fieldsMap = {$fieldsMap};
 		url: "{$url}",
 		data: {'cep':jQuery("#{$postalCodeID}").val()},
 		success: function(json){
-			if( json['result'] == '1'){
-				$.each(fieldsMap, function(key, val) {
-					jQuery('#' + val).removeAttr("disabled","true");
-					jQuery('#' + val).val(unescape(json[key]));
-				});
+			$.each(fieldsMap, function(key, val) {
+				jQuery('#' + val).removeAttr("disabled","true");
+				jQuery('#' + val).val(unescape(json[key]));
+			});
+			if( json['result'] == '0'){
+				jQuery("#{$errorMsgID}").val(json['result_text']);
 			}
 		}
 	});

BuscaPorCepApp.php

  * @package   extensions
  * @author	  Wanderson Bragança <wanderson.wbc@gmail.com>
  * @link	  https://bitbucket.org/wbraganca/correios
- * @version   1.0.2
+ * @version   1.0.3
  */
 class BuscaPorCepApp extends CApplicationComponent
 {
 	public function init()
 	{
 		parent::init();
-		$this->fieldsMap['restul_text'] = Yii::t('BuscaPorCepApp.correios', 'Address not found.');
+		$this->fieldsMap['result_text'] = Yii::t('BuscaPorCepApp.correios', 'Address not found.');
 	}
 
 	public function run($postalCode)
 		$postalCode = str_replace('-', '', $postalCode);
 		if( empty($postalCode) || strlen($postalCode) != 8 ){
 			$out = $this->fieldsMap;
-			$out['restul_text'] = Yii::t('BuscaPorCepApp.correios', 'Invalid postcode.');
+			$out['result_text'] = Yii::t('BuscaPorCepApp.correios', 'Invalid postcode.');
 			$out['result'] = 0; 
 			return $out;
 		}else{
 			$doc = new DOMDocument(); 
 			if( $doc->loadHTML($cleanHTML) ) {
 				$tagData = $doc->getElementsByTagName('td');
-				
 				if( $tagData->length > 0 ){
 					$out['location']    = $tagData->item(0)->nodeValue;
 					$out['district']    = $tagData->item(1)->nodeValue;
 					$out['city']        = $tagData->item(2)->nodeValue;
 					$out['state']       = $tagData->item(3)->nodeValue;
 					$out['result']      = 1;
-					$out['restul_text'] = Yii::t('BuscaPorCepApp.correios', 'Address found.');
+					$out['result_text'] = Yii::t('BuscaPorCepApp.correios', 'Address found.');
 				}
 			}
 		}
 Extensão para buscar endereço por CEP para o Yii Framework 
 ==========================================================
 
-Versão: 1.0.2
-------------
+Versão: 1.0.3
+-------------
 
 A busca é realizada diretamente no website dos correios.
 

actions/BuscaPorCepAction.php

  * @package   extensions
  * @author	  Wanderson Bragança <wanderson.wbc@gmail.com>
  * @link	  https://bitbucket.org/wbraganca/correios
- * @version   1.0.2
+ * @version   1.0.3
  */
 class BuscaPorCepAction extends CAction
 {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.