0

Link: How to create DateTime object from string in symfony2/php

I am taking date from a form and passing all that data via a form.serialize() method with ajax. On the Symfony side, I am getting that value in string format. Now when I try to insert that data into database it was giving me an error as the type of that field was given date. So thanks to @briosheje I solved that thing.

But what if Now, sometimes I get null value from the form for that date value? Any idea, Anyone? Ohh, btw right now I have used if($dateValue != null OR $dateValue != '') condition.. any better solution then if() condition?

1 Answer 1

1

If a date is optional and can be NULL in the database you can just do

$protocol->setStartedAt(null);

If not than the validation of your form (also with ajax!) should fail and the controller should return some JSON with a error message. You could do something like

{"success":false,"errors":[{"started_at":"Set this value!"}]}

And let javascript show the error(s).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.