处理Ember.js中的验证错误 [英] Handling Validation Errors in Ember.js
问题描述
我正在尝试从客户端上的表单上显示验证错误。
Rails正在返回此json:
{errors:{hometown [太长了(最多64个字符)]}}
在我的手柄模板中目前的路线我试图迭代错误,但是我没有得到错误部分的输出:
< div类=形式的基团>
< label> Hometown< / label>
{{#each errors.hometown}}
{{this}}
{{/ each}}
{{input type =textclass =form-control valueBinding =effectiveUser.hometowndisabled = entryNotAllowed size =50}}
< / div>
我还根据这个博客更新了我的RESTadapter: https://marcqualie.com/2014/04/model-errors-in-emberjs 包括:
ajaxError:function(jqXHR){
var error = this._super(jqXHR);
if(jqXHR&& jqXHR.status === 422){
var jsonErrors = Ember。$。parseJSON(jqXHR.responseText)[errors];
返回新的DS.InvalidError(jsonErrors);
} else {
return error;
}
}
我还是不明白这个错误的上下文对象是为什么我的观点可以访问我,但是我有几个不同的来源似乎说这个设置应该可以工作。任何见解都将不胜感激。
只要我明白,这是正常的,这是不正常的:
要将您的模型保存在控制器/路由/视图中,您将执行返回承诺的 save()
操作。如果一个承诺是被拒绝
,则可以使用原因
作为参数执行处理此拒绝的功能。在你的情况下, DS.InvalidError
对象将成为这个原因
。
$ b $ ($)$($)$($)$($)$($)$ {
$失败
});
所以在你的情况下(但取决于你在哪里处理我将在控制器中的行动)应该做的窍门;
actions:{
submitForm:function(){
this.set 错误,NULL);
var ctx = this;
myModel.save()。then(function(){
//显示一个succes msg?
},function(errors){
ctx.set(errors错误);
});
}
}
I have a rails app serving json to an ember frontend.
I am trying to display validation errors from on a form on the client.
Rails is returning this json:
{"errors":{"hometown":["is too long (maximum is 64 characters)"]}}
In my handlebars template for the current route I am attempting to iterate through the errors but I don't get any output for the errors section:
<div class="form-group">
<label>Hometown</label>
{{#each errors.hometown}}
{{this}}
{{/each}}
{{input type="text" class="form-control" valueBinding="effectiveUser.hometown" disabled=entryNotAllowed size="50"}}
</div>
I also updated my RESTadapter based on this blog: https://marcqualie.com/2014/04/model-errors-in-emberjs to include:
ajaxError: function(jqXHR) {
var error = this._super(jqXHR);
if (jqXHR && jqXHR.status === 422) {
var jsonErrors = Ember.$.parseJSON(jqXHR.responseText)["errors"];
return new DS.InvalidError(jsonErrors);
} else {
return error;
}
}
I still really dont understand the context of what this errors object is and why my view has access to it that I but several different sources seem to say this setup should work. Any insight would be appreciated.
Well as far as i understand ember it's normal this isn't working :
To save your model in your controller/route/view you are performing a save()
operation which returns a promise. If a promise is rejected
a function to handle this rejection can be executed with a reason
as a parameter. In your case the DS.InvalidError
object will become this reason
.
myModel.save().then(function(value){
//success
},function(reason){
//fail
});
so in your case (but depends where you are handeling action i will supose in controller) something like that should do the trick;
actions: {
submitForm : function(){
this.set("errors",null);
var ctx=this;
myModel.save().then( function(){
//display a succes msg ?
}, function(errors){
ctx.set("errors",errors);
});
}
}
这篇关于处理Ember.js中的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!