Skip to content
Snippets Groups Projects
Commit 3e52c39e authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #84 from greezybacon/issue/form-issues


Corrects various form designer issues

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents c137d876 3abada96
No related branches found
No related tags found
No related merge requests found
...@@ -266,6 +266,20 @@ class DynamicFormField extends VerySimpleModel { ...@@ -266,6 +266,20 @@ class DynamicFormField extends VerySimpleModel {
return $this->get('edit_mask') & 8; 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() { function delete() {
// Don't really delete form fields as that will screw up the data // Don't really delete form fields as that will screw up the data
// model. Instead, just drop the association with the form which // model. Instead, just drop the association with the form which
......
...@@ -183,6 +183,12 @@ class FormField { ...@@ -183,6 +183,12 @@ class FormField {
function errors() { function errors() {
return $this->_errors; return $this->_errors;
} }
function addError($message, $field=false) {
if ($field)
$this->_errors[$field] = $message;
else
$this->_errors[] = $message;
}
function isValidEntry() { function isValidEntry() {
$this->validateEntry(); $this->validateEntry();
......
...@@ -59,7 +59,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); ...@@ -59,7 +59,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
<th>Type</th> <th>Type</th>
<th>Internal</th> <th>Internal</th>
<th>Required</th> <th>Required</th>
<th>Name</th> <th>Variable</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
</thead> </thead>
...@@ -95,12 +95,12 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); ...@@ -95,12 +95,12 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
<th>Type</th> <th>Type</th>
<th>Internal</th> <th>Internal</th>
<th>Required</th> <th>Required</th>
<th>Name</th> <th>Variable</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
</thead> </thead>
<tbody class="sortable-rows" data-sort="sort-"> <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'); $id = $f->get('id');
$deletable = !$f->isDeletable() ? 'disabled="disabled"' : ''; $deletable = !$f->isDeletable() ? 'disabled="disabled"' : '';
$force_name = $f->isNameForced() ? 'disabled="disabled"' : ''; $force_name = $f->isNameForced() ? 'disabled="disabled"' : '';
......
...@@ -18,8 +18,8 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); ...@@ -18,8 +18,8 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
</thead> </thead>
<tbody> <tbody>
<?php <?php
$uf = UserForm::objects(); $uf = UserForm::getInstance();
$uf[0]->render(); $uf->render();
if($cfg->notifyONNewStaffTicket()) { ?> if($cfg->notifyONNewStaffTicket()) { ?>
<tr> <tr>
<td width="160">Alert:</td> <td width="160">Alert:</td>
......
...@@ -40,9 +40,14 @@ if($_POST) { ...@@ -40,9 +40,14 @@ if($_POST) {
} }
if ($field->isValid()) if ($field->isValid())
$field->save(); $field->save();
else
# notrans (not shown)
$errors["field-$id"] = 'Field has validation errors';
// Keep track of the last sort number // Keep track of the last sort number
$max_sort = max($max_sort, $field->get('sort')); $max_sort = max($max_sort, $field->get('sort'));
} }
if ($errors)
$errors['err'] = 'Unable to commit form. Check validation errors';
break; break;
case 'add': case 'add':
$form = DynamicForm::create(array( $form = DynamicForm::create(array(
...@@ -94,7 +99,8 @@ if($_POST) { ...@@ -94,7 +99,8 @@ if($_POST) {
$field->save(); $field->save();
} }
// XXX: Move to an instrumented list that can handle this better // XXX: Move to an instrumented list that can handle this better
$form->_dfields = $form->_fields = null; if (!$errors)
$form->_dfields = $form->_fields = null;
} }
} }
......
...@@ -115,8 +115,8 @@ $(document).ready(function(){ ...@@ -115,8 +115,8 @@ $(document).ready(function(){
}); });
} }
$("form#save :input").change(function() { var warnOnLeave = function (el) {
var fObj = $(this).closest('form'); var fObj = el.closest('form');
if(!fObj.data('changed')){ if(!fObj.data('changed')){
fObj.data('changed', true); fObj.data('changed', true);
$('input[type=submit]', fObj).css('color', 'red'); $('input[type=submit]', fObj).css('color', 'red');
...@@ -124,6 +124,10 @@ $(document).ready(function(){ ...@@ -124,6 +124,10 @@ $(document).ready(function(){
return 'Are you sure you want to leave? Any changes or info you\'ve entered will be discarded!'; return 'Are you sure you want to leave? Any changes or info you\'ve entered will be discarded!';
}); });
} }
};
$("form#save :input").change(function() {
warnOnLeave($(this));
}); });
$("form#save :input[type=reset]").click(function() { $("form#save :input[type=reset]").click(function() {
...@@ -453,6 +457,7 @@ $(document).ready(function(){ ...@@ -453,6 +457,7 @@ $(document).ready(function(){
'helper': fixHelper, 'helper': fixHelper,
'stop': function(e, ui) { 'stop': function(e, ui) {
var attr = ui.item.parent('tbody').data('sort'); var attr = ui.item.parent('tbody').data('sort');
warnOnLeave(ui.item);
$('input[name^='+attr+']', ui.item.parent('tbody')).each(function(i, el) { $('input[name^='+attr+']', ui.item.parent('tbody')).each(function(i, el) {
$(el).val(i+1); $(el).val(i+1);
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment