gpt4 book ai didi

forms - Symfony2 表单验证错误返回 HTTP 代码 '200'

转载 作者:行者123 更新时间:2023-12-01 13:48:08 26 4
gpt4 key购买 nike

通过 HTTP POST 提交无效表单后,我希望我的 Symfony REST 服务(使用 FOSRestBundle)返回 400 Bad Request 代码。相反,它返回 200 OK,即使它像我想要的那样返回 JSON 格式的错误。

在我的实体中,我正在使用

Symfony\Component\Validator\Constraints as Assert;

确保没有为任何字段提供空白条目。例如:

/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\NotBlank()
*/
private $title;

这是我的 Controller 方法:

public function postAvrequestAction(Request $request){
$entity = new AvRequest();

$form = $this->get('form.factory')->createNamed('', new AvRequestType(), $entity);
$form->handleRequest($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();

$serializer = $this->get('serializer');
$serialized = $serializer->serialize($entity, 'json');

return new Response($serialized, 201);
}

return new JsonResponse(array(
'errors' => $this->getFormErrors($form, 400)
));

这是我提交时带有 AJAX 代码的 HTML 表单:

       <form id="postform" role="form" method="POST" action="http://localhost:8000/api/avrequests">
Title: <input type="text" name="title" id="title"/>
Request Date: <input type="text" name="requestDate" id="requestDate"/>
Deliver Date: <input type="text" name="deliverDate" id="deliverDate"/>
Return Date: <input type="text" name="returnDate" id="returnDate"/>
<input type="submit" class="button" value="Submit Request" />
</form>
<script>
<!--
$(document).ready(function(){
$('#postform').validate({
debug: true,
rules: {
...
},
messages: {
...
},
submitHandler: function(form){
event.preventDefault();

ajaxObject = {
url: $("#postform").attr("action"),
type: 'POST', // Can be GET, PUT, POST or DELETE only
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({"title":$("#title").val(), "requestDate":$("#requestDate").val(), "deliverDate":$("#deliverDate").val(), "returnDate":$("#returnDate").val()})
};

$.ajax(ajaxObject)
.done(function(data,status,xhr) {
console.log( status );
$(':input','#postform').not(':button, :submit, :reset, :hidden').val('');
$('#postform').hide();
$('#submitmessage').addClass('alert-box success').html('Success! We have received your request and should receive a confirmation email shortly.');
$('#resultsdiv').html('<p><strong>Here is a review of your request:</strong></p><p><strong>Request Date:</strong> ' + data.request_date + '</p><p><strong>Delivery Date:</strong> ' + data.request_date + '</p><p><strong>Return Date:</strong> ' + data.return_date + '</p><p><a href="/library_new/forms/avrequest.php">Submit another request</a></p>');
})
.fail(function(data,status,xhr) {
console.log( status );
})
.always(function(data,status,xhr) {
console.log( data );
});
}
});
});
-->
</script>

假设我提交了一个完全空白的表格。我最终会在控制台中收到此作为响应:

success

{errors: Object}errors: Objectfields: Object
deliverDate: "This value should not be blank."
returnDate: "This value should not be blank."
...

成功函数中的代码运行,而不是错误代码。

最佳答案

return new JsonResponse(array(
'errors' => $this->getFormErrors($form)
), 400);

代替

return new JsonResponse(array(
'errors' => $this->getFormErrors($form, 400)
));

...小细节...

关于forms - Symfony2 表单验证错误返回 HTTP 代码 '200',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com