Last active
December 21, 2015 04:09
-
-
Save jimmylatreille/6247529 to your computer and use it in GitHub Desktop.
CLASS FORM PHP VALIDATE INPUT
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> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Ma fonction formField</title> | |
</head> | |
<body> | |
<?php | |
if(isset($_POST['btnSubmit'])){ | |
$formVideo = array('nomClient'=>array('required'=>true, 'regExp'=>'/^[a-zA-Z]+([a-zA-Z]*\s*)*$/'), | |
'prenomClient'=>array('required'=>true, 'regExp'=>'/^[a-zA-Z]+([a-zA-Z]*\s*)*$/'), | |
'courrielClient'=>array('required'=>true, 'regExp'=>'/^(\w+\.?)*\w+@(\w+\.?)+[a-zA-Z]+$/'), | |
'urlClient'=>array('required'=>true, 'regExp'=>'/^http(?:s?):\/\/(www\.)?youtube\.com\/watch\?v=([\w-]+).*$/i'), | |
'messageClient'=>array('required'=>false, 'regExp'=>'/^[a-zÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿA-Z0-9\.\#<>,;\-&\r\n ]{1,1000}$/i') | |
); | |
$post = Form::field($formVideo, 'Client'); | |
var_dump($post['fields']); | |
foreach ($post['errors'] as $key => $val) { | |
echo 'vous devez entrer votre '.$val.' valide<br />'; | |
} | |
} | |
?> | |
<form action="#" id="formVideo" method="POST"> | |
<h1>Télécharger une vidéo</h1> | |
<p>Champs obligatoire <em>*</em></p> | |
<label for="nomClient">Nom *</label> | |
<input type="text" name="nomClient"> | |
<br /> | |
<label for="prenomClient">Prenom *</label> | |
<input type="text" name="prenomClient"> | |
<br /> | |
<label for="courrielClient">Courriel *</label> | |
<input type="text" name="courrielClient"> | |
<br /> | |
<label for="urlClient">Url *</label> | |
<input type="text" name="urlClient"> | |
<br /> | |
<label for="messageClient">Message</label> | |
<input type="text" name="messageClient"> | |
<br /> | |
<input type="submit" name="btnSubmit"> | |
</form> | |
</body> | |
</html> | |
<?php | |
class Form { | |
//fonction qui valide les input d'un fomulaire en plus qui gère les erreurs | |
public static function field(Array $fields, $strReplace){ | |
$errors = []; | |
$post = []; | |
foreach ($fields as $name=>$valid) { | |
if (isset($_POST[$name]) && !empty($_POST[$name]) | |
&& preg_match($valid['regExp'], trim($_POST[$name]))){ | |
$post[$name] = self::clean($_POST[$name]); | |
}else{ | |
if($valid['required']) | |
$errors[$name] = preg_replace("/$strReplace/", "", $name); | |
} | |
} | |
return array('errors' => $errors,'fields' => $post); | |
} // fin de la fonction field() | |
//fonction qui enleve les espace de fin et de debut en plus des espace multiple | |
public static function clean($inputStr){ | |
$tableau = []; | |
$index = 0; | |
$inputStr = preg_split("/(\s)+/i", $inputStr); | |
foreach ($inputStr as $key => $val) { | |
if($val != ""){ | |
$tableau[$index] = $val; | |
$index++; | |
} | |
} | |
return implode(" ", $tableau); | |
} //fin de la fonction clean | |
} // fin de la class formField | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment