【发布时间】:2016-01-18 14:22:25
【问题描述】:
我使用 FOERestBundle 和类视图。当我验证实体时,我有这样的对象错误,这是:
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
当用户这样的安全令牌无效时,我需要返回对象错误
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
]
现在我有了纯文本。这是我的终点
/**
* Update existing Bit from the submitted data.
*
* @ApiDoc(
* resource = true,
* description = "Update single Bit",
* parameters={
* {"name"="status", "dataType"="string", "required"=false, "description"="status for bit"},
* {"name"="text", "dataType"="string", "required"=true, "description"="text for rejected"},
* {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"}
* },
* statusCodes = {
* 200 = "Bit successful update",
* 400 = "Secret token is not valid"
* },
* section="Bit"
* )
* @RestView()
*
* @param Request $request
* @param string $id
*
* @return View
*/
public function putBitAction(Request $request, $id)
{
$manager = $this->getDoctrine()->getManager();
$token = $this->get('request')->request->get('token');
$user = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneBySecuritytoken($token);
$bit = $manager->getRepository('MyBundle:Bit')->find($id);
$view = View::create();
if (!empty($user) && !empty($bit) && !empty($token)) {
*some logic
$view = $this->view($bit, 200);
return $this->handleView($view);
}
} else {
$view = $this->view('Secret token is not valid', 400);
return $this->handleView($view);
}
}
现在我有了纯文本
Response Body [Raw]
"Secret token is not valid"
这是返回对象错误验证,这没关系
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
如何返回自定义错误,如对象而不是纯文本?
【问题讨论】:
标签: php validation symfony fosrestbundle