From d9dea033a9e19c68f4bbc25cc5e8a6ea5e88d225 Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 18:13:48 +0100 Subject: [PATCH] New class DomainInputFormField Create a input that validates a domain name. --- evolibs/Form.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/evolibs/Form.php b/evolibs/Form.php index 6748031..e398ccb 100644 --- a/evolibs/Form.php +++ b/evolibs/Form.php @@ -403,7 +403,53 @@ class TextInputFormField extends FormField { $out = ''; $out .= "

\n"; $out .= $this->getLabelHTML(); - $out .= $this->getInputHTML(); + $out .= $this->getInputHTML(); + $out .= $this->getErrorHTML(); + $out .= "

\n\n"; + return $out; + } +} + +class DomainInputFormField extends FormField { + protected $mandatory = NULL; + protected $textsize = NULL; + + public function __construct($label, $mandatory=TRUE) { + parent::__construct($label); + $this->mandatory = $mandatory; + $this->textsize = $textsize; + } + + public function verify($set_error) { + if($this->mandatory && (!strlen($this->value))) { + if($set_error) $this->error = 'Champ obligatoire'; + return FALSE; + } + + if (!preg_match("/^[a-z0-9-.]+\.[a-z]{2,}$/i", $this->value)) { + if($set_error) $this->error = 'Ceci n\'est pas un nom de domaine'; + return FALSE; + } + + return TRUE; + } + + public function getInputHTML() { + $input = ''; + $input .= 'name.'" value="'.htmlspecialchars($this->value,ENT_QUOTES).'"'; + $input .= ' maxlength="'.$this->textsize[1].'" size="'.$this->textsize[0].'" '; + if($this->read_only) { $input .= 'readonly="readonly="'; } + if($this->disabled) { $input .= 'disabled="disabled="'; } + $input .= '/>'; + return $input; + } + + public function __toString() { + $out = ''; + $out .= "

\n"; + $out .= $this->getLabelHTML(); + $out .= $this->getInputHTML(); $out .= $this->getErrorHTML(); $out .= "

\n\n"; return $out;