Commits

Robert Clipsham committed a43cfb3

Restore form contents when it is invalid.
Improvements to the error controller.
Added a utility method to Log for debugging purposes.

  • Participants
  • Parent commits 101d487

Comments (0)

Files changed (6)

 [[ -z $DC ]] && DC='ldmd'
 [[ -z $DEBUG ]] && DEBUG=1
 ARGS="-c -Ifcgi $@"
-(( $DEBUG == 1 )) && ARGS="-g -unittest $ARGS"
+(( $DEBUG == 1 )) && ARGS="-g -debug -unittest $ARGS"
 if [[ $DC = 'gdmd' ]]; then
     OD='-od'
     OF='-of'

example/controllers/Error.d

     HtmlDocument viewDefault(Request request, char[][] args)
     {
         setTitle("Error");
+        log.error(args[0]);
         auto doc = new HtmlDocument;
-        doc.p.content = "Error: " ~ args[0];
+        doc.pre.content = "Error: " ~ args[0];
         return doc;
     }
 }
     {
         ret ~= `public Field ` ~ type ~ `(char[] label, char[] name, char[] value=null)
                 {
-                    auto el = mForm.input.attr("type", toLower("` ~ type ~ `"c));
+                    auto par = mForm.p;
+                    auto el = par.input.attr("type", toLower("` ~ type ~ `"c));
                     if (label !is null)
                     {
-                        mForm.label.attr("for", name).content = label;
+                        par.label(true).attr("for", name).content = label;
                         el.attr("id", name);
                     }
                     el.attr("name", name);
-                    if (value !is null)
+                    if (value is null)
+                    {
+                        if (auto ptr = name in mArgs)
+                        {
+                            el.attr("value", *ptr);
+                        }
+                    }
+                    else
                     {
                         el.attr("value", value);
                     }
     private Error[]      mErrors;
     private Field[]      mFields;
     private HtmlDocument mForm;
+    private Method       mMethod;
 
     this(Method method, char[] action, Arguments args)
     {
         mForm = form.attr("method", method ? "post" : "get")
                     .attr("action", action);
         mArgs = args is null ? new Arguments : args;
+        mMethod = method;
     }
 
     /**
      */
     bool validate(bool prependErrors=true)
     {
-        if (mArgs.size == 0)
+        if (mMethod != Method.Post || mArgs.size == 0)
         {
             return false;
         }
         foreach (field; mFields)
         {
-            if (auto arg = field.mName in mArgs)
-            {
-                mErrors ~= field.validate(arg is null ? "" : *arg);
-            }
+            auto arg = field.mName in mArgs;
+            mErrors ~= field.validate(arg is null ? "" : *arg);
         }
         if (prependErrors)
         {
     
     public Field textArea(char[] label, char[] name, char[] value=null)
     {
-        mForm.label.attr("for", name).content = label;
-        mForm.textarea.attr("id", name).content = value;
+        auto par = mForm.p;
+        par.label.attr("for", name).content = label;
+        if (value is null)
+        {
+            if (auto ptr = name in mArgs)
+            {
+                value = *ptr;
+            }
+        }
+        par.textarea.attr("id", name).attr("name", name).content = value;
         mFields ~= new Field(label, name);
         return mFields[$-1];
     }

serenity/HtmlDocument.d

         char[] elName = fcToLower(el == "Body_" ? "Body" : el);
         methods ~= `final public typeof(this) ` ~ fcToLower(el) ~`()
                     {
-                        debug static int count = 0;
                         if (auto el = root.find("` ~ elName ~ `"))
                         {
                             return el[0];
                         }
                         else
                         {
-                            debug assert(count == 0);
-                            debug count++;
                             mChildren ~= new typeof(this)(this, ElementType.` ~ el ~ `);
                             return mChildren[$-1];
                         }
     }
 
     /**
+     * Log a generic debugging error message
+     *
+     * Params:
+     *  message = message to print
+     *  params  = parameters, if any for formatting
+     */
+    debug static void error(T...)(char[] message, T params)
+    {
+        TangoLog.lookup("serenity.debug").error(message, params);
+    }
+
+    /**
      * Set the logging level
      *
      * Params:

serenity/Serenity.d

         catch (Exception e)
         {
             sw.stop;
+            // TODO Replace this with stderr, shouldn't appear when not debugging
+            mStdout("Status: 500 Internal Server Error\r\n\r\n");
             mStdout("<pre>\n");
-            e.writeOut((char[] str) { mStderr(str); });
+            e.writeOut((char[] str) { mStdout(str); });
             mStdout("\n</pre>\n");
             return 1;
         }