Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions BrValidationGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ 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"
Comment thread
jacksonvpj marked this conversation as resolved.
def grailsVersion = "1.1.1 > *"
def dependsOn = [:]

def pluginExcludes = [
'grails-app/domain/**'
]

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" ]
]
Expand All @@ -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 ->
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,7 +54,7 @@ class PessoaJuridica {

```groovy
class Pessoa {
String nome
String nome
String cpfCnpj
static constraints {
cpfCnpj cpfcnpj: true
Expand Down Expand Up @@ -85,17 +85,19 @@ 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
-------

Now the BrValidation has a tag to format unmasked data to render masked one. For this use the tag bellow in gsp files:

```html
<g:formatCpf cpf="${personInstance.cpf"/>
```
<g:formatCpf value="${personInstance.cpf"/>
Comment thread
jacksonvpj marked this conversation as resolved.

<g:formatCnpj value="${personInstance.cnpj"/>
Comment thread
jacksonvpj marked this conversation as resolved.

<g:formatCpfCnpj value="${personInstance.cpfcnpj"/>
Comment thread
jacksonvpj marked this conversation as resolved.
```
88 changes: 64 additions & 24 deletions grails-app/taglib/grails/plugins/brvalidation/FormatTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
jacksonvpj marked this conversation as resolved.
* @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
Comment thread
jacksonvpj marked this conversation as resolved.
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->
Comment thread
jacksonvpj marked this conversation as resolved.
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")
Comment thread
jacksonvpj marked this conversation as resolved.
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)
Expand All @@ -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)
}
}
}
104 changes: 52 additions & 52 deletions src/groovy/grails/plugins/brvalidation/constraint/CepConstraint.groovy
Original file line number Diff line number Diff line change
@@ -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";
Comment thread
jacksonvpj marked this conversation as resolved.
Outdated
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}$/
}

}
}