私が達成しようとしているのは、このサンプルアプリだけです:〜\ play-2.1.0 \ samples \ java \ forms
更新された最新のコード:
私のようにquestion.scala.html
見えます:
@(questionForm: Form[Question])
@import helper._
@import helper.twitterBootstrap._
@answerField(field: Field, className: String = "answer") = {
<div class="twipsies well @className">
<table>
<tr>
<td> @checkbox(field("addRight"),'_label -> "Add")</td>
<td> @checkbox(field("editRight"),'_label -> "Edit")</td>
<td> @checkbox(field("delRight"),'_label -> "Delete")</td>
<td> @checkbox(field("viewRight"),'_label -> "View")</td>
</tr>
</table>
</div>
}
@if(questionForm.hasErrors) {
<div class="alert-message error">
<p><strong>Oops</strong> Please fix all errors</p>
</div>
}
@helper.form(action = routes.Questions.submit, 'id -> "form") {
<fieldset>
@inputText(
questionForm("name"),
'_label -> "Name of a Right"
)
@inputText(
questionForm("namex"),
'_label -> "Name of a Right"
)
<div class="answers">
@repeat(questionForm("answers"), min = 0) { answer =>
@answerField(answer)
}
@**
* Keep an hidden block that will be used as template for Javascript copy code
* answer_template is only css style to make it hidden (look main.css and declare your own answer_template at bottom)
**@
@answerField(
questionForm("answers"),
className = "answer_template"
)
<div class="clearfix">
<div class="input">
<a class="addAnswer btn success">Add </a>
</div>
</div>
</div>
</fieldset>
<div class="actions">
<input type="submit" class="btn primary" value="Insert">
</div>
}
<script type="text/javascript" charset="utf-8">
$('.removeAnswer').on('click', function(e) {
var answers = $(this).parents('.answers');
$(this).parents('.answer').remove();
renumber(answers);
});
$('.addAnswer').on('click', function(e) {
var answers = $(this).parents('.answers');
var template = $('.answer_template', answers);
template.before('<div class="clearfix answer">' + template.html() + '</div>');
renumber(answers);
});
$('#form').submit(function() {
$('.answer_template').remove()
});
// -- renumber fields
// This is probably not the easiest way to do it. A jQuery plugin would help.
var renumber = function(answers) {
$('.answer').each(function(i) {
$('input', this).each(function() {
$(this).attr('name', $(this).attr('name').replace(/answers\[.+?\]/g, 'answers[' + i + ']'))
});
});
}
</script>
...。
質問モデル:
package models;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Question extends Model {
@Id
public Long id;
@Constraints.Required
public String name;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "question")
public List<Answer> answers;
public Question() {
}
public Question(String name) {
this.name = name;
}
}
回答モデル:
@Entity
public class Answer extends Model {
@Id
public Long id;
public boolean addRight;
public boolean editRight;
public boolean delRight;
public boolean viewRight;
@ManyToOne
public Question question;
public Answer() {
}
public Answer(boolean addRight,boolean editRight,boolean delRight,boolean viewRight,
Question question) {
this.addRight = addRight;
this.editRight = editRight;
this.delRight = delRight;
this.viewRight = viewRight;
this.question = question;
}
}
そして最後にコントローラーの保存部分:
public static Result submit() {
Form<Question> filledForm = questionForm.bindFromRequest();
if(filledForm.hasErrors()) {
User user = User.findByUserName("samuel");
return badRequest(question.render(filledForm));
}
else {
// If we dont have any errors, we should be around here :)
Question question = filledForm.get();
// Since Answer needs reference to Question and with new Question
// it cant get it loaded from DB we need to do little dirty trick here
// in order to save new question id instantly to answers foreign_key
// as question_id, otherwise it will be null
System.out.println("-->" + question.answers.size() );
if(question.answers != null) {
ArrayList<Answer> answersCopy = new ArrayList<Answer>();
Logger.trace("Size of Anwsers : " +answersCopy.size());
for(Answer answer : question.answers) {
answersCopy.add(new
Answer(answer.addRight,answer.editRight,answer.delRight,answer.viewRight,question));
System.out.println("##" + answer.addRight);
}
question.answers = answersCopy;
}
question.save();
return ok("Nice, all saved!");
}
}
上記のコードでは、例外は発生しませんが、..質問の部分では、Anwserを残しておく必要があります。
ありがとう