Created
May 31, 2019 23:01
-
-
Save erikfig/82cff1dd6b91a332fa9ec2d20effdd9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<form action="result.php" method="post"> | |
<input type="text" name="name"> | |
<input type="submit" value="salvar"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$name = filter_input(INPUT_POST, 'name'); // pego o valor enviado pelo formulário | |
try { | |
if (!$name) { | |
throw new InvalidArgumentException('Nome não pode ficar em branco'); | |
} | |
preg_match('/\s/', $name, $matches); | |
if (count($matches) > 0) { | |
throw new InvalidArgumentException('Nome não pode conter espaços'); | |
} | |
echo 'Ok, valor correto'; | |
} catch(InvalidArgumentException $e) { | |
$error_message = [ | |
'Erro de validação:', | |
$e->getMessage() | |
]; | |
echo implode(' ', $error_message); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment