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

forms: Don't save on validation errors

parent 95c56a89
No related branches found
No related tags found
No related merge requests found
...@@ -4,21 +4,24 @@ $info=array(); ...@@ -4,21 +4,24 @@ $info=array();
if($form && $_REQUEST['a']!='add') { if($form && $_REQUEST['a']!='add') {
$title = 'Update custom form section'; $title = 'Update custom form section';
$action = 'update'; $action = 'update';
$url = "?id=".urlencode($_REQUEST['id']);
$submit_text='Save Changes'; $submit_text='Save Changes';
$info = $form->ht; $info = $form->ht;
$newcount=2; $newcount=2;
} else { } else {
$title = 'Add new custom form section'; $title = 'Add new custom form section';
$action = 'add'; $action = 'add';
$url = '?a=add';
$submit_text='Add Form'; $submit_text='Add Form';
$newcount=4; $newcount=4;
} }
$info=Format::htmlchars(($errors && $_POST)?$_POST:$info); $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
?> ?>
<form action="?id=<?php echo urlencode($_REQUEST['id']); ?>" method="post" id="save"> <form action="<?php echo $url ?>" method="post" id="save">
<?php csrf_token(); ?> <?php csrf_token(); ?>
<input type="hidden" name="do" value="<?php echo $action; ?>"> <input type="hidden" name="do" value="<?php echo $action; ?>">
<input type="hidden" name="a" value="<?php echo $action; ?>">
<input type="hidden" name="id" value="<?php echo $info['id']; ?>"> <input type="hidden" name="id" value="<?php echo $info['id']; ?>">
<h2>Custom Form</h2> <h2>Custom Form</h2>
<table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2"> <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
......
...@@ -10,6 +10,7 @@ if($_POST) { ...@@ -10,6 +10,7 @@ if($_POST) {
$fields = array('title', 'notes', 'instructions'); $fields = array('title', 'notes', 'instructions');
$required = array('title'); $required = array('title');
$max_sort = 0; $max_sort = 0;
$form_fields = array();
switch(strtolower($_POST['do'])) { switch(strtolower($_POST['do'])) {
case 'update': case 'update':
foreach ($fields as $f) foreach ($fields as $f)
...@@ -48,7 +49,7 @@ if($_POST) { ...@@ -48,7 +49,7 @@ if($_POST) {
if ($field->get('name')) if ($field->get('name'))
$names[] = $field->get('name'); $names[] = $field->get('name');
if ($field->isValid()) if ($field->isValid())
$field->save(); $form_fields[] = $field;
else else
# notrans (not shown) # notrans (not shown)
$errors["field-$id"] = 'Field has validation errors'; $errors["field-$id"] = 'Field has validation errors';
...@@ -57,11 +58,14 @@ if($_POST) { ...@@ -57,11 +58,14 @@ if($_POST) {
} }
break; break;
case 'add': case 'add':
$form = DynamicForm::create(array( $form = DynamicForm::create();
'title'=>$_POST['title'], foreach ($fields as $f) {
'instructions'=>$_POST['instructions'], if (in_array($f, $required) && !$_POST[$f])
'notes'=>$_POST['notes'])); $errors[$f] = sprintf('%s is required',
$form->save(true); mb_convert_case($f, MB_CASE_TITLE));
elseif (isset($_POST[$f]))
$form->set($f, $_POST[$f]);
}
break; break;
case 'mass_process': case 'mass_process':
...@@ -93,7 +97,6 @@ if($_POST) { ...@@ -93,7 +97,6 @@ if($_POST) {
if (!$_POST["label-new-$i"]) if (!$_POST["label-new-$i"])
continue; continue;
$field = DynamicFormField::create(array( $field = DynamicFormField::create(array(
'form_id'=>$form->get('id'),
'sort'=>$_POST["sort-new-$i"] ? $_POST["sort-new-$i"] : ++$max_sort, 'sort'=>$_POST["sort-new-$i"] ? $_POST["sort-new-$i"] : ++$max_sort,
'label'=>$_POST["label-new-$i"], 'label'=>$_POST["label-new-$i"],
'type'=>$_POST["type-new-$i"], 'type'=>$_POST["type-new-$i"],
...@@ -103,13 +106,19 @@ if($_POST) { ...@@ -103,13 +106,19 @@ if($_POST) {
)); ));
$field->setForm($form); $field->setForm($form);
if ($field->isValid()) if ($field->isValid())
$field->save(); $form_fields[] = $field;
else else
$errors["new-$i"] = $field->errors(); $errors["new-$i"] = $field->errors();
} }
// XXX: Move to an instrumented list that can handle this better // XXX: Move to an instrumented list that can handle this better
if (!$errors) if (!$errors) {
$form->_dfields = $form->_fields = null; $form->_dfields = $form->_fields = null;
$form->save(true);
foreach ($form_fields as $field) {
$field->set('form_id', $form->get('id'));
$field->save();
}
}
} }
if ($errors) if ($errors)
$errors['err'] = 'Unable to commit form. Check validation errors'; $errors['err'] = 'Unable to commit form. Check validation errors';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment