Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Milestone: enhance form validation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Keesun Baik authored and insanehong committed May 21, 2015
1 parent 4d1e7f3 commit b377be2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 12 additions & 4 deletions app/controllers/MilestoneApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ public static Result newMilestoneForm(String userName, String projectName) {
/**
* 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)
public static Result newMilestone(String userName, String projectName) {
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 {
Expand All @@ -115,13 +116,19 @@ public static Result newMilestone(String userName, String projectName) {
}
}

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");
}
}

/**
* when: GET /:user/:project/milestone/:id/editform
*/
Expand All @@ -146,8 +153,9 @@ public static Result editMilestone(String userName, String projectName, Long mil
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 {
Expand Down
6 changes: 3 additions & 3 deletions app/views/milestone/create.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/service/yobi.milestone.Write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit b377be2

Please sign in to comment.