我创建了一个输入表单。当用户提交值但未通过验证时,如何从用户那里获取原始值以显示?我可以正确地看到声明为“必填字段”的验证错误消息。它转发回输入表单并显示验证错误,但它不保存用户输入的值。
该模型
@Entity
public class Question extends BaseModel {
@Required
@MaxSize(100)
public String title;
@Required
@MaxSize(5000)
public String txt;
}
控制器
public class Questions extends Controller {
public static void ask() {
Question question = new Question();
render(question);
}
public static void addQuestion(Question question) {
// save question
if (!question.validateAndSave()) {
params.flash();
Validation.keep();
ask();
}
Application.index();
}
}
风景
#{form @exchange.Questions.addQuestion()}
<label class="control-label" for="question.title">Question Title</label>
<input type="text" class="input-large" name="question.title" value="${question.title}" />
#{if errors.forKey('question.title')}<span class="alert-error"> #{error 'question.title'/}</span> #{/if}
<label class="control-label" for="question.txt">Details</label>
<textarea class="input-xxlarge span7" rows="6" name="question.txt">${question.txt}</textarea>
#{if errors.forKey('question.txt')}<br/><span class="alert-error"> #{error 'question.txt'/}</span> #{/if}
<button type="submit" class="btn btn-primary">Ask Question</button>
#{/form}