Javascript ExtJS 4 - 如果自定义验证失败,如何将表单字段标记为无效并在其周围显示红色边框(如 ExtJS 的默认设置)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7723041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJS 4 - How to mark form field invalid and display red borders around it (as done default by ExtJS) if custom validation fails?
提问by netemp
I have a form in which some of the fields need to be validated at the server side.
我有一个表单,其中一些字段需要在服务器端进行验证。
When the form is submitted, then the server validates the values of these fields and if validations fail then server returns success:false (along with name and error message of each field at which validation has failed).
提交表单时,服务器会验证这些字段的值,如果验证失败,则服务器返回 success:false(以及验证失败的每个字段的名称和错误消息)。
Now, I need to display such fields as 'invalid' and apply the same red-border around them which is default done by ExtJS if client side validation failed.
现在,我需要将这些字段显示为“无效”并在它们周围应用相同的红色边框,如果客户端验证失败,这是 ExtJS 默认完成的。
I tried using the following:
我尝试使用以下方法:
Ext.getCmp('fieldId').markInvalid() and invalidCls:'x-form-invalid-field'
Ext.getCmp('fieldId').markInvalid() 和 invalidCls:'x-form-invalid-field'
I used the above statements in the 'failure' callback function of form.submit. These statments get called but don't apply any effect on such fields.
我在form.submit 的'failure' 回调函数中使用了上述语句。这些语句被调用,但不会对这些字段产生任何影响。
Thus could anyone guide at the following:
因此,任何人都可以指导以下内容:
How to mark a field invaild and apply the same effect (having red-borders) around it when a custom validation fails?
当自定义验证失败时,如何将字段标记为无效并在其周围应用相同的效果(具有红色边框)?
Thanks in advance.
提前致谢。
采纳答案by netemp
I have been able to find a solution for this.
我已经能够为此找到解决方案。
In case of server side validation failing, following needs to be returned from the server:
如果服务器端验证失败,则需要从服务器返回以下内容:
success:false,
errors:{
field1:errorMsg1,
field2:errorMsg2
}
This will itself mark the fields as invalid and apply the red-border to the fields if there is an error associated.
如果存在相关错误,这本身会将字段标记为无效并将红色边框应用于字段。
Hope this helps someone looking for something similar.
希望这有助于有人寻找类似的东西。
回答by Kld
Get the error message from the server
从服务器获取错误信息
Ext.getCmp('your_form_id').getForm().findField('field_id_or_field_name').markInvalid('server_error_message');
回答by andep
Instead of markInvalid function use setActiveError if you want to change error message too.
如果您也想更改错误消息,请使用 setActiveError 代替 markInvalid 函数。
Ext.getCmp('your_component_id').setActiveError('your_custom_error_message')
回答by Kld
Try this , it's about the server validation , you can use json or xml http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/form/xml-form.html
试试这个,它是关于服务器验证的,你可以使用 json 或 xml http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/form/xml-form.html
回答by user6554589
Ext.getCmp('#fieldId').isValid();
Ext.getCmp('#fieldId').isValid();