diff --git a/BrValidationGrailsPlugin.groovy b/BrValidationGrailsPlugin.groovy index 7a56078..18d40d9 100644 --- a/BrValidationGrailsPlugin.groovy +++ b/BrValidationGrailsPlugin.groovy @@ -23,17 +23,17 @@ import org.codehaus.groovy.grails.validation.ConstrainedProperty /** - * BrValidation Plugin. Implements gorm validation to same documents - * (CPF and CNPJ) and codes formats (CEP) used in Brazil + * BrValidation Plugin. Implements gorm validation to same documents + * (CPF and CNPJ) and codes formats (CEP) used in Brazil * * @author Oscar Konno Sampaio (oscarks@gmail.com) * @since 0.1 */ class BrValidationGrailsPlugin { - def version = "0.3" + def version = "0.4" def grailsVersion = "1.1.1 > *" def dependsOn = [:] - + def pluginExcludes = [ 'grails-app/domain/**' ] @@ -41,17 +41,17 @@ class BrValidationGrailsPlugin { def title = "Br Validation Plugin" // Headline display name of the plugin def author = "Oscar Konno Sampaio" def authorEmail = "oscarks@gmail.com" - def description = '''Implements gorm validation to same documents -(CPF and CNPJ) and codes formats (CEP) used in Brazil + def description = '''Implements gorm validation to same documents +(CPF and CNPJ) and codes formats (CEP) used in Brazil ''' def documentation = "http://grails.org/plugin/br-validation" def license = "APACHE" - + def issueManagement = [system: "github", url: "https://github.com/oscarks/BrValidation/issues"] def scm = [ url: "https://github.com/oscarks/BrValidation" ] - - def developers = [ + + def developers = [ [ name: "Oscar Konno", email: "oscarks@gmail.com" ], [ name: "Leandro G. Gehlen", email: "leandrogehlen@gmail.com" ] ] @@ -78,7 +78,7 @@ class BrValidationGrailsPlugin { } def doWithApplicationContext = { applicationContext -> - // TODO Implement post initialization spring config (optional) + // TODO Implement post initialization spring config (optional) } def onChange = { event -> diff --git a/README.md b/README.md index 868bc4f..7edf19e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Br Validation Implements same validations in grails constraints for units used in application from Brazil: CPF, CNPJ and CEP. CPF - Cadastro de Pessoa FĂ­sica (like a Security Social Numeber in USA) -CNPJ - Cadastro Nacional de Pessoa Juridica +CNPJ - Cadastro Nacional de Pessoa Juridica CEP - Postal Code to Brazilian territory Install @@ -54,7 +54,7 @@ class PessoaJuridica { ```groovy class Pessoa { - String nome + String nome String cpfCnpj static constraints { cpfCnpj cpfcnpj: true @@ -85,9 +85,9 @@ then, if you define: grails.plugins.brValidation.validation.type=masked ``` -the BrValidation will consider a valid CPF or CNPJ when they has mask. In the same way if unmasked is configured only unmasked data will pass in validation. +the BrValidation will consider a valid CPF or CNPJ when they has mask. In the same way if unmasked is configured only unmasked data will pass in validation. If both is configured, boths, masked and unmasked data is validated. - + Tag Lib ------- @@ -95,7 +95,9 @@ Tag Lib Now the BrValidation has a tag to format unmasked data to render masked one. For this use the tag bellow in gsp files: ```html - -``` + + + +``` diff --git a/grails-app/taglib/grails/plugins/brvalidation/FormatTagLib.groovy b/grails-app/taglib/grails/plugins/brvalidation/FormatTagLib.groovy index 9725bed..3fd5103 100644 --- a/grails-app/taglib/grails/plugins/brvalidation/FormatTagLib.groovy +++ b/grails-app/taglib/grails/plugins/brvalidation/FormatTagLib.groovy @@ -5,46 +5,61 @@ import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException class FormatTagLib { /** * Format a CPF value with the mask 999.999.999-99 - * - * @attr cpf REQUIRED the cpf value + * + * @attr value REQUIRED the cpf value * @attr unknow character to be used when the CPF is not informed */ def formatCpf = { attrs-> - def cpf=attrs.cpf - if (cpf) { - if (cpf instanceof Number) { - def c=cpf.toString() - while (c.size()<11) - c='0'+c - cpf=c - } - cpf=cpf.replaceAll('\\.','').replaceAll('-','') - if (cpf.size()!=11) throw new GrailsTagException("CPF with invalid size (found ${cpf.size()} expected 11") - out << "${cpf.substring(0,3)}.${cpf.substring(3,6)}.${cpf.substring(6,9)}-${cpf.substring(9,11)}" - } else if (attrs.unknow) { - if (!(attrs.unknow instanceof String) || attrs.unknow.size()!=1) - throw new GrailsTagException("Unknow character invalid") - def u=attrs.unknow - out << u.multiply(3)+'.'+u.multiply(3)+'.'+u.multiply(3)+'-'+u.multiply(2) + def cpf = attrs.value + processCpf(cpf,out) + } + + /** + * Format a CNPJ value with the mask 99.999.999/9999-99 + * + * @attr value REQUIRED the cnpj value + * @attr unknow character to be used when the CPF is not informed + */ + def formatCnpj = { attrs-> + def cnpj = attrs.value + processCnpj(cnpj,out) + } + + /** + * Format a CNPJ value with the mask 99.999.999/9999-99 + * + * @attr value REQUIRED the cpf ou cnpj value + * @attr unknow character to be used when the CPF is not informed + */ + def formatCpfCnpj = { attrs-> + def cpfcnpj = attrs.value + if(cpfcnpj?.toString().size()==14){ + processCnpj(cpfcnpj,out) + + } else if(cpfcnpj?.toString().size()==14){{ + processCpf(cpfcnpj,out) + + } else { + throw new GrailsTagException("Unknow character invalid") } } - + /** - * Format a CPF value with the mask 99.999.999/9999-99 + * Format a CNPJ value with the mask 99.999.999/9999-99 * * @attr cnpj REQUIRED the cnpj value + * @attr out REQUIRED output to taglib */ - def formatCnpj = { attrs-> - def cnpj=attrs.cnpj + private void processCnpj(cnpj,out){ if (cnpj) { if (cnpj instanceof Number) { def c=cnpj.toString() while (c.size()<14) c='0'+c cnpj=c - } + } cnpj=cnpj.replaceAll('\\.','').replaceAll('-','').replaceAll('/','') - if (cnpj.size()!=11) throw new GrailsTagException("CPF with invalid size (found ${cnpj.size()} expected 11") + if (cnpj.size()!=14) throw new GrailsTagException("CNPJ with invalid size (found ${cnpj.size()} expected 14") out << "${cnpj.substring(0,2)}.${cnpj.substring(2,5)}.${cnpj.substring(5,8)}/${cnpj.substring(8,12)}-${cnpj.substring(12,14)}" } else if (attrs.unknow) { if (!(attrs.unknow instanceof String) || attrs.unknow.size()!=1) @@ -53,4 +68,29 @@ class FormatTagLib { out << u.multiply(2)+'.'+u.multiply(3)+'.'+u.multiply(3)+'/'+u.multiply(4)+'-'+u.multiply(2) } } + + /** + * Format a CPF value with the mask 999.999.999-99 + * + * @attr cpf REQUIRED the cpf value + * @attr out REQUIRED output to taglib + */ + private void processCpf(cpf,out){ + if (cpf) { + if (cpf instanceof Number) { + def c=cpf.toString() + while (c.size()<11) + c='0'+c + cpf=c + } + cpf=cpf.replaceAll('\\.','').replaceAll('-','') + if (cpf.size()!=11) throw new GrailsTagException("CPF with invalid size (found ${cpf.size()} expected 11") + out << "${cpf.substring(0,3)}.${cpf.substring(3,6)}.${cpf.substring(6,9)}-${cpf.substring(9,11)}" + } else if (attrs.unknow) { + if (!(attrs.unknow instanceof String) || attrs.unknow.size()!=1) + throw new GrailsTagException("Unknow character invalid") + def u=attrs.unknow + out << u.multiply(3)+'.'+u.multiply(3)+'.'+u.multiply(3)+'-'+u.multiply(2) + } + } } diff --git a/src/groovy/grails/plugins/brvalidation/constraint/CepConstraint.groovy b/src/groovy/grails/plugins/brvalidation/constraint/CepConstraint.groovy index a76f885..9440340 100644 --- a/src/groovy/grails/plugins/brvalidation/constraint/CepConstraint.groovy +++ b/src/groovy/grails/plugins/brvalidation/constraint/CepConstraint.groovy @@ -1,70 +1,70 @@ /* - * Copyright (c) 2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +* Copyright (c) 2012 the original author or authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ package grails.plugins.brvalidation.constraint import org.codehaus.groovy.grails.validation.AbstractConstraint import org.springframework.validation.Errors /** - * Add CEP validation to gorm constraints - * - * @author Oscar Konno Sampaio (oscarks@gmail.com) - * @since 0.1 - * - * 12/12/2009 - version 0.1 - * First commit - */ +* Add CEP validation to gorm constraints +* +* @author Oscar Konno Sampaio (oscarks@gmail.com) +* @since 0.1 +* +* 12/12/2009 - version 0.1 +* First commit +*/ public class CepConstraint extends AbstractConstraint { - private static final String DEFAULT_NOT_POSTAL_CODE_MESSAGE_CODE = "default.not.cpf.message"; - public static final String POSTAL_CODE_CONSTRAINT = "cep"; + private static final String DEFAULT_NOT_POSTAL_CODE_MESSAGE_CODE = "default.not.cep.message"; + public static final String POSTAL_CODE_CONSTRAINT = "cep"; - private boolean postalCode; + private boolean postalCode; - public void setParameter(Object constraintParameter) { - if(!(constraintParameter instanceof Boolean)) - throw new IllegalArgumentException("Parameter for constraint [" - +POSTAL_CODE_CONSTRAINT+"] of property [" - +constraintPropertyName+"] of class [" - +constraintOwningClass+"] must be a boolean value"); + public void setParameter(Object constraintParameter) { + if(!(constraintParameter instanceof Boolean)) + throw new IllegalArgumentException("Parameter for constraint [" + +POSTAL_CODE_CONSTRAINT+"] of property [" + +constraintPropertyName+"] of class [" + +constraintOwningClass+"] must be a boolean value"); - this.postalCode = ((Boolean)constraintParameter).booleanValue(); - super.setParameter(constraintParameter); - } + this.postalCode = ((Boolean)constraintParameter).booleanValue(); + super.setParameter(constraintParameter); + } - protected void processValidate(Object target, Object propertyValue, Errors errors) { - if (! validPostalCode(target, propertyValue)) { - def args = (Object[]) [constraintPropertyName, constraintOwningClass, - propertyValue] - super.rejectValue(target, errors, DEFAULT_NOT_POSTAL_CODE_MESSAGE_CODE, - "not." + POSTAL_CODE_CONSTRAINT, args); - } - } + protected void processValidate(Object target, Object propertyValue, Errors errors) { + if (! validPostalCode(target, propertyValue)) { + def args = (Object[]) [constraintPropertyName, constraintOwningClass, + propertyValue] + super.rejectValue(target, errors, DEFAULT_NOT_POSTAL_CODE_MESSAGE_CODE, + "not." + POSTAL_CODE_CONSTRAINT, args); + } + } - boolean supports(Class type) { - return type != null && String.class.isAssignableFrom(type); - } + boolean supports(Class type) { + return type != null && String.class.isAssignableFrom(type); + } - String getName() { - return POSTAL_CODE_CONSTRAINT; - } + String getName() { + return POSTAL_CODE_CONSTRAINT; + } - boolean validPostalCode(target, propertyValue) { - propertyValue==~ /^[0-9]{5}-[0-9]{3}$/ - } + boolean validPostalCode(target, propertyValue) { + propertyValue==~ /^[0-9]{5}-[0-9]{3}$/ + } -} + }