Index: src/include/lib/Garradin/Form.php ================================================================== --- src/include/lib/Garradin/Form.php +++ src/include/lib/Garradin/Form.php @@ -40,18 +40,28 @@ public function addError($msg) { $this->errors[] = $msg; } - public function getErrorMessages() + public function getErrorMessages($membre = false) { $errors = []; + + if ($membre) + { + $champs = Config::getInstance()->get('champs_membres'); + } foreach ($this->errors as $error) { if (is_array($error)) { + if ($membre) + { + $error['name'] = $champs->get($error['name'], 'title'); + } + $errors[] = $this->getErrorMessage($error['rule'], $error['name'], $error['params']); } else { $errors[] = $error; @@ -78,10 +88,14 @@ return sprintf('Le champ %s est vide.', $element); case 'min': return sprintf('Le champ %s doit faire au moins %d caractères.', $element, $params[0]); case 'file': return sprintf('Le fichier envoyé n\'est pas valide.'); + case 'confirmed': + return sprintf('La vérification du champ %s n\'est pas identique au champ lui-même.', $element); + case 'date_format': + return sprintf('Format de date invalide dans le champ %s.', $element); default: return sprintf('Erreur "%s" dans le champ "%s"', $rule, $element); } } Index: src/include/lib/Garradin/Membres.php ================================================================== --- src/include/lib/Garradin/Membres.php +++ src/include/lib/Garradin/Membres.php @@ -75,29 +75,13 @@ } } if (isset($data[$key])) { - if ($config->type == 'email' && trim($data[$key]) !== '' && !filter_var($data[$key], FILTER_VALIDATE_EMAIL)) - { - throw new UserException('Adresse e-mail invalide dans le champ "' . $config->title . '".'); - } - elseif ($config->type == 'url' && trim($data[$key]) !== '' && !filter_var($data[$key], FILTER_VALIDATE_URL)) - { - throw new UserException('Adresse URL invalide dans le champ "' . $config->title . '".'); - } - elseif ($config->type == 'date' && trim($data[$key]) !== '' && !Utils::checkDate($data[$key])) - { - throw new UserException('Date invalide "' . $config->title . '", format attendu : AAAA-MM-JJ.'); - } - elseif ($config->type == 'datetime' && trim($data[$key]) !== '') - { - if (!Utils::checkDateTime($data[$key]) || !($dt = new DateTime($data[$key]))) - { - throw new UserException('Date invalide "' . $config->title . '", format attendu : AAAA-MM-JJ HH:mm.'); - } - + if ($config->type == 'datetime' && trim($data[$key]) !== '') + { + $dt = new DateTime($data[$key]); $data[$key] = $dt->format('Y-m-d H:i'); } elseif ($config->type == 'tel') { $data[$key] = Utils::normalizePhoneNumber($data[$key]); @@ -114,13 +98,10 @@ { if (empty($data[$key])) { $data[$key] = 0; } - - if (!is_numeric($data[$key])) - throw new UserException('Le champ "' . $config->title . '" doit contenir un chiffre.'); } elseif ($config->type == 'select' && !in_array($data[$key], $config->options)) { throw new UserException('Le champ "' . $config->title . '" ne correspond pas à un des choix proposés.'); } @@ -151,23 +132,10 @@ $data[$key] = null; } } } - if (isset($data['code_postal']) && trim($data['code_postal']) != '') - { - if (!empty($data['pays']) && $data['pays'] == 'FR' && !preg_match('!^\d{5}$!', $data['code_postal'])) - { - throw new UserException('Code postal invalide.'); - } - } - - if (!empty($data['passe']) && strlen($data['passe']) < 5) - { - throw new UserException('Le mot de passe doit faire au moins 5 caractères.'); - } - return true; } public function add($data = []) { Index: src/include/lib/Garradin/Membres/Champs.php ================================================================== --- src/include/lib/Garradin/Membres/Champs.php +++ src/include/lib/Garradin/Membres/Champs.php @@ -44,11 +44,12 @@ 'help', 'editable', 'list_row', 'mandatory', 'private', - 'options' + 'options', + 'rules' ]; static protected $presets = null; public function __toString() @@ -196,10 +197,79 @@ } return $key; } } + + public function getValidationRules($mode = 'edit') + { + assert(in_array($mode, ['edit', 'create', 'user_edit'])); + + $all_rules = []; + + foreach ($this->champs as $name => $config) + { + if (empty($config->editable) && $mode == 'user_edit') + { + $all_rules[$name] = 'absent'; + break; + } + + $rules = []; + + if (!empty($config->mandatory) && !($name == 'passe' && $mode != 'create')) + { + $rules[] = 'required'; + } + + if ($config->type == 'email') + { + $rules[] = 'email'; + } + elseif ($config->type == 'url') + { + $rules[] = 'url'; + } + elseif ($config->type == 'date') + { + $rules[] = 'date_format:Y-m-d'; + } + elseif ($config->type == 'date') + { + $rules[] = 'date_format:Y-m-d H\:i'; + } + elseif ($config->type == 'number' || $config->type == 'multiple') + { + $rules[] = 'numeric'; + } + elseif ($config->type == 'select') + { + $rules[] = 'in:' . range(0, count($this->options) - 1); + } + elseif ($config->type == 'checkbox') + { + $rules[] = 'boolean'; + } + + if ($name == 'passe') + { + $rules[] = 'min:6'; + } + + if (isset($config->rules)) + { + $rules[] = $config->rules; + } + + if (count($rules)) + { + $all_rules[$name] = implode('|', $rules); + } + } + + return $all_rules; + } /** * Vérifie la cohérence et la présence des bons éléments pour un champ * @param string $name Nom du champ * @param array $config Configuration du champ Index: src/include/lib/Garradin/Template.php ================================================================== --- src/include/lib/Garradin/Template.php +++ src/include/lib/Garradin/Template.php @@ -42,11 +42,11 @@ if (!$form->hasErrors()) { return ''; } - return '
- {$error} -
-{/if} - -