Validators for Form Fields
This commit is contained in:
parent
7043d56329
commit
6bb19ad6b0
3
tasks/oop/classes/.htaccess
Normal file
3
tasks/oop/classes/.htaccess
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Order deny,allow
|
||||||
|
Deny from all
|
||||||
|
Allow from 127.0.0.1
|
||||||
112
tasks/oop/classes/fieldValidator/fieldValidator.php
Normal file
112
tasks/oop/classes/fieldValidator/fieldValidator.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
61
tasks/oop/classes/formValidator/formValidator.php
Normal file
61
tasks/oop/classes/formValidator/formValidator.php
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
38
tasks/oop/fieldValidator/index.php
Normal file
38
tasks/oop/fieldValidator/index.php
Normal 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>
|
||||||
46
tasks/oop/formValidator/index.php
Normal file
46
tasks/oop/formValidator/index.php
Normal 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>
|
||||||
@ -14,7 +14,7 @@
|
|||||||
Headers::html();
|
Headers::html();
|
||||||
|
|
||||||
//Testdatei für die Klasse Hyperlink
|
//Testdatei für die Klasse Hyperlink
|
||||||
include_once "./hyperlink.php";
|
include_once "../classes/hyperlink/hyperlink.php";
|
||||||
|
|
||||||
//Objektinstanzen
|
//Objektinstanzen
|
||||||
$link1 = new Hyperlink("https://www.google.de", "Google");
|
$link1 = new Hyperlink("https://www.google.de", "Google");
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
include_once "../../../base/headers.php";
|
include_once "../../../base/headers.php";
|
||||||
Headers::html();
|
Headers::html();
|
||||||
|
|
||||||
include_once "./person.php";
|
include_once "../classes/person/person.php";
|
||||||
$person = new Person("John Doe", 170, new DateTime("01-01-1970"));
|
$person = new Person("John Doe", 170, new DateTime("01-01-1970"));
|
||||||
|
|
||||||
echo $person;
|
echo $person;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user