...
 
Commits (2)
......@@ -418,46 +418,25 @@ class msgPool
}
/*!
* \brief Display error about invalid characters
* \brief Return error about invalid value
*
* \param string $name The field name
*
* \param string $data The submited data
*
* \param string $regex
*
* \param string $example Example of a right submited data
*/
public static function invalid ($name, $data = "", $regex = "", $example = "")
public static function invalid (string $name, string $data = '', string $example = ''): string
{
/* Stylize example */
if ($example != "") {
$example = "<br><br><i>"._("Example:")."</i> ".$example;
}
/* If validChars are posted, take data and paint all invalid
characters... */
if ($regex) {
$result = "";
$mismatch = "";
mb_internal_encoding('UTF-8');
for ($i = 0; $i <= mb_strlen($data); $i++) {
$currentChar = mb_substr($data, $i, 1);
if (preg_match("$regex", $currentChar)) {
$result .= $currentChar;
} else {
$result .= '<span style="color:red;text-decoration:underline;">'.($currentChar).'</span>';
$mismatch .= $currentChar;
}
}
$error = sprintf(_('The field "%s" contains an invalid value.'), htmlentities($name, ENT_COMPAT, 'UTF-8'));
$error .= '<br/><br/> "'.htmlentities($data, ENT_COMPAT, 'UTF-8').'"';
return sprintf(_("The field '%s' contains invalid characters"), $name).". ".
((strlen($mismatch) == 1) ? sprintf(_("'%s' is not allowed:"), $mismatch) : sprintf(_("'%s' are not allowed!"), $mismatch)).
"<br><br> \"$result\"$example";
} else {
return sprintf(_("The field '%s' contains invalid characters"), $name)."!$example";
/* Stylize example */
if ($example !== '') {
$error .= '<br/><br/><i>'.sprintf(_('Example: %s'), htmlentities($example, ENT_COMPAT, 'UTF-8')).'</i> ';
}
return $error;
}
/*!
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2016 FusionDirectory
Copyright (C) 2012-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -44,14 +44,14 @@ class IntAttribute extends Attribute
parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
$this->min = ($min === FALSE ? FALSE : $this->inputValue($min));
$this->max = ($max === FALSE ? FALSE : $this->inputValue($max));
$this->example = "";
$this->example = '';
if (($min !== FALSE) && ($max !== FALSE)) {
$this->example = sprintf(_("An integer between %d and %d"), $min, $max);
$this->example = sprintf(_('An integer between %d and %d'), $min, $max);
} elseif ($min !== FALSE) {
$this->example = sprintf(_("An integer larger than %d"), $min);
$this->example = sprintf(_('An integer larger than %d'), $min);
} elseif ($max !== FALSE) {
$this->example = sprintf(_("An integer smaller than %d"), $max);
$this->example = sprintf(_('An integer smaller than %d'), $max);
}
}
......@@ -80,11 +80,11 @@ class IntAttribute extends Attribute
return $error;
} elseif ($this->value !== '') {
if (!is_numeric($this->value)) {
return msgPool::invalid($this->getLabel(), $this->value, "/./", $this->example);
return msgPool::invalid($this->getLabel(), $this->value, $this->example);
}
if ((($this->min !== FALSE) && ($this->value < $this->min))
|| (($this->max !== FALSE) && ($this->value > $this->max))) {
return msgPool::invalid($this->getLabel(), $this->value, "/./", $this->example);
return msgPool::invalid($this->getLabel(), $this->value, $this->example);
}
}
}
......@@ -157,13 +157,13 @@ class FloatAttribute extends IntAttribute
$this->step = 0.01;
$this->example = "";
$this->example = '';
if (($min !== FALSE) && ($max !== FALSE)) {
$this->example = sprintf(_("A float between %f and %f"), $min, $max);
$this->example = sprintf(_('A float between %f and %f'), $min, $max);
} elseif ($min !== FALSE) {
$this->example = sprintf(_("A float larger than %f"), $min);
$this->example = sprintf(_('A float larger than %f'), $min);
} elseif ($max !== FALSE) {
$this->example = sprintf(_("A float smaller than %f"), $max);
$this->example = sprintf(_('A float smaller than %f'), $max);
}
}
......
......@@ -127,8 +127,8 @@ class StringAttribute extends Attribute
function validate ()
{
if (($this->pattern !== "") && !preg_match($this->pattern, $this->value)) {
return msgPool::invalid($this->getLabel(), $this->value, $this->pattern, htmlentities($this->example));
if (($this->pattern !== '') && !preg_match($this->pattern, $this->value)) {
return msgPool::invalid($this->getLabel(), $this->value, $this->example);
}
}
......
......@@ -30,7 +30,7 @@ class TestValidateAttribute extends StringAttribute
{
$func = $this->testFunc;
if (!tests::$func($this->value)) {
return msgPool::invalid($this->getLabel(), $this->value);
return msgPool::invalid($this->getLabel(), $this->value, $this->example);
}
}
}
......