(reprise du message précédent)
Lis et teste ce bout de code :
Modifié par niuxe (11 Jan 2024 - 09:36)
Lis et teste ce bout de code :
<?php
function slugify($text, string $divider = '-'){
$text = preg_replace('~[^\pL\d]+~u', $divider, $text);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, $divider);
$text = preg_replace('~-+~', $divider, $text);
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
if(!empty($_POST)){
// nettoie les données du formulaire
$clean_methods = [
'trim',
'strip_tags',
'htmlspecialchars',
];
foreach($clean_methods as $method){
$_POST = array_map($method, $_POST);
}
// validation
$_POST['errors'] = [];
foreach($_POST as $key => $value){
switch($key){
case 'email':
if(empty($value)){
$_POST['errors'][$key] = "Ne doit pas être vide";
}else if(!filter_var($value, FILTER_VALIDATE_EMAIL)){
$_POST['errors'][$key] = "l'email ne semble pas valide";
}
break;
case 'message':
$words = file('./interdictions.txt');
$value_slugified = slugify($value);
if(empty($value)){
$_POST['errors'][$key] = "Ne doit pas être vide";
}else{
$words_slugified = array_map('slugify', $words);
$pattern = sprintf('#\b(%s)\b#', implode('|', $words_slugified));
if(preg_match($pattern, $value_slugified, $word)){
$_POST['errors'][$key] = sprintf("Le mot <strong>%s</strong> est interdit, veuillez ne pas utiliser l'écriture inclusive", current($word));
}
}
break;
}
}
if(empty($_POST['errors'])){
// envoyer email
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.8.1/dist/css/foundation-float.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.8.1/dist/css/foundation-prototype.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.8.1/dist/css/foundation-rtl.min.css" crossorigin="anonymous">
<style>
.grid-container{
max-width:50rem;
}
input[type=text], textarea{
margin: 0;
}
.input{
margin-top: 1rem;
}
span.error{
color: red;
}
</style>
</head>
<body>
<main class="grid-container margin-vertical-3">
<form method="post">
<div class="input text">
<label>
<span>email</span>
<input type="text" name="email" value="<?= !empty($_POST['email']) ? $_POST['email'] : ''?>">
</label>
<?php if(!empty($_POST['errors']['email'] )): ?>
<span class="error"><?= $_POST['errors']['email'] ?></span>
<?php endif ?>
</div>
<div class="input textarea">
<label>
<span>message</span>
<textarea name="message"><?= !empty($_POST['message']) ? $_POST['message'] : ''?></textarea>
</label>
<?php if(!empty($_POST['errors']['message'] )): ?>
<span class="error"><?= $_POST['errors']['message'] ?></span>
<?php endif ?>
</div>
<div class="input submit">
<button class="button" type="submit">envoyer</button>
</div>
</form>
</main>
</body>
</html>
Modifié par niuxe (11 Jan 2024 - 09:36)