From aa34b140ee700deb1a2fe723c11671ede720cf6f Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 16 May 2014 17:22:20 +0000
Subject: [PATCH] bug: Re-render dynamic form on validation error

---
 include/class.dynamic_forms.php   | 13 +++++++++++--
 include/class.ticket.php          |  2 +-
 include/client/open.inc.php       | 10 ++++++++++
 include/staff/ticket-open.inc.php | 18 +++++++++++++++---
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 2da492069..b01871b56 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -580,17 +580,26 @@ class DynamicFormEntry extends VerySimpleModel {
      * $filter - (callback) function to receive each field and return
      *      boolean true if the field's errors are significant
      */
-    function isValid($include=false) {
+    function isValid($filter=false) {
         if (!is_array($this->_errors)) {
             $this->_errors = array();
             $this->getClean();
             foreach ($this->getFields() as $field)
-                if ($field->errors() && (!$include || $include($field)))
+                if ($field->errors() && (!$filter || $filter($field)))
                     $this->_errors[$field->get('id')] = $field->errors();
         }
         return !$this->_errors;
     }
 
+    function isValidForClient() {
+
+        $filter = function($f) {
+            return !$f->get('private');
+        };
+
+        return $this->isValid($filter);
+    }
+
     function getClean() {
         if (!$this->_clean) {
             $this->_clean = array();
diff --git a/include/class.ticket.php b/include/class.ticket.php
index e70b25dec..d46734e7d 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2294,7 +2294,7 @@ class Ticket {
         }
 
         if ($vars['topicId'] && ($topic=Topic::lookup($vars['topicId']))) {
-            if ($topic_form = DynamicForm::lookup($topic->ht['form_id'])) {
+            if ($topic_form = $topic->getForm()) {
                 $topic_form = $topic_form->instanciate();
                 if (!$topic_form->getForm()->isValid($field_filter('topic')))
                     $errors = array_merge($errors, $topic_form->getForm()->errors());
diff --git a/include/client/open.inc.php b/include/client/open.inc.php
index 26d146f22..dc481b3b9 100644
--- a/include/client/open.inc.php
+++ b/include/client/open.inc.php
@@ -8,6 +8,16 @@ if($thisclient && $thisclient->isValid()) {
 }
 
 $info=($_POST && $errors)?Format::htmlchars($_POST):$info;
+
+$form = null;
+if ($info['topicId'] && ($topic=Topic::lookup($info['topicId']))) {
+    $form = $topic->getForm();
+    if ($_POST && $form) {
+        $form = $form->instanciate();
+        $form->isValidForClient();
+    }
+}
+
 ?>
 <h1>Open a New Ticket</h1>
 <p>Please fill in the form below to open a new ticket.</p>
diff --git a/include/staff/ticket-open.inc.php b/include/staff/ticket-open.inc.php
index b472f4676..dbf2180e3 100644
--- a/include/staff/ticket-open.inc.php
+++ b/include/staff/ticket-open.inc.php
@@ -2,6 +2,16 @@
 if(!defined('OSTSCPINC') || !$thisstaff || !$thisstaff->canCreateTickets()) die('Access Denied');
 $info=array();
 $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
+
+$form = null;
+if ($info['topicId'] && ($topic=Topic::lookup($info['topicId']))) {
+    $form = $topic->getForm();
+    if ($_POST && $form) {
+        $form = $form->instanciate();
+        $form->isValid();
+    }
+}
+
 ?>
 <form action="tickets.php?a=open" method="post" id="save"  enctype="multipart/form-data">
  <?php csrf_token(); ?>
@@ -130,8 +140,8 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                                 $selected, $name);
                         }
                         if (count($topics) == 1 && !$form) {
-                            $T = Topic::lookup($id);
-                            $form = DynamicForm::lookup($T->ht['form_id']);
+                            if (($T = Topic::lookup($id)))
+                                $form =  $T->getForm();
                         }
                     }
                     ?>
@@ -233,7 +243,9 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
         </tbody>
         <tbody id="dynamic-form">
         <?php
-            if ($form) $form->getForm()->render(true);
+            if ($form) {
+                include(STAFFINC_DIR .  'templates/dynamic-form.tmpl.php');
+            }
         ?>
         </tbody>
         <tbody> <?php
-- 
GitLab