4. Alfanumérico
@alganet
30 de Novembro PHP Conference 2012 4
5. Alfanumérico Aceita _
@alganet
30 de Novembro PHP Conference 2012 5
6. Alfanumérico Aceita _
@alganet
Sem espaços
30 de Novembro PHP Conference 2012 6
7. Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 7
8. /^[[:alnum:]_]{1,15}$/
(Expressão Regular)
Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 8
9. /^[[:alnum:]_]{1,15}$/
Alfanumérico Aceita _
@alexandre_gomes_gaigalas
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 9
10. /^[[:alnum:]_]{1,15}$/
Alfanumérico Aceita _
@al-ga-net
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 10
11. /^[[:alnum:]_]{1,15}$/
Alfanumérico Aceita _
@Alexandre Gomes
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 11
12. E se eu quiser exibir os erros?
30 de Novembro PHP Conference 2012 12
13. if (!ctype_alnum(str_replace('_', '', $username)))
echo “Username pode conter apenas letras, números e _”;
Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 13
14. if (!ctype_alnum(str_replace('_', '', $username)))
echo “Username pode conter apenas letras, números e _”;
if (strlen($username) < 1 || strlen($username) > 15)
echo “Username pode conter de 1 a 15 caracteres”;
Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 14
15. $e = array();
if (!ctype_alnum(str_replace('_', '', $username)))
$e[] = “Username pode conter apenas letras, números e _”;
if (strlen($username) < 1 || strlen($username) > 15)
$e[] = “Username pode conter de 1 a 15 caracteres”;
if ($e)
echo implode($e);
Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 15
16. E o erro para o “Sem espaços”?
30 de Novembro PHP Conference 2012 16
17. $e = array();
if (!ctype_alnum(str_replace('_', '', $username)))
$e[] = “Username pode conter apenas letras, números e _”;
if (strlen($username) < 1 || strlen($username) > 15)
$e[] = “Username pode conter de 1 a 15 caracteres”;
if (false !== strpos($username, “ ”)
$e[] = “Username não pode conter espaços”;
/*...*/
Alfanumérico Aceita _
@alganet
Sem espaços 1-15 caracteres
30 de Novembro PHP Conference 2012 17
19. Construindo um Validator
use RespectValidationRules as r;
$validator = new rAllOf(
);
30 de Novembro PHP Conference 2012 19
20. Construindo um Validator
use RespectValidationRules as r;
$validator = new rAllOf(
new rAlnum(“_”)
);
30 de Novembro PHP Conference 2012 20
21. Construindo um Validator
use RespectValidationRules as r;
$validator = new rAllOf(
new rAlnum(“_”),
new rNoWhitespace
);
30 de Novembro PHP Conference 2012 21
22. Construindo um Validator
use RespectValidationRules as r;
$validator = new rAllOf(
new rAlnum(“_”),
new rNoWhitespace,
new rLength(1, 15)
);
30 de Novembro PHP Conference 2012 22
23. Construindo um Validator
use RespectValidationRules as r;
$validator = new rAllOf(
new rAlnum(“_”),
new rNoWhitespace,
new rLength(1, 15)
);
$isOk = $validator->validate(“alganet”);
30 de Novembro PHP Conference 2012 23
24. Tem um jeito mais simples?
30 de Novembro PHP Conference 2012 24
25. Construção via Fluent Builder
use RespectValidationValidator as v;
$validator = v::alnum('_')
->noWhitespace()
->length(1, 15);
$isOk = $validator->validate(“alganet”);
30 de Novembro PHP Conference 2012 25
30. //Dispara exception para o primeiro
//erro
$validator->check($algo);
30 de Novembro PHP Conference 2012 30
31. E se eu quiser todos os erros?
30 de Novembro PHP Conference 2012 31
32. try {
$validator->assert(“e ae”);
} catch (InvalidArgumentException $e) {
echo $e->getFullMessage();
}
30 de Novembro PHP Conference 2012 32
33. $validator->assert(“td errado#”);
-All of the 3 required rules must pass
|-"td errado#" must contain only letters (a-z)
| and digits (0-9)
|-"td errado#" must not contain whitespace
-"td errado#" must have a length between 1
and 15
30 de Novembro PHP Conference 2012 33
34. E se eu quiser agrupar só alguns
erros?
30 de Novembro PHP Conference 2012 34
47. //Valida atributos de objetos
v::object()->attribute(“name”);
//Valida chaves de array
v::arr()->key(“email”);
30 de Novembro PHP Conference 2012 47
48. //Valida atributos de objetos
v::object()->attribute(“name”)
//N atributos...
->attribute(“sex”);
//Valida chaves de array
v::arr()->key(“email”)
->key(“sex”);
30 de Novembro PHP Conference 2012 48
49. //Valida atributos de objetos
v::object()->attribute(“name”)
//N atributos...
->attribute(“sex”,
//Valida o valor também
v::in('F', 'M'));
//Valida chaves de array
v::arr()->key(“email”)
->key(“sex”,
v::in('F', 'M'));
30 de Novembro PHP Conference 2012 49
50. E se eu quiser negar uma regra?
30 de Novembro PHP Conference 2012 50
52. E as mensagens pra regras
negadas?
30 de Novembro PHP Conference 2012 52
53. //”123” must be a string
v::string()->check(123);
//”bla bla” must not be a string
v::not(v::string())->check('bla bla');
30 de Novembro PHP Conference 2012 53
54. Dá pra mudar o “123” e “bla bla”
nas mensagens?
30 de Novembro PHP Conference 2012 54
55. //Nome Completo must be a string
v::string()
->setName(“Nome Completo”)
->check(123);
//Idade must not be a string
v::not(v::string())
->setName(“Idade”)
->check('bla bla');
30 de Novembro PHP Conference 2012 55
56. //Validadores Básicos de Tipo
v::arr()
v::date()
v::float()
v::int()
v::nullValue()
v::numeric()
v::object()
v::string()
v::instance()
30 de Novembro PHP Conference 2012 56
70. Ok, mas posso ter meu próprio
objeto?
30 de Novembro PHP Conference 2012 70
71. use RespectValidation;
class Universe
implements ValidationValidatable
extends ValidationRulesAbstractRule
{
public function validate($input)
{
return $input === 42;
}
}
v::int()->addRule(new Universe);
30 de Novembro PHP Conference 2012 71
73. use RespectValidation;
class UniverseException
extends ValidationExceptionsValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD
=> '{{name}} must be 42',
),
self::MODE_NEGATIVE => array(
self::STANDARD
=> '{{name}} must not be 42',
)
);
}
30 de Novembro PHP Conference 2012 73