Skip to content

Commit 666bb3b

Browse files
committed
Merge branch 'validar_cpf_e_cnpj'
2 parents 3a2a55f + e7a3607 commit 666bb3b

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/geekcom/validator-docs/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/geekcom/validator-docs/?branch=master)
44

5-
Biblioteca Laravel para validação de CPF, CNPJ e CNH.
5+
Biblioteca Laravel para validação de CPF, CNPJ, CPF/CNPJ (quando salvos no mesmo atributo) e CNH.
66

77
# Instalação
88

@@ -28,13 +28,16 @@ Após a instalação, adicione no arquivo `config/app.php` no array `providers`
2828
geekcom\ValidatorDocs\ValidatorProvider::class
2929
```
3030

31-
Para utilizar a validação agora, basta fazer o procedimento padrão do `Laravel`, confira na documentação especifica para a sua versão
31+
Para utilizar a validação agora, basta fazer o procedimento padrão do `Laravel`, confira na documentação especifica para a sua versão,
3232
a diferença é que agora, você terá os seguintes métodos de validação:
3333

34+
3435
* cnpj - Verifica se o CNPJ é valido. Para testar, basta utilizar o site http://www.geradorcnpj.com/
3536
* cpf - Verifica se o cpf é valido. Para testar, basta utilizar o site http://geradordecpf.org
37+
* cpf_cnpj - Verifica se é um cpf ou cnpj valido. Para testar, basta utilizar um dos sites acima
3638
* formato_cnpj - Verifica se a mascara do CNPJ é válida. ( 99.999.999/9999-99 )
3739
* formato_cpf - Verifica se a mascara do cpf é válida. ( 999.999.999-99 )
40+
* formato_cpf_cnpj - Verifica se a mascara do cpf ou cnpj é válida. ( 999.999.999-99 ) ou ( 99.999.999/9999-99 )
3841

3942

4043
Então, podemos usar um simples teste onde dizemos que o campo CPF será obrigatório e usamos a biblioteca para validar:
@@ -45,4 +48,12 @@ $this->validate($request, [
4548
]);
4649
```
4750

51+
No exemplo abaixo, fazemos um teste onde validamos a formatação e a validade de um CPF ou CNPJ, para os casos onde a informação deva ser salva em um mesmo atributo:
52+
53+
```php
54+
$this->validate($request, [
55+
'cpf_or_cnpj' => 'formato_cpf_cnpj|cpf_cnpj',
56+
]);
57+
```
58+
4859
Fique a vontade para contribuir fazendo um fork.

src/validator-docs/Validator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ protected function validateFormatoCnpj($attribute, $value)
3333
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
3434
}
3535

36+
/**
37+
* Valida o formato do cpf ou cnpj
38+
* @param string $attribute
39+
* @param string $value
40+
* @return boolean
41+
*/
42+
protected function validateFormatoCpfCnpj($attribute, $value)
43+
{
44+
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
45+
}
46+
3647
/**
3748
* Valida se o CPF é válido
3849
* @param string $attribute
@@ -96,6 +107,17 @@ protected function validateCnpj($attribute, $value)
96107

97108
}
98109

110+
/**
111+
* Valida se o CNPJ é válido
112+
* @param string $attribute
113+
* @param string $value
114+
* @return boolean
115+
*/
116+
protected function validateCpfCnpj($attribute, $value)
117+
{
118+
return ($this->validateCpf($attribute, $value) || $this->validateCnpj($attribute, $value));
119+
}
120+
99121
/**
100122
* Valida se o CNH é válido
101123
* @param string $attribute

src/validator-docs/ValidatorProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ protected function getMessages()
4040
'cnh' => 'O campo :attribute não é uma carteira nacional de habilitação válida',
4141
'cnpj' => 'O campo :attribute não é um CNPJ válido',
4242
'cpf' => 'O campo :attribute não é um CPF válido',
43+
'cpf_cnpj' => 'O campo :attribute não é válido',
4344
'formato_cnpj' => 'O campo :attribute não possui o formato válido de CNPJ',
4445
'formato_cpf' => 'O campo :attribute não possui o formato válido de CPF',
46+
'formato_cpf_cnpj' => 'O campo :attribute não possui um formato válido',
4547
];
4648
}
4749

tests/TestValidator.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,43 @@ public function testCnpjFormato()
7474
$this->assertTrue($incorrect->fails());
7575
}
7676

77+
78+
public function testCpfCnpj()
79+
{
80+
$correct = \Validator::make(
81+
['certo' => '53.084.587/0001-20'],
82+
['certo' => 'cpf-cnpj']
83+
);
84+
85+
$incorrect = \Validator::make(
86+
['errado' => '99800-1926'],
87+
['errado' => 'cpf-cnpj']
88+
);
89+
90+
$this->assertTrue($correct->passes());
91+
92+
$this->assertTrue($incorrect->fails());
93+
}
94+
95+
96+
public function testCpfCnpjFormato()
97+
{
98+
$correct = \Validator::make(
99+
['certo' => '094.050.986-59'],
100+
['certo' => 'formato-cpf-cnpj']
101+
);
102+
103+
$incorrect = \Validator::make(
104+
['errado' => '51.084.587/000120'],
105+
['errado' => 'formato-cpf-cnpj']
106+
);
107+
108+
$this->assertTrue($correct->passes());
109+
110+
$this->assertTrue($incorrect->fails());
111+
}
112+
113+
77114
public function testCnh()
78115
{
79116
$correct = \Validator::make(

0 commit comments

Comments
 (0)