From 9c6aa2dee794888a13da48e2619c830526d495b1 Mon Sep 17 00:00:00 2001 From: Dany wm DB Date: Sat, 10 May 2025 23:25:57 +0200 Subject: [PATCH] HTML_INPUT improve and test --- .gitignore | 1 + include/lib/html_input.class.php | 20 ++++++- include/lib/idate.class.php | 27 +++------- include/lib/ihidden.class.php | 5 +- include/lib/itext.class.php | 36 ++++++++++--- scenario/HtmlInput.test.php | 89 +++++++++++++++++++++++++++++++- 6 files changed, 145 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index b5318cf3b..202d931f6 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ local/* .idea/* include/tfpdf/font/unifont/* html/ads.html +/nbproject/private/ diff --git a/include/lib/html_input.class.php b/include/lib/html_input.class.php index 03c4f3aee..50f357700 100755 --- a/include/lib/html_input.class.php +++ b/include/lib/html_input.class.php @@ -76,6 +76,7 @@ class HtmlInput var $id; var $style; var $css_size; + var $placeholder; /*< $placeholder string in the INPUT Text */ function __construct($p_name="", $p_value="", $p_id="") { @@ -100,9 +101,26 @@ class HtmlInput { $this->readOnly=$p_read; } + /** + * @return string HTML placeholder attribut + */ + public function get_placeholder() + { + return $this->placeholder; + } + + /** + * @brief set HTML placeholder attribut + * @param string $placeholder + */ + public function set_placeholder($placeholder) + { + $this->placeholder = $placeholder; + return $this; + } /*! - * \brief set the extra javascript property for the INPUT field + * \brief add an HTML attribute for the INPUT field * \param $p_name name of the parameter * \param $p_value default value of this parameter */ diff --git a/include/lib/idate.class.php b/include/lib/idate.class.php index 1a131b16f..407513066 100644 --- a/include/lib/idate.class.php +++ b/include/lib/idate.class.php @@ -46,25 +46,9 @@ class IDate extends HtmlInput $this->extra=""; $this->style=' class="input_text" '; $this->autofocus=false; + $this->javascript='onchange="format_date(this)"'; } - /** - * @return string - */ - public function get_placeholder() - { - return $this->placeholder; - return $this; - } - - /** - * @param string $placeholder - */ - public function set_placeholder($placeholder) - { - $this->placeholder = $placeholder; - return $this; - } - + /** * @return string */ @@ -131,7 +115,8 @@ class IDate extends HtmlInput return $this->display(); if ($this->id=="") $this->id=self::generate_id($this->name); $autofocus=($this->autofocus)?" autofocus ":""; - $onchange='onchange="format_date(this)"'; + $strAttribute=$this->get_node_attribute(); + $r=sprintf(' ',$this->name,$this->id,$this->value,$this->placeholder,$this->title, - $onchange, + $this->javascript, + $strAttribute, $this->id ); // @see calendar-setup.js diff --git a/include/lib/ihidden.class.php b/include/lib/ihidden.class.php index ae2f07271..c2d474e62 100644 --- a/include/lib/ihidden.class.php +++ b/include/lib/ihidden.class.php @@ -35,8 +35,9 @@ class IHidden extends HtmlInput $this->name=($p_name==null)?$this->name:$p_name; $this->value=($p_value==null)?$this->value:$p_value; $this->id=($this->id=="")?$this->name:$this->id; - - $r=''; + $strAttribute=$this->get_node_attribute(); + + $r=''; return $r; } diff --git a/include/lib/itext.class.php b/include/lib/itext.class.php index 1168cb16c..8118710c7 100644 --- a/include/lib/itext.class.php +++ b/include/lib/itext.class.php @@ -28,11 +28,11 @@ */ class IText extends HtmlInput { - var $placeholder; var $title; var $autofocus; var $css_size; - + var $pattern; /*!< $pattern HTML pattern */ + var $maxlength; /*!< HTML maxlength */ function __construct($name='',$value='',$p_id="") { parent::__construct($name,$value,$p_id); @@ -43,6 +43,8 @@ class IText extends HtmlInput $this->autofocus=false; $this->require=false; $this->css_size=""; + $this->pattern=""; + $this->maxlength=""; } /*!\brief show the html input of the widget*/ public function input($p_name=null,$p_value=null) @@ -60,11 +62,24 @@ class IText extends HtmlInput $t= 'title="'.$this->title.'" '; $autofocus=($this->autofocus)?" autofocus ":""; $require=($this->require)?"required":""; + + // var $pattern regex to match the INPUT TEXT + $pattern=""; + if ($this->pattern != "") { + $pattern= sprintf( 'pattern="%s"',$this->pattern); + } + + // var maxlength HTML attribute + $maxlength = ""; + if ($this->maxlength !="" ) { + $maxlength=sprintf(' maxlength="%s" ',$this->maxlength); + } + if ( ! isset ($this->css_size) || empty ($this->css_size)) { $r= sprintf(' + Size="%s" %s %s %s %s %s %s %s > ',$this->style, $this->id, $this->name, @@ -76,11 +91,13 @@ class IText extends HtmlInput $this->extra, $autofocus, $require, - $strAttribute + $strAttribute, + $pattern, + $maxlength ); } else { $r= sprintf(' + style="width:%s;" %s %s %s %s %s %s %s> ',$this->style, $this->id, $this->name, @@ -92,7 +109,9 @@ class IText extends HtmlInput $this->extra, $autofocus, $require, - $strAttribute + $strAttribute, + $pattern, + $maxlength ); } @@ -108,6 +127,7 @@ class IText extends HtmlInput $t= ((isset($this->title)))?'title="'.$this->title.'" ':' '; $extra=(isset($this->extra))?$this->extra:""; + $strAttribute=$this->get_node_attribute(); $readonly=" readonly "; $this->value=htmlentities($this->value??"", ENT_COMPAT|ENT_QUOTES, "UTF-8"); @@ -117,12 +137,12 @@ class IText extends HtmlInput $r='style.' TYPE="TEXT" id="'. $this->id.'"'.$t. 'NAME="'.$this->name.'" VALUE="'.$this->value.'" '. - 'SIZE="'.$this->size.'" '.$this->javascript." $readonly $this->extra >"; + 'SIZE="'.$this->size.'" '.$this->javascript." $readonly $this->extra $strAttribute>"; } else { $r='style.' TYPE="TEXT" id="'. $this->id.'"'.$t. 'NAME="'.$this->name.'" VALUE="'.$this->value.'" '. - ' style="width:'.$this->css_size.'" '.$this->javascript." $readonly $this->extra >"; + ' style="width:'.$this->css_size.'" '.$this->javascript." $readonly $this->extra $strAttribute>"; } /* add tag for column if inside a table */ diff --git a/scenario/HtmlInput.test.php b/scenario/HtmlInput.test.php index 0a0c1e02f..80a313f00 100644 --- a/scenario/HtmlInput.test.php +++ b/scenario/HtmlInput.test.php @@ -175,7 +175,92 @@ $http=new HttpInput(); echo $icard->input(); echo $icard->search(); ?> -
+

itext

+

HTML Attribute

+
+    $itext=new IText("itext_name");
+    $itext->set_attribute("key1",1);
+    $itext->set_attribute("data1","-data-");
+    $itext->set_attribute("logx","data:logx");
+    echo $itext->input();
+     echo $itext->display();
+
+style=' class="input_text" '; + $itext->set_attribute("key1",1); + $itext->set_attribute("data1","-data-"); + $itext->set_attribute("logx","data:logx"); + echo $itext->input(); + echo $itext->display(); +?> +
+    input());?>
+    display());?>
+
+

Maxlenght = 15

+maxlength=15; + $itext->style=' class="input_text" '; - \ No newline at end of file + echo $itext->input(); + echo $itext->display(); + ?> +
+    input());?>
+    display());?>
+
+

placeholder

+style=' class="input_text" '; + $itext->placeholder="Donnez une information"; + echo $itext->input(); + echo $itext->display(); + ?> +
+    $itext->placeholder="Donnez une information";
+    input());?>
+    display());?>
+
+

require

+style=' class="input_text" '; + $itext->require=true; + echo $itext->input(); + echo $itext->display(); + ?> +
+ $itext->require=true;
+    input());?>
+    display());?>
+
+ +

pattern : uniquement des chiffres

+
+ +style=' class="input_text" '; + $itext->pattern="[0-9]+"; + $itext->maxlength="4"; + $itext->placeholder="9999"; + $itext->title="code"; + echo $itext->input(); + echo $itext->display(); + echo HtmlInput::submit("valider","valider"); + ?> +
+    $itext->pattern="[0-9]+";
+    input());?>
+    display());?>
+
+
+

IDATE

+input(); + echo htmlspecialchars($date->input()); + $date->id= uniqid(); + $date->javascript=sprintf('onchange = "console.debug(\'change date\');%s"',"format_date(this);"); + echo $date->input(); + ?>