LSattr_html: add $singleValue class variable to handle this common case

Also unify the usage of the class variable $LSformElement_type.
This commit is contained in:
Benjamin Renard 2021-08-18 12:28:56 +02:00
parent 99f5cc3728
commit 2fa20316bc
4 changed files with 30 additions and 52 deletions

View file

@ -29,11 +29,24 @@ LSsession :: loadLSclass('LSlog_staticLoggerClass');
*/ */
class LSattr_html extends LSlog_staticLoggerClass { class LSattr_html extends LSlog_staticLoggerClass {
// The attribute name
var $name; var $name;
// The attribute configuration
var $config; var $config;
// The reference of the parent LSattribute object
var $attribute; var $attribute;
// The corresponding LSformElement object type
var $LSformElement_type = false; var $LSformElement_type = false;
// If true, this LSattr_html type is considered as able to only support
// the first and unique value of the attribute. If more than one value
// is passed to this LSattr_html type, an LSattr_html_03 error will be
// triggered.
protected $singleValue = false;
/** /**
* Constructeur * Constructeur
* *
@ -94,8 +107,16 @@ class LSattr_html extends LSlog_staticLoggerClass {
LSerror :: addErrorCode('LSform_06',$this -> name); LSerror :: addErrorCode('LSform_06',$this -> name);
return; return;
} }
if (!is_null($data)) if (!is_null($data)) {
$element -> setValue($data); $data = ensureIsArray($data);
if ($this -> singleValue) {
if (count($data) > 1)
LSerror :: addErrorCode('LSattr_html_03', get_class($this));
$element -> setValue($data[0]);
}
else
$element -> setValue($data);
}
return $element; return $element;
} }
@ -144,5 +165,5 @@ ___("LSattr_html : The method addToForm() of the HTML type of the attribute %{at
); );
// 02 : not yet used // 02 : not yet used
LSerror :: defineError('LSattr_html_03', LSerror :: defineError('LSattr_html_03',
___("LSattr_html_%{type} : Multiple data are not supported for this field type.") ___("%{type} : Multiple data are not supported for this field type.")
); );

View file

@ -27,29 +27,7 @@
*/ */
class LSattr_html_image extends LSattr_html { class LSattr_html_image extends LSattr_html {
/** var $LSformElement_type = 'image';
* Ajoute l'attribut au formualaire passer en paramètre protected $singleValue = true;
*
* @param[in] &$form LSform Le formulaire
* @param[in] $idForm L'identifiant du formulaire
* @param[in] $data Valeur du champs du formulaire
*
* @retval LSformElement L'element du formulaire ajouté
*/
function addToForm (&$form, $idForm, $data=NULL) {
$element=$form -> addElement('image', $this -> name, $this -> getLabel(), $this -> config, $this);
if(!$element) {
LSerror :: addErrorCode('LSform_06',$this -> name);
return;
}
if ($data && count($data) > 1) {
LSerror :: addErrorCode('LSattr_html_03','password');
}
if ($data)
$element -> setValue(ensureIsArray($data)[0]);
return $element;
}
} }

View file

@ -27,29 +27,7 @@
*/ */
class LSattr_html_password extends LSattr_html { class LSattr_html_password extends LSattr_html {
/** var $LSformElement_type = 'password';
* Ajoute l'attribut au formualaire passer en paramètre protected $singleValue = true;
*
* @param[in] &$form LSform Le formulaire
* @param[in] $idForm L'identifiant du formulaire
* @param[in] $data Valeur du champs du formulaire
*
* @retval LSformElement L'element du formulaire ajouté
*/
public function addToForm (&$form,$idForm,$data=NULL) {
$element = $form -> addElement('password', $this -> name, $this -> getLabel(), $this -> config, $this);
if(!$element) {
LSerror :: addErrorCode('LSform_06', $this -> name);
return;
}
if ($data) {
$data = ensureIsArray($data);
if (count($data) > 1)
LSerror :: addErrorCode('LSattr_html_03', 'password');
$element -> setValue($data[0]);
}
return $element;
}
} }

View file

@ -27,6 +27,7 @@
*/ */
class LSattr_html_select_object extends LSattr_html{ class LSattr_html_select_object extends LSattr_html{
var $LSformElement_type = 'select_object';
var $unrecognizedValues=false; var $unrecognizedValues=false;
/** /**
@ -40,7 +41,7 @@ class LSattr_html_select_object extends LSattr_html{
*/ */
public function addToForm (&$form,$idForm,$data=NULL) { public function addToForm (&$form,$idForm,$data=NULL) {
$this -> config['attrObject'] = $this; $this -> config['attrObject'] = $this;
$element=$form -> addElement('select_object', $this -> name, $this -> getLabel(), $this -> config, $this); $element=$form -> addElement($this -> LSformElement_type, $this -> name, $this -> getLabel(), $this -> config, $this);
if(!$element) { if(!$element) {
LSerror :: addErrorCode('LSform_06',$this -> name); LSerror :: addErrorCode('LSform_06',$this -> name);
return; return;