diff options
| author | Michaela Brückner <brueckner@data-quest.de> | 2024-01-02 08:55:04 +0000 |
|---|---|---|
| committer | Michaela Brückner <brueckner@data-quest.de> | 2024-01-02 08:55:04 +0000 |
| commit | 502e6a104f200acc2f8ff81ba09257a6035406e5 (patch) | |
| tree | 6c10c6662c822475c92814ec32d60951eda9cad8 /lib/classes/forms | |
| parent | b6bdba58a8e090cb144bae382457c2e439d8a72a (diff) | |
new registry formular, re #1559
Closes #3533
Merge request studip/studip!1141
Diffstat (limited to 'lib/classes/forms')
| -rw-r--r-- | lib/classes/forms/ConfirmInput.php | 18 | ||||
| -rw-r--r-- | lib/classes/forms/DatalistInput.php | 21 | ||||
| -rw-r--r-- | lib/classes/forms/Form.php | 71 | ||||
| -rw-r--r-- | lib/classes/forms/Input.php | 30 | ||||
| -rw-r--r-- | lib/classes/forms/Part.php | 4 | ||||
| -rw-r--r-- | lib/classes/forms/PasswordInput.php | 18 | ||||
| -rw-r--r-- | lib/classes/forms/RadioInput.php | 22 | ||||
| -rw-r--r-- | lib/classes/forms/RangeInput.php | 3 |
8 files changed, 181 insertions, 6 deletions
diff --git a/lib/classes/forms/ConfirmInput.php b/lib/classes/forms/ConfirmInput.php new file mode 100644 index 0000000..13e3634 --- /dev/null +++ b/lib/classes/forms/ConfirmInput.php @@ -0,0 +1,18 @@ +<?php + +namespace Studip\Forms; + +class ConfirmInput extends Input +{ + public function render() + { + $template = $GLOBALS['template_factory']->open('forms/confirm_password_input'); + $template->title = $this->title; + $template->name = $this->name; + $template->value = $this->value; + $template->id = md5(uniqid()); + $template->required = $this->required; + $template->attributes = arrayToHtmlAttributes($this->attributes); + return $template->render(); + } +} diff --git a/lib/classes/forms/DatalistInput.php b/lib/classes/forms/DatalistInput.php new file mode 100644 index 0000000..06cc9e0 --- /dev/null +++ b/lib/classes/forms/DatalistInput.php @@ -0,0 +1,21 @@ +<?php + +namespace Studip\Forms; + +class DatalistInput extends Input +{ + public function render() + { + $options = $this->extractOptionsFromAttributes($this->attributes); + + $template = $GLOBALS['template_factory']->open('forms/datalist_input'); + $template->title = $this->title; + $template->name = $this->name; + $template->value = $this->value; + $template->id = md5(uniqid()); + $template->required = $this->required; + $template->attributes = arrayToHtmlAttributes($this->attributes); + $template->options = $options; + return $template->render(); + } +} diff --git a/lib/classes/forms/Form.php b/lib/classes/forms/Form.php index 49713b7..3b29552 100644 --- a/lib/classes/forms/Form.php +++ b/lib/classes/forms/Form.php @@ -17,6 +17,8 @@ class Form extends Part protected $save_button_text = ''; protected $save_button_name = ''; + protected $cancel_button_text = ''; + protected $cancel_button_name = ''; protected $autoStore = false; protected $debugmode = false; protected $success_message = ''; @@ -207,6 +209,31 @@ class Form extends Part return $this->save_button_name ?: $this->getSaveButtonText(); } + public function setCancelButtonText(string $text): Form + { + $this->cancel_button_text = $text; + return $this; + } + + /** + * @return string The text for the "save" button in the form. + */ + public function getCancelButtonText() : string + { + return $this->cancel_button_text ?: _('Abbrechen'); + } + + public function setCancelButtonName(string $name): Form + { + $this->cancel_button_name = $name; + return $this; + } + + public function getCancelButtonName() : string + { + return $this->cancel_button_name ?: $this->getCancelButtonText(); + } + public function setSuccessMessage(string $success_message): Form { $this->success_message = $success_message; @@ -250,12 +277,35 @@ class Form extends Part { $this->autoStore = true; if (\Request::isPost() && \Request::isAjax() && !\Request::isDialog()) { - $this->store(); - if ($this->success_message) { - \PageLayout::postSuccess($this->success_message); + if (\Request::submitted('STUDIPFORM_SERVERVALIDATION')) { + //verify the user input: + $output = []; + foreach ($this->getAllInputs() as $input) { + if ($input->validate) { + $callback = $input->getValidationCallback(); + $value = $this->getStorableValueFromRequest($input); + $valid = $callback($value, $input); + if ($valid !== true) { + $output[$input->getName()] = [ + 'name' => $input->getName(), + 'label' => $input->getTitle(), + 'error' => $callback($value, $input) + ]; + } + } + } + echo json_encode($output); + page_close(); + die(); + } else { + //storing the input + $this->store(); + if ($this->success_message) { + \PageLayout::postSuccess($this->success_message); + } + page_close(); + die(); } - page_close(); - die(); } return $this; } @@ -325,6 +375,17 @@ class Form extends Part $stored = 0; + foreach ($this->getAllInputs() as $input) { + if ($input->validate) { + $callback = $input->getValidationCallback(); + $value = $this->getStorableValueFromRequest($input); + $valid = $callback($value, $input); + if ($valid !== true) { + return $stored; + } + } + } + //store by each input $all_values = []; foreach ($this->getAllInputs() as $input) { diff --git a/lib/classes/forms/Input.php b/lib/classes/forms/Input.php index aa3069b..9d2ad32 100644 --- a/lib/classes/forms/Input.php +++ b/lib/classes/forms/Input.php @@ -11,6 +11,7 @@ abstract class Input protected $parent = null; public $mapper = null; public $store = null; + public $validate = null; public $if = null; public $permission = true; public $required = false; @@ -133,6 +134,21 @@ abstract class Input return $this->name; } + public function getTitle() + { + return $this->title; + } + + public function hasValidation() + { + return $this->validate !== null; + } + + public function getValidationCallback() + { + return $this->validate; + } + /** * Returns the value of this input. * @return null @@ -216,6 +232,18 @@ abstract class Input } /** + * Sets the server-side verify function of this input. The callable returns true if the given value is okay, or + * false or a textstring representing the error. + * @param callable $verify + * @return $this + */ + public function setValidationFunction(Callable $validate) + { + $this->validate = $validate; + return $this; + } + + /** * Sets a condition to display this input. The condition is a javascript condition which is used by vue to * hide the input if the condition is not satisfies. * @param string $if @@ -261,7 +289,7 @@ abstract class Input protected function extractOptionsFromAttributes(array &$attributes) { - $options = null; + $options = []; if (isset($attributes['options'])) { $options = $attributes['options']; unset($attributes['options']); diff --git a/lib/classes/forms/Part.php b/lib/classes/forms/Part.php index 3609eb4..fdca8f5 100644 --- a/lib/classes/forms/Part.php +++ b/lib/classes/forms/Part.php @@ -235,6 +235,7 @@ abstract class Part $attributes['type'], $attributes['mapper'], $attributes['store'], + $attributes['validate'], $attributes['if'], $attributes['permission'], $attributes['required'], @@ -257,6 +258,9 @@ abstract class Part if (isset($data['store']) && is_callable($data['store'])) { $input->store = $data['store']; } + if (isset($data['validate']) && is_callable($data['validate'])) { + $input->validate = $data['validate']; + } if (!empty($data['if'])) { $input->if = $data['if']; } diff --git a/lib/classes/forms/PasswordInput.php b/lib/classes/forms/PasswordInput.php new file mode 100644 index 0000000..68e4718 --- /dev/null +++ b/lib/classes/forms/PasswordInput.php @@ -0,0 +1,18 @@ +<?php + +namespace Studip\Forms; + +class PasswordInput extends Input +{ + public function render() + { + $template = $GLOBALS['template_factory']->open('forms/password_input'); + $template->title = $this->title; + $template->name = $this->name; + $template->value = $this->value; + $template->id = md5(uniqid()); + $template->required = $this->required; + $template->attributes = arrayToHtmlAttributes($this->attributes); + return $template->render(); + } +} diff --git a/lib/classes/forms/RadioInput.php b/lib/classes/forms/RadioInput.php new file mode 100644 index 0000000..1945d87 --- /dev/null +++ b/lib/classes/forms/RadioInput.php @@ -0,0 +1,22 @@ +<?php + +namespace Studip\Forms; + +class RadioInput extends Input +{ + public function render() + { + $options = $this->extractOptionsFromAttributes($this->attributes); + $template = $GLOBALS['template_factory']->open('forms/radio_input'); + $template->title = $this->title; + $template->name = $this->name; + $template->value = $this->value; + $template->id = md5(uniqid()); + $template->required = $this->required; + $template->options = $options; + $template->attributes = arrayToHtmlAttributes($this->attributes); + $template->orientation = $this->attributes['orientation']; + return $template->render(); + + } +} diff --git a/lib/classes/forms/RangeInput.php b/lib/classes/forms/RangeInput.php index 9f99d59..7a69e9e 100644 --- a/lib/classes/forms/RangeInput.php +++ b/lib/classes/forms/RangeInput.php @@ -11,6 +11,9 @@ class RangeInput extends Input $template->name = $this->name; $template->value = $this->value; $template->id = md5(uniqid()); + $template->min = $this->attributes['min']; + $template->max = $this->attributes['max']; + $template->step = $this->attributes['step']; $template->required = $this->required; $template->attributes = arrayToHtmlAttributes($this->attributes); return $template->render(); |
