php - Symfony Assert DateTime JMSSerializer -
i have endpoint postorder - create entity order. in entity order have fields type datetime , want when write string instead datetime have "not valid, should datetime" . other fields use
* @assert\length(min=3, max=255)
or
* @assert\regex( * pattern= "/^[\d() \-+]+$/", * message= "this text cannot contain numbers" * )
or
* @assert\notblank()
i request serialize deserialize concrete entity when validate , have information assert in endpoint, datetime not work use fosrestbundle , jmsserializer action
/** * post order. * * @apidoc( * resource = true, * description = "post order", * parameters={ * {"name"="comment", "datatype"="string", "required"=false, "description"="comment"}, * {"name"="interview_date", "datatype"="date", "required"=false, "description"="date conect developer"}, * {"name"="contact_date", "datatype"="date", "required"=false, "description"="date contact fir tim"} * * }, * statuscodes = { * 200 = "returned when successful", * 400 = "returned secret token not valid" * }, * section="order" * ) * * @restview() * * @param request $request * * @return view * * @throws notfoundhttpexception when not exist */ public function postorderaction(request $request) { $data = $request->request->all(); $data = $this->get('serializer')->serialize($data, 'json'); $servicelead = $this->get('serializer')->deserialize($data, 'artel\profilebundle\entity\codeservicelead', 'json'); $errors = $this->get('validator')->validate($servicelead); if (count($errors) > 0) { $view = $this->view($errors, 400); return $this->handleview($view); }
and fields
class orders { /** * @var string * * @orm\column(name="comment", type="string", nullable=true) * @groups({"get_all_orders_admin", "get_all_orders", "for_vip"}) */ protected $comment; /** * @var \datetime * @orm\column(name="interview_date", type="date", nullable=true) * @groups({"get_all_orders_admin", "get_all_orders", "for_vip"}) * @assert\datetime() */ protected $interview_date; /** * @var \datetime * @orm\column(name="contact_date", type="date", nullable=true) * @groups({"get_all_orders_admin", "get_all_orders", "for_vip"}) * @assert\datetime() */ protected $contact_date;
now have error when try deserialize entity order
{ "error": { "code": 500, "message": "internal server error", "exception": [ { "message": "invalid datetime \"some_string\", expected format y-m-d\\th:i:so.", "class": "jms\\serializer\\exception\\runtimeexception",
how return correct error or assert, without 500, in situation ?
when call deserialize()
, checking entity validity against doctrine annotations since jms serializer already integrates doctrine orm.
if deserializing fails, exception thrown, seeing.
all need in case put code in try/catch
block if want handle yourself:
public function postorderaction(request $request) { $data = $request->request->all(); $data = $this->get('serializer')->serialize($data, 'json'); try { $servicelead = $this->get('serializer')->deserialize( $data, 'artel\profilebundle\entity\codeservicelead', 'json' ); } catch (\exception $e) { $view = $this->view((array) $e->getmessage(), 400); return $this->handleview($view); } }
i didn't see view()
function assumed expecting array of strings of error messages cast exception message array. either way idea.
Comments
Post a Comment