我有一个 RESTful Web 服务来编写代码,现在我必须验证发送到服务器的请求日期,同时使用多个实体执行 PUT 方法。
但现在最大的问题是:我如何应对(可能很多)验证错误?
请求方法和 URL:
PUT http://example.dev/app_dev.php/api-1.0/labels.xml
请求正文(当服务器接收到它时):
array(size=2)
0 =>
array (size=11)
'id' => string '53' (length=2)
'name' => string '2222' (length=4)
'app_domain' => string '11' (length=2)
[...]
1 =>
array (size=12)
'id' => string '54' (length=2)
'name' => string 'testname2' (length=9)
'controllpanel_domain' => string 'testname2' (length=9)
'label' => string 'testname2' (length=9)
'app_domain' => string 'testname2' (length=9)
[...]
我目前得到以下回应。HTTP 状态码是 400。
在 xml 中:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>
<![CDATA[error]]>
</status>
<code>400</code>
<text>
<![CDATA[Bad Request]]>
</text>
<message>
<![CDATA[Unable to validate label entities]]>
</message>
<violations>
<object>
<object>
<label>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</label>
<controllpanel_domain>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</controllpanel_domain>
<app_domain>
<entry>
<violation_message>
<![CDATA[This value is too short. It should have 3 characters or more]]>
</violation_message>
</entry>
<entry>
<violation_message>
<![CDATA[This value should be 333 or more]]>
</violation_message>
</entry>
</app_domain>
</object>
</object>
<object>
<object>
<controllpanel_domain>
<entry>
<violation_message>
<![CDATA[This value should be a valid number]]>
</violation_message>
</entry>
</controllpanel_domain>
<app_domain>
<entry>
<violation_message>
<![CDATA[This value should be a valid number]]>
</violation_message>
</entry>
</app_domain>
<login_left_text>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</login_left_text>
</object>
</object>
</violations>
</result>
在json中:
{
"status": "error",
"code": 400,
"text": "Bad Request",
"message": "Unable to validate label entities",
"violations": [
{
"object": {
"label": [
{
"violation_message": "This value should not be blank"
}
],
"controllpanel_domain": [
{
"violation_message": "This value should not be blank"
}
],
"app_domain": [
{
"violation_message": "This value is too short. It should have 3 characters or more"
},
{
"violation_message": "This value should be 333 or more"
}
]
}
},
{
"object": {
"controllpanel_domain": [
{
"violation_message": "This value should be a valid number"
}
],
"app_domain": [
{
"violation_message": "This value should be a valid number"
}
],
"login_left_text": [
{
"violation_message": "This value should not be blank"
}
]
}
}
]
}
我可以用更好的数据来回应吗?