是否可以将POST元素与模型中的其他名称匹配 [英] Is it possible to match a POST element to an other name in a Model
本文介绍了是否可以将POST元素与模型中的其他名称匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用PlayFramework 2.0.1,我有一个模型:
Using PlayFramework 2.0.1, I have a model :
class MyModel extends Model {
@Constraints.Required
public String someProperty;
}
class MyController extends Controller {
public static Result action() {
Form<MyModel> form = form(MyModel.class).bindFromRequest();
if (form.hasErrors()) {
// Return errors
}
else {
// Process
}
}
}
假设我发布了mysite.com/action?some-property=value
如何将请求中的some-property
匹配到someProperty
到模型?
有可能吗?
How can I match some-property
from the request, to someProperty
to the model ?
Is it possible?
因为到目前为止,它hasErrors()
触发了true
,因为someProperty
似乎丢失了:/
Because so far, it hasErrors()
fire true
since someProperty
appears missing :/
感谢您的帮助!
推荐答案
不要向模型添加约束,而是手动验证并绑定模型:
Don't add constraints to the model, instead validate and bind it manualy:
// ...
MyModel some = MyModel.find.byId(id);
String someFromReq = form().bindFromRequest().get("some-property");
if (someFromReq == null || someFromReq.equals("")){
return badRequest();
}
some.someProperty
some.update();
// ...
这篇关于是否可以将POST元素与模型中的其他名称匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文