Keesun Baik 2015-05-20
Milestone: enhance form validation
Change the regular expression to prevent wrong date string like 2015-05-2222.
Even if, the changed regular expression can prevent it, but it is still possible to send wrong date sting like 2015-05-99.
So, I've added server-side validation for the case of someone send form data without browser.
Last but not least, in case there are form errors checked by server-side, show the form data that the user sent.
@b377be2bed741d251f37ce79cfe00aecec47cdb5
app/controllers/MilestoneApp.java
--- app/controllers/MilestoneApp.java
+++ app/controllers/MilestoneApp.java
@@ -88,7 +88,7 @@
     /**
      * when: POST /:user/:project/milestones
      *
-     * @see {@link #validate(models.Project, play.data.Form)}
+     * @see {@link #validateTitle(models.Project, play.data.Form)}
      */
     @Transactional
     @IsCreatable(ResourceType.MILESTONE)
@@ -96,7 +96,8 @@
         Form<Milestone> milestoneForm = new Form<>(Milestone.class).bindFromRequest();
         Project project = Project.findByOwnerAndProjectName(userName, projectName);
 
-        validate(project, milestoneForm);
+        validateTitle(project, milestoneForm);
+        validateDueDate(milestoneForm);
         if (milestoneForm.hasErrors()) {
             return ok(create.render("title.newMilestone", milestoneForm, project));
         } else {
@@ -115,10 +116,16 @@
         }
     }
 
-    private static void validate(Project project, Form<Milestone> milestoneForm) {
+    private static void validateTitle(Project project, Form<Milestone> milestoneForm) {
         if (!Milestone.isUniqueProjectIdAndTitle(project.id, milestoneForm.field("title").value())) {
             milestoneForm.reject("title", "milestone.title.duplicated");
             flash(Constants.WARNING, "milestone.title.duplicated");
+        }
+    }
+
+    private static void validateDueDate(Form<Milestone> milestoneForm) {
+        if (milestoneForm.hasErrors() && milestoneForm.errors().containsKey("dueDate")) {
+            flash(Constants.WARNING, "milestone.error.duedateFormat");
         }
     }
 
@@ -146,8 +153,9 @@
         Milestone original = Milestone.findById(milestoneId);
 
         if(!original.title.equals(milestoneForm.field("title").value())) {
-            validate(project, milestoneForm);
+            validateTitle(project, milestoneForm);
         }
+        validateDueDate(milestoneForm);
         if (milestoneForm.hasErrors()) {
             return ok(edit.render("title.editMilestone", milestoneForm, milestoneId, project));
         } else {
app/views/milestone/create.scala.html
--- app/views/milestone/create.scala.html
+++ app/views/milestone/create.scala.html
@@ -34,12 +34,12 @@
                     <div class="inner left">
                         <div class="title-wrap">
                             <label for="title">@Messages("milestone.form.title")</label>
-                            <input type="text" name="title" id="title" placeholder="">
+                            <input type="text" name="title" id="title" placeholder="" value="@form.data().get("title")">
                         </div>
                         <div class="content-wrap">
                             <label for="contents">@Messages("milestone.form.content")</label>
                             <div style="position: relative;">
-                                @common.editor("contents")
+                                @common.editor("contents", form.data().get("contents"))
                             </div>
                         </div>
 
@@ -61,7 +61,7 @@
                         <hr/>
                         <p>@Messages("milestone.form.dueDate")</p>
                         <label for="dueDate">
-                            <input type="text" name="dueDate" id="dueDate" class="validate due-date">
+                            <input type="text" name="dueDate" id="dueDate" class="validate due-date" value="@form.data().get("dueDate")">
                         </label>
                         <div id="datepicker" class="date-picker"></div>
 
public/javascripts/service/yobi.milestone.Write.js
--- public/javascripts/service/yobi.milestone.Write.js
+++ public/javascripts/service/yobi.milestone.Write.js
@@ -42,7 +42,7 @@
          */
         function _initVar(htOptions){
             htVar.sDateFormat  = htOptions.sDateFormat  || "YYYY-MM-DD";
-            htVar.rxDateFormat = htOptions.rxDateFormat || /\d{4}-\d{2}-\d{2}/;
+            htVar.rxDateFormat = htOptions.rxDateFormat || /\d{4}-\d{2}-\d{2}$/;
             htVar.sTplFileItem = $('#tplAttachedFile').text();
         }
 
Add a comment
List