Connexion
S'inscrire
Actualités
Quoi de neuf
Activités générales
Auteurs
Forums
Nouveaux messages
Rechercher un forum
Quoi de neuf
Nouveaux messages
Nouveaux messages de profil
Activités générales
Membres
Membres inscrits
Visiteurs actuels
Nouveaux messages de profil
Rechercher dans les messages des profils
Teams
Créé ton équipe
Quoi de neuf ?
Nouveaux messages
Rechercher un forum
Menu
Connexion
S'inscrire
Install the app
Install
Forums
Forums généraux
Informatique
Développement
Créer un Validateur
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Vous utilisez un navigateur non à jour ou ancien. Il ne peut pas afficher ce site ou d'autres sites correctement.
Vous devez le mettre à jour ou utiliser un
navigateur alternatif
.
Répondre à la discussion
Message
<blockquote data-quote="WhiiTe'" data-source="post: 7164869" data-attributes="member: 8987"><p>Bonjour à tous,</p><p></p><p style="text-align: center">Dans ce tutoriel je vais vous apprendre à créer votre propre <span style="color: rgb(192, 57, 43)"><strong>validateur de donnée</strong></span>, de sorte à avoir un code plus <span style="color: rgb(192, 57, 43)"><strong>propre et plus organisé [FA]fa-check[/FA]</strong></span>.</p> <p style="text-align: center">Ce tutoriel a été réalisé sous <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">[FA]fa-terminal[/FA]PHP 7.0.10</span></span></span></strong>.</p><p></p><p style="text-align: center"><span style="color: rgb(192, 57, 43)">[FA]fa-question-circle fa-4x fa-border[/FA]</span></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)"><strong><span style="font-size: 18px">Qu'est ce qu'un Validateur ?</span></strong></span></p><p></p><p style="text-align: center">Un <span style="color: rgb(192, 57, 43)"><strong>[FA]fa-tasks[/FA]validateur</strong></span>, est une classe possédant plusieurs <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">fonctions </span></span></span></strong>qui vérifient la validité d'une chaîne de caractère selon des <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">[FA]fa-pencil[/FA]critères donnés</span></span></span></strong>.</p> <p style="text-align: center">Nous allons utiliser la <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">programmation orienté objet [FA]fa-code[/FA]</span></span></span></strong>.</p> <p style="text-align: center">A la fin de ce tutoriel, vous aurez un code <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">semblable </span></span></span></strong>à celui ci-dessous, et vous saurez le <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">modifier [FA]pencil-square-o[/FA] </span></span></span></strong>afin qu'il fonctionne pour <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">votre projet</span></span></span></strong>.</p><p></p><p>[PHP]$validateur = $this->validator->check("WhiiTe'")->notEmpty()->alpha()->lengthBetween(2, 25);</p><p>[/PHP]</p><p></p><p style="text-align: center">Cet exemple est tiré d'une <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">[FA]fa-cog[/FA]base MVC</span></span></span></strong> que je suis entrain de construire, j'y ai implanté un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">validateur </span></span></span></strong>qui fonctionne comme cela, pour le tutoriel, on partira d'un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">dossier vide [FA]fa-folder-o[/FA]</span></span></span></strong>.</p><p></p><p></p><p style="text-align: center"><span style="color: rgb(192, 57, 43)">[FA]fa-cogs fa-4x fa-border[/FA]</span></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)"><strong><span style="font-size: 18px">Créer le Validateur</span></strong></span></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)"></span></p><p></p><p style="text-align: center">Dans un dossier <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">vide</span></span></span></strong>, vous allez créer un fichier nommé <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">Validator.php</span></span></span></strong>, qui contiendra notre <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">classe</span></span></span></strong>, et nos <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)"><strong>[FA]fa-table[/FA]f</strong>onctions</span></span></span></strong>, puis un fichier <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">index.php</span></span></span></strong>.</p> <p style="text-align: center"></p> <p style="text-align: center">Premièrement nous allons créer la classe <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">Validator </span></span></span></strong>qui n'<strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">implémentera</span></span></span></strong> et n’<strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">étendra</span></span></span></strong> rien.</p><p></p><p>[PHP]</p><p><?php</p><p></p><p>class Validator {</p><p></p><p> private $check;</p><p> private $errors = [];</p><p></p><p>}</p><p></p><p>?></p><p></p><p>[/PHP]</p><p></p><p style="text-align: center">Les deux variables <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">privées [FA]fa-lock[/FA] </span></span></span></strong>nous serviront plus tard, la première nous permettra de <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">garder en mémoire [FA]sticky-note-o[/FA]</span></span></span></strong> la chaîne de caractère qui est testée, et la seconde contiendra <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">les erreurs [FA]fa-exclamation-triangle[/FA]</span></span></span></strong>, qu'on ajoutera au tableau au fur et à mesure que le script <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">détecte une erreur</span></span></span></strong>.</p> <p style="text-align: center"></p> <p style="text-align: center">Maintenant, dans le fichier <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">index.php</span></span></span></strong>, on va créer une <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">nouvelle instance </span></span></span></strong>de notre classe.</p><p></p><p>[PHP]</p><p><?php</p><p></p><p>require_once "Validator.php";</p><p>$validator = new Validator();</p><p></p><p>$test = $validator->check("WhiiTe")->notEmpty();</p><p></p><p>?></p><p></p><p>[/PHP]</p><p></p><p style="text-align: center">Vous remarquez que j'ai appelé deux fonctions <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">à la suite [FA]fa-arrow-right[/FA]</span></span></span></strong>, c'est la <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">syntaxe </span></span></span></strong>que nous allons donner à notre <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">validateur</span></span></span></strong>, ce ne sera qu'une simple <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">succession de fonctions </span></span></span></strong>qui s'occupent de traiter vos données plus <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">rapidement </span></span></span></strong>et sans <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">vous faire répéter [FA]fa-repeat[/FA]</span></span></span></strong> votre code.</p> <p style="text-align: center"></p> <p style="text-align: center">Premièrement dans le <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">validateur</span></span></span></strong>, nous allons créer la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">check</span></span></span></strong>, puis <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">notEmpty </span></span></span></strong>pour l'exemple.</p> <p style="text-align: center">La fonction check prendra qu'un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">paramètre</span></span></span></strong>, celui-ci sera <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">stocké </span></span></span></strong>tout au long du traitement dans la <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">variable privée errors [FA]fa-exclamation[/FA]</span></span></span></strong></p><p></p><p>[PHP]</p><p>public function check(String $check): Validator{</p><p> $this->check = $check;</p><p> return $this;</p><p>}</p><p></p><p>public function notEmpty(): Validator{</p><p> if(empty($this->check)){</p><p> $this->errors += ["Field cannot be empty."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p>[/PHP]</p><p></p><p style="text-align: center">Comme vous l'avez remarqué, je <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">retourne [FA]fa-undo[/FA] </span></span></span></strong>toujours <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">$this</span></span></span></strong>, de sorte à ce qu'on puisse <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">enchaîner les fonctions [FA]fa-repeat[/FA]</span></span></span></strong> les unes après les autres.</p> <p style="text-align: center">L'avantage est qu'une fois que la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">check </span></span></span></strong>a définie la <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">variable privée check</span></span></span></strong>, il ne nous sera plus nécessaire de <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">préciser </span></span></span></strong>en paramètre des autres <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">fonctions </span></span></span></strong>la chaîne de caractère à tester.</p> <p style="text-align: center"></p> <p style="text-align: center">A présent dans le fichier <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">index.php</span></span></span></strong>, faîtes un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">[FA]fa-camera[/FA] var_dump</span></span></span></strong> de la variable <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">test</span></span></span></strong>.</p><p></p><p>[CODE]</p><p>C:\wamp\www\Validator\index.php:8:</p><p>object(Validator)[1]</p><p> private 'check' => string 'WhiiTe' (length=6)</p><p> private 'errors' =></p><p> array (size=0)</p><p> empty</p><p>[/CODE]</p><p></p><p style="text-align: center">Notre objet <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">Validator </span></span></span></strong>contient nos deux variables, et un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">tableau vide</span></span></span></strong> pour les erreurs, normal car nous n'avons pas fait passer de chaîne <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">vide </span></span></span></strong>dans la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">check</span></span></span></strong>, si ça avait été le cas, nous aurions <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">une entrée</span></span></span></strong> avec le <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">message d'erreur du Validator</span></span></span></strong>.</p> <p style="text-align: center"></p> <p style="text-align: center">Comme ceci.</p><p></p><p>[CODE]</p><p>C:\wamp\www\Validator\index.php:8:</p><p>object(Validator)[1]</p><p> private 'check' => string '' (length=0)</p><p> private 'errors' =></p><p> array (size=1)</p><p> 0 => string 'Field cannot be empty.' (length=22)</p><p>[/CODE]</p><p></p><p style="text-align: center">La validateur est quasiment <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">terminé</span></span></span></strong>, nous allons créer deux <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">dernières </span></span></span></strong>fonctions, <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">getErrors [FA]fa-table[/FA] </span></span></span></strong>qui nous renverra le tableau des erreurs, et <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">getResponse</span></span></span></strong>, qui nous renverra un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">boolean </span></span></span></strong>selon le tableau des erreurs.</p><p></p><p>[PHP]</p><p>public function getResponse(): bool{</p><p> if(empty($this->errors)){</p><p> return true;</p><p> }</p><p></p><p> return false;</p><p>}</p><p></p><p>public function errors(): Array{</p><p> return $this->errors;</p><p>}</p><p>[/PHP]</p><p></p><p style="text-align: center">Si le tableau des erreurs est <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">vide</span></span></span></strong>, la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">getResponse </span></span></span></strong>retournera <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">true</span></span></span></strong>, on aurait pu se servir de la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">errors </span></span></span></strong>qui retourne directement le <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">tableau des erreurs</span></span></span></strong>, mais cela ajouterait du code et des <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">vérifications en plus [FA]fa-plus-square[/FA]</span></span></span></strong> qui peuvent être <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">évitées </span></span></span></strong>justement en faisant deux fonctions <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">distinctes</span></span></span></strong>.</p> <p style="text-align: center"></p> <p style="text-align: center">Et voilà, votre validateur est <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">terminé</span></span></span></strong>, vous pouvez vous en servir, peu importe <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">l'architecture </span></span></span></strong>de votre site.</p> <p style="text-align: center"></p> <p style="text-align: center">Si vous utilisez un <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">framework</span></span></span></strong>, tel que <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">Laravel</span></span></span></strong>, ou <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">CakePHP</span></span></span></strong>, sachez qu'il y a déjà un système de <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">Validation</span></span></span></strong>.</p><p></p><p style="text-align: center"><span style="color: rgb(192, 57, 43)">[FA]fa-code fa-4x fa-border[/FA]</span></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)"><strong><span style="font-size: 18px">Quelques fonctions</span></strong></span></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)"></span></p><p></p><p style="text-align: center">Comme je suis quelqu'un de <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">généreux [FA]fa-handshake-o[/FA]</span></span></span></strong>, je vous ai fait <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">quelques fonctions</span></span></span></strong> qui peuvent être plus ou moins utiles selon votre <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">projet</span></span></span></strong>.</p> <p style="text-align: center">Je ne les ai pas <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">décrites</span></span></span></strong>, le nom des fonctions parlent d'eux-mêmes, le code est <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">compréhensible </span></span></span></strong>même par un débutant, pour les utiliser, vous devrez simplement faire comme la fonction <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">notEmpty </span></span></span></strong>dans le fichier index.php, rajouter <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">à la suite [FA]fa-plus[/FA]</span></span></span></strong> les fonctions que vous voulez utiliser.</p><p></p><p>[PHP]</p><p>public function lengthBetween(Int $min, Int $max): Validator{</p><p> if($min > strlen($this->check)){</p><p> $this->errors += ["Field is too short."];</p><p> }</p><p></p><p> if($max < strlen($this->check)){</p><p> $this->errors += ["Field is too long."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function isInt(): Validator{</p><p> if(!is_int($this->check)){</p><p> $this->errors += ["Field is not a valid int."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function alpha(): Validator{</p><p> if(!ctype_alnum($this->check)){</p><p> $this->errors += ["Field is not an alphanumeric value."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function isBool(): Validator{</p><p> if(!is_bool($this->check)){</p><p> $this->errors += ["Field is not a boolean value."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function isEmail(): Validator{</p><p> if(!preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $this->check) && !filter_var($this->check, FILTER_VALIDATE_EMAIL)){</p><p> $this->errors += ["Field is not a valid email."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function isUrl(): Validator{</p><p> if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $this->check)){</p><p> $this->errors += ["Field is not a valid url."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p></p><p>public function isEqual(String $compare): Validator{</p><p> if($this->check !== $compare){</p><p> $this->errors += ["Fields not match."];</p><p> }</p><p></p><p> return $this;</p><p>}</p><p>[/PHP]</p><p></p><p style="text-align: center">Vous pouvez à tout moment <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">rajouter des fonctions,</span></span></span></strong> tout dépend de ce que vous cherchez à <strong><span style="font-family: 'Open Sans'"><span style="font-size: 15px"><span style="color: rgb(192, 57, 43)">faire</span></span></span></strong>.</p> <p style="text-align: center"></p> <p style="text-align: center"><span style="color: rgb(192, 57, 43)">[FA]fa-user fa-4x fa-border[/FA]</span></p> <p style="text-align: center"><span style="color: #c0392b"><span style="font-size: 18px"><strong>Fin du tutoriel</strong></span></span></p><p></p><p>Merci d'avoir lu, si vous avez une <strong><span style="color: rgb(192, 57, 43)">question [FA]fa-question-circle[/FA]</span></strong>, une <strong><span style="color: rgb(192, 57, 43)">suggestion [FA]fa-user-plus[/FA] </span></strong> ou un <strong><span style="color: rgb(192, 57, 43)">problème [FA]fa-exclamation-circle[/FA] </span></strong> n'hésitez pas à me solliciter dans les commentaires.</p><p></p><p>Bon développement,</p><p><strong><span style="color: #FC7F3C">[FA]fa-star[/FA]W.</span></strong></p></blockquote><p></p>
[QUOTE="WhiiTe', post: 7164869, member: 8987"] Bonjour à tous, [CENTER]Dans ce tutoriel je vais vous apprendre à créer votre propre [COLOR=rgb(192, 57, 43)][B]validateur de donnée[/B][/COLOR], de sorte à avoir un code plus [COLOR=rgb(192, 57, 43)][B]propre et plus organisé [FA]fa-check[/FA][/B][/COLOR]. Ce tutoriel a été réalisé sous [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)][FA]fa-terminal[/FA]PHP 7.0.10[/COLOR][/SIZE][/FONT][/B].[/CENTER] [CENTER][COLOR=rgb(192, 57, 43)][FA]fa-question-circle fa-4x fa-border[/FA] [B][SIZE=5]Qu'est ce qu'un Validateur ?[/SIZE][/B][/COLOR][/CENTER] [CENTER]Un [COLOR=rgb(192, 57, 43)][B][FA]fa-tasks[/FA]validateur[/B][/COLOR], est une classe possédant plusieurs [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]fonctions [/COLOR][/SIZE][/FONT][/B]qui vérifient la validité d'une chaîne de caractère selon des [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)][FA]fa-pencil[/FA]critères donnés[/COLOR][/SIZE][/FONT][/B]. Nous allons utiliser la [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]programmation orienté objet [FA]fa-code[/FA][/COLOR][/SIZE][/FONT][/B]. A la fin de ce tutoriel, vous aurez un code [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]semblable [/COLOR][/SIZE][/FONT][/B]à celui ci-dessous, et vous saurez le [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]modifier [FA]pencil-square-o[/FA] [/COLOR][/SIZE][/FONT][/B]afin qu'il fonctionne pour [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]votre projet[/COLOR][/SIZE][/FONT][/B].[/CENTER] [PHP]$validateur = $this->validator->check("WhiiTe'")->notEmpty()->alpha()->lengthBetween(2, 25); [/PHP] [CENTER]Cet exemple est tiré d'une [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)][FA]fa-cog[/FA]base MVC[/COLOR][/SIZE][/FONT][/B] que je suis entrain de construire, j'y ai implanté un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]validateur [/COLOR][/SIZE][/FONT][/B]qui fonctionne comme cela, pour le tutoriel, on partira d'un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]dossier vide [FA]fa-folder-o[/FA][/COLOR][/SIZE][/FONT][/B].[/CENTER] [CENTER][COLOR=rgb(192, 57, 43)][FA]fa-cogs fa-4x fa-border[/FA] [B][SIZE=5]Créer le Validateur[/SIZE][/B] [/COLOR][/CENTER] [CENTER]Dans un dossier [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]vide[/COLOR][/SIZE][/FONT][/B], vous allez créer un fichier nommé [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]Validator.php[/COLOR][/SIZE][/FONT][/B], qui contiendra notre [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]classe[/COLOR][/SIZE][/FONT][/B], et nos [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)][B][FA]fa-table[/FA]f[/B]onctions[/COLOR][/SIZE][/FONT][/B], puis un fichier [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]index.php[/COLOR][/SIZE][/FONT][/B]. Premièrement nous allons créer la classe [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]Validator [/COLOR][/SIZE][/FONT][/B]qui n'[B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]implémentera[/COLOR][/SIZE][/FONT][/B] et n’[B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]étendra[/COLOR][/SIZE][/FONT][/B] rien.[/CENTER] [PHP] <?php class Validator { private $check; private $errors = []; } ?> [/PHP] [CENTER]Les deux variables [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]privées [FA]fa-lock[/FA] [/COLOR][/SIZE][/FONT][/B]nous serviront plus tard, la première nous permettra de [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]garder en mémoire [FA]sticky-note-o[/FA][/COLOR][/SIZE][/FONT][/B] la chaîne de caractère qui est testée, et la seconde contiendra [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]les erreurs [FA]fa-exclamation-triangle[/FA][/COLOR][/SIZE][/FONT][/B], qu'on ajoutera au tableau au fur et à mesure que le script [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]détecte une erreur[/COLOR][/SIZE][/FONT][/B]. Maintenant, dans le fichier [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]index.php[/COLOR][/SIZE][/FONT][/B], on va créer une [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]nouvelle instance [/COLOR][/SIZE][/FONT][/B]de notre classe.[/CENTER] [PHP] <?php require_once "Validator.php"; $validator = new Validator(); $test = $validator->check("WhiiTe")->notEmpty(); ?> [/PHP] [CENTER]Vous remarquez que j'ai appelé deux fonctions [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]à la suite [FA]fa-arrow-right[/FA][/COLOR][/SIZE][/FONT][/B], c'est la [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]syntaxe [/COLOR][/SIZE][/FONT][/B]que nous allons donner à notre [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]validateur[/COLOR][/SIZE][/FONT][/B], ce ne sera qu'une simple [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]succession de fonctions [/COLOR][/SIZE][/FONT][/B]qui s'occupent de traiter vos données plus [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]rapidement [/COLOR][/SIZE][/FONT][/B]et sans [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]vous faire répéter [FA]fa-repeat[/FA][/COLOR][/SIZE][/FONT][/B] votre code. Premièrement dans le [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]validateur[/COLOR][/SIZE][/FONT][/B], nous allons créer la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]check[/COLOR][/SIZE][/FONT][/B], puis [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]notEmpty [/COLOR][/SIZE][/FONT][/B]pour l'exemple. La fonction check prendra qu'un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]paramètre[/COLOR][/SIZE][/FONT][/B], celui-ci sera [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]stocké [/COLOR][/SIZE][/FONT][/B]tout au long du traitement dans la [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]variable privée errors [FA]fa-exclamation[/FA][/COLOR][/SIZE][/FONT][/B][/CENTER] [PHP] public function check(String $check): Validator{ $this->check = $check; return $this; } public function notEmpty(): Validator{ if(empty($this->check)){ $this->errors += ["Field cannot be empty."]; } return $this; } [/PHP] [CENTER]Comme vous l'avez remarqué, je [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]retourne [FA]fa-undo[/FA] [/COLOR][/SIZE][/FONT][/B]toujours [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]$this[/COLOR][/SIZE][/FONT][/B], de sorte à ce qu'on puisse [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]enchaîner les fonctions [FA]fa-repeat[/FA][/COLOR][/SIZE][/FONT][/B] les unes après les autres. L'avantage est qu'une fois que la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]check [/COLOR][/SIZE][/FONT][/B]a définie la [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]variable privée check[/COLOR][/SIZE][/FONT][/B], il ne nous sera plus nécessaire de [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]préciser [/COLOR][/SIZE][/FONT][/B]en paramètre des autres [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]fonctions [/COLOR][/SIZE][/FONT][/B]la chaîne de caractère à tester. A présent dans le fichier [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]index.php[/COLOR][/SIZE][/FONT][/B], faîtes un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)][FA]fa-camera[/FA] var_dump[/COLOR][/SIZE][/FONT][/B] de la variable [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]test[/COLOR][/SIZE][/FONT][/B].[/CENTER] [CODE] C:\wamp\www\Validator\index.php:8: object(Validator)[1] private 'check' => string 'WhiiTe' (length=6) private 'errors' => array (size=0) empty [/CODE] [CENTER]Notre objet [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]Validator [/COLOR][/SIZE][/FONT][/B]contient nos deux variables, et un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]tableau vide[/COLOR][/SIZE][/FONT][/B] pour les erreurs, normal car nous n'avons pas fait passer de chaîne [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]vide [/COLOR][/SIZE][/FONT][/B]dans la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]check[/COLOR][/SIZE][/FONT][/B], si ça avait été le cas, nous aurions [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]une entrée[/COLOR][/SIZE][/FONT][/B] avec le [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]message d'erreur du Validator[/COLOR][/SIZE][/FONT][/B]. Comme ceci.[/CENTER] [CODE] C:\wamp\www\Validator\index.php:8: object(Validator)[1] private 'check' => string '' (length=0) private 'errors' => array (size=1) 0 => string 'Field cannot be empty.' (length=22) [/CODE] [CENTER]La validateur est quasiment [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]terminé[/COLOR][/SIZE][/FONT][/B], nous allons créer deux [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]dernières [/COLOR][/SIZE][/FONT][/B]fonctions, [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]getErrors [FA]fa-table[/FA] [/COLOR][/SIZE][/FONT][/B]qui nous renverra le tableau des erreurs, et [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]getResponse[/COLOR][/SIZE][/FONT][/B], qui nous renverra un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]boolean [/COLOR][/SIZE][/FONT][/B]selon le tableau des erreurs.[/CENTER] [PHP] public function getResponse(): bool{ if(empty($this->errors)){ return true; } return false; } public function errors(): Array{ return $this->errors; } [/PHP] [CENTER]Si le tableau des erreurs est [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]vide[/COLOR][/SIZE][/FONT][/B], la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]getResponse [/COLOR][/SIZE][/FONT][/B]retournera [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]true[/COLOR][/SIZE][/FONT][/B], on aurait pu se servir de la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]errors [/COLOR][/SIZE][/FONT][/B]qui retourne directement le [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]tableau des erreurs[/COLOR][/SIZE][/FONT][/B], mais cela ajouterait du code et des [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]vérifications en plus [FA]fa-plus-square[/FA][/COLOR][/SIZE][/FONT][/B] qui peuvent être [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]évitées [/COLOR][/SIZE][/FONT][/B]justement en faisant deux fonctions [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]distinctes[/COLOR][/SIZE][/FONT][/B]. Et voilà, votre validateur est [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]terminé[/COLOR][/SIZE][/FONT][/B], vous pouvez vous en servir, peu importe [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]l'architecture [/COLOR][/SIZE][/FONT][/B]de votre site. Si vous utilisez un [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]framework[/COLOR][/SIZE][/FONT][/B], tel que [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]Laravel[/COLOR][/SIZE][/FONT][/B], ou [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]CakePHP[/COLOR][/SIZE][/FONT][/B], sachez qu'il y a déjà un système de [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]Validation[/COLOR][/SIZE][/FONT][/B].[/CENTER] [CENTER][COLOR=rgb(192, 57, 43)][FA]fa-code fa-4x fa-border[/FA] [B][SIZE=5]Quelques fonctions[/SIZE][/B] [/COLOR][/CENTER] [CENTER]Comme je suis quelqu'un de [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]généreux [FA]fa-handshake-o[/FA][/COLOR][/SIZE][/FONT][/B], je vous ai fait [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]quelques fonctions[/COLOR][/SIZE][/FONT][/B] qui peuvent être plus ou moins utiles selon votre [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]projet[/COLOR][/SIZE][/FONT][/B]. Je ne les ai pas [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]décrites[/COLOR][/SIZE][/FONT][/B], le nom des fonctions parlent d'eux-mêmes, le code est [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]compréhensible [/COLOR][/SIZE][/FONT][/B]même par un débutant, pour les utiliser, vous devrez simplement faire comme la fonction [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]notEmpty [/COLOR][/SIZE][/FONT][/B]dans le fichier index.php, rajouter [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]à la suite [FA]fa-plus[/FA][/COLOR][/SIZE][/FONT][/B] les fonctions que vous voulez utiliser.[/CENTER] [PHP] public function lengthBetween(Int $min, Int $max): Validator{ if($min > strlen($this->check)){ $this->errors += ["Field is too short."]; } if($max < strlen($this->check)){ $this->errors += ["Field is too long."]; } return $this; } public function isInt(): Validator{ if(!is_int($this->check)){ $this->errors += ["Field is not a valid int."]; } return $this; } public function alpha(): Validator{ if(!ctype_alnum($this->check)){ $this->errors += ["Field is not an alphanumeric value."]; } return $this; } public function isBool(): Validator{ if(!is_bool($this->check)){ $this->errors += ["Field is not a boolean value."]; } return $this; } public function isEmail(): Validator{ if(!preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $this->check) && !filter_var($this->check, FILTER_VALIDATE_EMAIL)){ $this->errors += ["Field is not a valid email."]; } return $this; } public function isUrl(): Validator{ if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $this->check)){ $this->errors += ["Field is not a valid url."]; } return $this; } public function isEqual(String $compare): Validator{ if($this->check !== $compare){ $this->errors += ["Fields not match."]; } return $this; } [/PHP] [CENTER]Vous pouvez à tout moment [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]rajouter des fonctions,[/COLOR][/SIZE][/FONT][/B] tout dépend de ce que vous cherchez à [B][FONT=Open Sans][SIZE=4][COLOR=rgb(192, 57, 43)]faire[/COLOR][/SIZE][/FONT][/B]. [COLOR=rgb(192, 57, 43)][FA]fa-user fa-4x fa-border[/FA][/COLOR] [COLOR=#c0392b][SIZE=5][B]Fin du tutoriel[/B][/SIZE][/COLOR][/CENTER] Merci d'avoir lu, si vous avez une [B][COLOR=rgb(192, 57, 43)]question [FA]fa-question-circle[/FA][/COLOR][/B], une [B][COLOR=rgb(192, 57, 43)]suggestion [FA]fa-user-plus[/FA] [/COLOR][/B] ou un [B][COLOR=rgb(192, 57, 43)]problème [FA]fa-exclamation-circle[/FA] [/COLOR][/B] n'hésitez pas à me solliciter dans les commentaires. Bon développement, [B][COLOR=#FC7F3C][FA]fa-star[/FA]W.[/COLOR][/B] [/QUOTE]
Insérer les citations…
Vérification
Publier la réponse
Forums
Forums généraux
Informatique
Développement
Créer un Validateur
Ce site utilise des cookies. En continuant à utiliser ce site, vous acceptez l'utilisation des cookies.
Acceptez
En savoir plus.…
Haut