patternphpMinor
HTML Form validation with PHP
Viewed 0 times
withphpvalidationformhtml
Problem
I'm kinda new to PHP and I'm trying to make a script to validate a HTML form only with PHP (I know there are options to do so with JS, but I guess it's better to have a "cover" with a server-side script).
Project folder structure:
HTML form code, which is located in the project folder:
classes/ini.php
classes/DB.php
`class DB
{
private $driver = 'mysql';
private $host = '127.0.0.1';
private $user = 'root';
private $pass = '';
private $name = 'project';
public $db;
public function __construct()
{
try
{
$this->db = new PDO("$this->driver:host=$this->host;dbname=$this->name;charset=utf8", $this->user, $this->pass);
//Error reporting. Throw exceptions
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Use native prepared statements
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//Don't use persistent connections across multiple server sessions
$this->db->setAttribute(PDO::ATTR_PERSISTENT, false);
//Set default fetch mode
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//Return the connection object
return $this->db;
} catch (PDOException $e)
{
#echo 'Sorry. We have some problemes. Try again later!';
Project folder structure:
project/
classes/ #contains classes to validate and process the HTML form
DB.php #contains the connection to the MySQL server
Rules.php #contains all rules to validate the form
Validate.php #contains methods to validate each field
Register.php #is the registration class
ini.php #contains the spl_autoload_register() function
index.php #contains the HTML form
HTML form code, which is located in the project folder:
register:
*
classes/ini.php
error_reporting(E_ALL);
ini_set('display_errors','On');
spl_autoload_register(function($class){
require_once $class.'.php';
});
classes/DB.php
`class DB
{
private $driver = 'mysql';
private $host = '127.0.0.1';
private $user = 'root';
private $pass = '';
private $name = 'project';
public $db;
public function __construct()
{
try
{
$this->db = new PDO("$this->driver:host=$this->host;dbname=$this->name;charset=utf8", $this->user, $this->pass);
//Error reporting. Throw exceptions
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Use native prepared statements
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//Don't use persistent connections across multiple server sessions
$this->db->setAttribute(PDO::ATTR_PERSISTENT, false);
//Set default fetch mode
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//Return the connection object
return $this->db;
} catch (PDOException $e)
{
#echo 'Sorry. We have some problemes. Try again later!';
Solution
Use boolean expressions directly
like this one:
You can return boolean expressions directly:
Don't repeat yourself
All the rules
Instead of writing
it would be simpler to
so that you don't have to repeatedly write
Don't return two types of values
The
an array of errors, or
This is poor design.
Return one type of value.
If there are no errors, return an empty array.
That way the return type will be consistently an array,
which is a good thing.
The code snippet above will become simpler too:
Rules.php is full of ternaries that return true or false,like this one:
public static function minlen($str, $value)
{
return mb_strlen(trim($str)) < $value ? true : false;
}You can return boolean expressions directly:
return mb_strlen(trim($str)) < $value;Don't repeat yourself
All the rules
trim the input.Instead of writing
trim in all of them,it would be simpler to
trim once before using the rule methods,so that you don't have to repeatedly write
trim so many times in every single method.Don't return two types of values
The
validateName may return two kinds of values:an array of errors, or
true (boolean).if (isset($errors['name'])) {
return $errors['name'];
} else {
return true;
}This is poor design.
Return one type of value.
If there are no errors, return an empty array.
That way the return type will be consistently an array,
which is a good thing.
The code snippet above will become simpler too:
return $errors['name'];Code Snippets
public static function minlen($str, $value)
{
return mb_strlen(trim($str)) < $value ? true : false;
}return mb_strlen(trim($str)) < $value;if (isset($errors['name'])) {
return $errors['name'];
} else {
return true;
}return $errors['name'];Context
StackExchange Code Review Q#95879, answer score: 7
Revisions (0)
No revisions yet.