From 3abada967ee253fe23974e8dd9240564fae49e70 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Mon, 4 Nov 2013 19:26:41 +0000 Subject: [PATCH] Required 'name' if 'required' is set If a form field is required, then a name will be necessary in order to plumb up the API. --- include/class.dynamic_forms.php | 14 ++++++++++++++ include/class.forms.php | 6 ++++++ include/staff/dynamic-form.inc.php | 2 +- scp/forms.php | 8 +++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 7771802bc..846202885 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -266,6 +266,20 @@ class DynamicFormField extends VerySimpleModel { return $this->get('edit_mask') & 8; } + /** + * Used when updating the form via the admin panel. This represents + * validation on the form field template, not data entered into a form + * field of a custom form. The latter would be isValidEntry() + */ + function isValid() { + if (count($this->errors()) || !parent::isValid()) + return false; + if ($this->get('required') && !$this->get('name')) + $this->addError( + "Variable name is required for required fields", "name"); + return count($this->errors()) == 0; + } + function delete() { // Don't really delete form fields as that will screw up the data // model. Instead, just drop the association with the form which diff --git a/include/class.forms.php b/include/class.forms.php index 8bfe89fc6..97c27e922 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -183,6 +183,12 @@ class FormField { function errors() { return $this->_errors; } + function addError($message, $field=false) { + if ($field) + $this->_errors[$field] = $message; + else + $this->_errors[] = $message; + } function isValidEntry() { $this->validateEntry(); diff --git a/include/staff/dynamic-form.inc.php b/include/staff/dynamic-form.inc.php index 1685d6cac..b7b9e875f 100644 --- a/include/staff/dynamic-form.inc.php +++ b/include/staff/dynamic-form.inc.php @@ -100,7 +100,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); </tr> </thead> <tbody class="sortable-rows" data-sort="sort-"> - <?php if ($form) foreach ($form->getFields() as $f) { + <?php if ($form) foreach ($form->getDynamicFields() as $f) { $id = $f->get('id'); $deletable = !$f->isDeletable() ? 'disabled="disabled"' : ''; $force_name = $f->isNameForced() ? 'disabled="disabled"' : ''; diff --git a/scp/forms.php b/scp/forms.php index 5c0a2ad04..311207756 100644 --- a/scp/forms.php +++ b/scp/forms.php @@ -40,9 +40,14 @@ if($_POST) { } if ($field->isValid()) $field->save(); + else + # notrans (not shown) + $errors["field-$id"] = 'Field has validation errors'; // Keep track of the last sort number $max_sort = max($max_sort, $field->get('sort')); } + if ($errors) + $errors['err'] = 'Unable to commit form. Check validation errors'; break; case 'add': $form = DynamicForm::create(array( @@ -94,7 +99,8 @@ if($_POST) { $field->save(); } // XXX: Move to an instrumented list that can handle this better - $form->_dfields = $form->_fields = null; + if (!$errors) + $form->_dfields = $form->_fields = null; } } -- GitLab