
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
... | ... | @@ -88,7 +88,7 @@ |
88 | 88 |
/** |
89 | 89 |
* when: POST /:user/:project/milestones |
90 | 90 |
* |
91 |
- * @see {@link #validate(models.Project, play.data.Form)} |
|
91 |
+ * @see {@link #validateTitle(models.Project, play.data.Form)} |
|
92 | 92 |
*/ |
93 | 93 |
@Transactional |
94 | 94 |
@IsCreatable(ResourceType.MILESTONE) |
... | ... | @@ -96,7 +96,8 @@ |
96 | 96 |
Form<Milestone> milestoneForm = new Form<>(Milestone.class).bindFromRequest(); |
97 | 97 |
Project project = Project.findByOwnerAndProjectName(userName, projectName); |
98 | 98 |
|
99 |
- validate(project, milestoneForm); |
|
99 |
+ validateTitle(project, milestoneForm); |
|
100 |
+ validateDueDate(milestoneForm); |
|
100 | 101 |
if (milestoneForm.hasErrors()) { |
101 | 102 |
return ok(create.render("title.newMilestone", milestoneForm, project)); |
102 | 103 |
} else { |
... | ... | @@ -115,10 +116,16 @@ |
115 | 116 |
} |
116 | 117 |
} |
117 | 118 |
|
118 |
- private static void validate(Project project, Form<Milestone> milestoneForm) { |
|
119 |
+ private static void validateTitle(Project project, Form<Milestone> milestoneForm) { |
|
119 | 120 |
if (!Milestone.isUniqueProjectIdAndTitle(project.id, milestoneForm.field("title").value())) { |
120 | 121 |
milestoneForm.reject("title", "milestone.title.duplicated"); |
121 | 122 |
flash(Constants.WARNING, "milestone.title.duplicated"); |
123 |
+ } |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ private static void validateDueDate(Form<Milestone> milestoneForm) { |
|
127 |
+ if (milestoneForm.hasErrors() && milestoneForm.errors().containsKey("dueDate")) { |
|
128 |
+ flash(Constants.WARNING, "milestone.error.duedateFormat"); |
|
122 | 129 |
} |
123 | 130 |
} |
124 | 131 |
|
... | ... | @@ -146,8 +153,9 @@ |
146 | 153 |
Milestone original = Milestone.findById(milestoneId); |
147 | 154 |
|
148 | 155 |
if(!original.title.equals(milestoneForm.field("title").value())) { |
149 |
- validate(project, milestoneForm); |
|
156 |
+ validateTitle(project, milestoneForm); |
|
150 | 157 |
} |
158 |
+ validateDueDate(milestoneForm); |
|
151 | 159 |
if (milestoneForm.hasErrors()) { |
152 | 160 |
return ok(edit.render("title.editMilestone", milestoneForm, milestoneId, project)); |
153 | 161 |
} else { |
--- app/views/milestone/create.scala.html
+++ app/views/milestone/create.scala.html
... | ... | @@ -34,12 +34,12 @@ |
34 | 34 |
<div class="inner left"> |
35 | 35 |
<div class="title-wrap"> |
36 | 36 |
<label for="title">@Messages("milestone.form.title")</label> |
37 |
- <input type="text" name="title" id="title" placeholder=""> |
|
37 |
+ <input type="text" name="title" id="title" placeholder="" value="@form.data().get("title")"> |
|
38 | 38 |
</div> |
39 | 39 |
<div class="content-wrap"> |
40 | 40 |
<label for="contents">@Messages("milestone.form.content")</label> |
41 | 41 |
<div style="position: relative;"> |
42 |
- @common.editor("contents") |
|
42 |
+ @common.editor("contents", form.data().get("contents")) |
|
43 | 43 |
</div> |
44 | 44 |
</div> |
45 | 45 |
|
... | ... | @@ -61,7 +61,7 @@ |
61 | 61 |
<hr/> |
62 | 62 |
<p>@Messages("milestone.form.dueDate")</p> |
63 | 63 |
<label for="dueDate"> |
64 |
- <input type="text" name="dueDate" id="dueDate" class="validate due-date"> |
|
64 |
+ <input type="text" name="dueDate" id="dueDate" class="validate due-date" value="@form.data().get("dueDate")"> |
|
65 | 65 |
</label> |
66 | 66 |
<div id="datepicker" class="date-picker"></div> |
67 | 67 |
|
--- public/javascripts/service/yobi.milestone.Write.js
+++ public/javascripts/service/yobi.milestone.Write.js
... | ... | @@ -42,7 +42,7 @@ |
42 | 42 |
*/ |
43 | 43 |
function _initVar(htOptions){ |
44 | 44 |
htVar.sDateFormat = htOptions.sDateFormat || "YYYY-MM-DD"; |
45 |
- htVar.rxDateFormat = htOptions.rxDateFormat || /\d{4}-\d{2}-\d{2}/; |
|
45 |
+ htVar.rxDateFormat = htOptions.rxDateFormat || /\d{4}-\d{2}-\d{2}$/; |
|
46 | 46 |
htVar.sTplFileItem = $('#tplAttachedFile').text(); |
47 | 47 |
} |
48 | 48 |
|
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?