Validators for Form Fields

This commit is contained in:
Kilian Hofmann 2024-07-05 13:39:02 +02:00
parent 7043d56329
commit 6bb19ad6b0
9 changed files with 262 additions and 2 deletions

View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

View File

@ -0,0 +1,112 @@
<?php
class FieldValidator
{
/*
* Private members
*/
private string $name;
private string $value;
private string $type;
private bool $required;
private int $minLen;
private int $maxLen;
private string $error;
/*
* Constructor
*/
public function __construct(string $name, string $value, string $type, bool $required = false, int $minLen = -1, int $maxLen = -1, string $error = "")
{
$this->name = $name;
$this->value = $value;
$this->type = $type;
$this->required = $required;
$this->minLen = $minLen;
$this->maxLen = $maxLen;
$this->error = $error;
return $this;
}
/*
* Fluent Style
*/
public static function create(string $name, string $value, string $type)
{
return new FieldValidator($name, $value, $type);
}
public function isRequired()
{
$this->required = true;
return $this;
}
public function withMinLen(int $minLen)
{
$this->minLen = $minLen;
return $this;
}
public function withMaxLen(int $maxLen)
{
$this->maxLen = $maxLen;
return $this;
}
/*
* Getters and Setters
*/
public function getError()
{
return $this->error;
}
public function getName()
{
return $this->name;
}
/*
* Methods
*/
public function isValid()
{
if ($this->required && empty($this->value)) {
$this->error = "$this->name is required";
return false;
}
if ($this->minLen >= 0 && !empty($this->value) && strlen($this->value) < $this->minLen) {
$this->error = "$this->name requires $this->minLen characters, got " . strlen($this->value);
return false;
}
if ($this->maxLen >= 0 && !empty($this->value) && strlen($this->value) > $this->maxLen) {
$this->error = "$this->name allows $this->maxLen characters, got " . strlen($this->value);
return false;
}
switch ($this->type) {
case "email":
if (!filter_var($this->value, FILTER_VALIDATE_EMAIL)) {
$this->error = "Not a valid E-Mail";
return false;
}
break;
case "number":
if (!filter_var($this->value, FILTER_VALIDATE_FLOAT) || !filter_var($this->value, FILTER_VALIDATE_INT)) {
$this->error = "Not a valid number";
return false;
}
break;
}
return true;
}
}

View File

@ -0,0 +1,61 @@
<?php
class FormValidator
{
/*
* Private members
*/
private array $fields;
/*
* Constructor
*/
public function __construct(FieldValidator ...$fields)
{
$this->fields = $fields;
}
/*
* Fluent Style
*/
public static function create(FieldValidator ...$fields)
{
return new FormValidator(...$fields);
}
/*
* Methods
*/
public function isValid()
{
foreach ($this->fields as $field) {
if (!$field->isValid) return false;
}
return true;
}
public function getErrors()
{
$errors = [];
foreach ($this->fields as $field) {
if (!$field->isValid()) array_push($errors, $field->getError());
}
return $errors;
}
public function getField(string $name)
{
$field = current(array_filter($this->fields, fn ($field) => strcmp($field->getName(), $name) === 0));
if ($field !== false) {
return $field;
}
return "No field named $name";
}
}

View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once "../../../base/meta.php" ?>
<title>FieldValidator Test</title>
</head>
<body>
<?php
include_once "../../../base/settings.php";
include_once "../../../base/headers.php";
Headers::html();
//Testdatei für die Klasse FieldValidator
include_once "../classes/fieldValidator/fieldValidator.php";
$validators = [
FieldValidator::create("Empty Valid", "", "string"),
FieldValidator::create("Empty Invalid", "", "string")->isRequired(),
FieldValidator::create("MinLen Valid", "ssss", "string")->withMinLen(4),
FieldValidator::create("MinLen Invalid", "sss", "string")->withMinLen(4),
FieldValidator::create("MaxLen Valid", "ssss", "string")->withMaxLen(4),
FieldValidator::create("MaxLen Invalid", "sssss", "string")->withMaxLen(4),
FieldValidator::create("E-Mail Valid", "khofmann@fu-berlin.de", "email"),
FieldValidator::create("E-Mail Invalid", "khofmann@fu-berlin de", "email"),
];
foreach ($validators as $validator) {
echo "<b>" . $validator->getName() . "</b>: ";
echo $validator->isValid() ? "<em>True</em>" : "<em>False</em>" . " with " . $validator->getError();
echo "<br />";
}
?>
</body>
</html>

View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once "../../../base/meta.php" ?>
<title>FormValidator Test</title>
</head>
<body>
<?php
include_once "../../../base/settings.php";
include_once "../../../base/headers.php";
Headers::html();
//Testdatei für die Klasse FormValidator
include_once "../classes/fieldValidator/fieldValidator.php";
include_once "../classes/formValidator/formValidator.php";
$form = FormValidator::create(
FieldValidator::create("Empty Valid", "", "string"),
FieldValidator::create("Empty Invalid", "", "string")->isRequired(),
FieldValidator::create("MinLen Valid", "ssss", "string")->withMinLen(4),
FieldValidator::create("MinLen Invalid", "sss", "string")->withMinLen(4),
FieldValidator::create("MaxLen Valid", "ssss", "string")->withMaxLen(4),
FieldValidator::create("MaxLen Invalid", "sssss", "string")->withMaxLen(4),
FieldValidator::create("E-Mail Valid", "khofmann@fu-berlin.de", "email"),
FieldValidator::create("E-Mail Invalid", "khofmann@fu-berlin de", "email"),
);
foreach ($form->getErrors() as $error) {
echo "$error<br />";
}
?>
<br />
<pre>
<?php
print_r($form->getField("E-Mail Valid"));
echo "\n\n";
print_r($form->getField("Name"));
?>
</pre>
</body>
</html>

View File

@ -14,7 +14,7 @@
Headers::html();
//Testdatei für die Klasse Hyperlink
include_once "./hyperlink.php";
include_once "../classes/hyperlink/hyperlink.php";
//Objektinstanzen
$link1 = new Hyperlink("https://www.google.de", "Google");

View File

@ -13,7 +13,7 @@
include_once "../../../base/headers.php";
Headers::html();
include_once "./person.php";
include_once "../classes/person/person.php";
$person = new Person("John Doe", 170, new DateTime("01-01-1970"));
echo $person;