Skip to content
Snippets Groups Projects
Commit 3abada96 authored by Jared Hancock's avatar Jared Hancock
Browse files

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.
parent 8e509f00
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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();
......
......@@ -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"' : '';
......
......@@ -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;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment