form : add the verify of forms. Actually check only the mandatory part
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2705 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
44
form.php
44
form.php
@@ -118,20 +118,6 @@ class form
|
||||
return $values;
|
||||
}
|
||||
|
||||
/** Return TRUE if the value associated to a field is correct. Return an
|
||||
array with a severity and a message to explain why a field is not
|
||||
correct.
|
||||
Fields can be an array with just one element, then only this element is
|
||||
checked
|
||||
@param array $fieldsVerify The fields to verify
|
||||
@param array|null $valuesVerify The values of the fields to verify
|
||||
@deprecated 0.17
|
||||
*/
|
||||
public function verify (&$fieldsVerify, $valuesVerify = NULL)
|
||||
{
|
||||
die ("FORM/VERIFY : UNUSED and deprecated\n");
|
||||
}
|
||||
|
||||
/** Return the fields in HTML code. If $values is provided, use it in place
|
||||
of default values. In case of select boxes, $values are the selected
|
||||
elements
|
||||
@@ -222,6 +208,8 @@ class form
|
||||
$res .= "<script>document.getElementById('".$this->formName."_".
|
||||
$focusElement."').focus();</script>\n";
|
||||
$res .= "</form>\n";
|
||||
if (isset ($_SESSION))
|
||||
$_SESSION["domframework"]["form"]["fields"] = $this->fields;
|
||||
return $res;
|
||||
}
|
||||
|
||||
@@ -234,8 +222,32 @@ class form
|
||||
$csrf->checkToken ($tokenFromUser);
|
||||
}
|
||||
|
||||
/** Check if the parameters are correct with the defined fields
|
||||
* Need the session !
|
||||
* @return array containing the errors
|
||||
*/
|
||||
public function verify ($values, $fields=array ())
|
||||
{
|
||||
if (count ($fields) === 0)
|
||||
{
|
||||
if (! isset ($_SESSION["domframework"]["form"]["fields"]))
|
||||
return array ();
|
||||
$fields = $_SESSION["domframework"]["form"]["fields"];
|
||||
}
|
||||
$errors = array ();
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
if ($field->mandatory !== null &&
|
||||
(! array_key_exists ($field->name, $values) ||
|
||||
trim ($values[$field->name]) === ""))
|
||||
$errors[$field->name] = _("Field mandatory and not provided");
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/** Save the values and errors to be displayed in the next page if the session
|
||||
* is available
|
||||
* Need the session to work
|
||||
* @param array $values The values of the fields filled by the user
|
||||
* @param array|null $errors The errors detected by a verify
|
||||
*/
|
||||
@@ -248,7 +260,9 @@ class form
|
||||
}
|
||||
}
|
||||
|
||||
/** Reset the saved values to provide a clean form next page */
|
||||
/** Reset the saved values to provide a clean form next page
|
||||
* Need the session to work
|
||||
*/
|
||||
public function saveValuesErrorsReset ()
|
||||
{
|
||||
unset ($_SESSION["domframework"]["form"]["values"]);
|
||||
|
||||
Reference in New Issue
Block a user