1

I am quite new to Symfony 2.7. I am facing a problem with Symfony form. Inside my form i have a form field as follows

 Builder->add('passDate', 'hidden')
        ->add('issueDate','hidden')

Inside my controller function, i need to pass some fix value to database. So here is my form action

$entity->setIssueDate('1950/01/01');
$entity->setPassDate('1950/01/01');

But when i submit it, it show me the error as that these form fields are required, although i set as default date as 1950/01/01. Please help me. Thank you

5
  • 1
    Do you have some validation constraints related to these form fields? I mean, NotBlank()? Commented Aug 23, 2017 at 14:43
  • 1
    Btw, the validation error comes from the form component, right? Commented Aug 23, 2017 at 14:45
  • yes, i want to add in default value .. before i add in Builder .. code is something like this Commented Aug 23, 2017 at 14:49
  • ->add('issueDate', 'date', array('label'=>'issue Date','widget' => 'single_text', 'format' => 'dd/MM/yyyy')) Commented Aug 23, 2017 at 14:50
  • Inside entity . yes it is NotBlank () Commented Aug 23, 2017 at 14:55

1 Answer 1

1

You can either remove the NotBlank symfomy validator mapping from the two fields or use the empty_data option which will populate default values if the hidden fields weren't filled via JavaScript like this:

$builder->add('passDate', 'hidden', array(
  'data' => null,
  'empty_data' => '1950/01/01'
)
Sign up to request clarification or add additional context in comments.

9 Comments

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function format() on string" at /Applications/MAMP/htdocs/myProgramme/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateType.php line 53
Is there anyway to convert string to date in symfony 2.7?
I assumed your getters/setters are taking care of formatting because you pass a string to them in your question. You can either use a data transformer or just use 'empty_data' => new \DateTime('1950/01/01')
I did exactly what you guide me and still not going through, is there anything i am missed ?
my builder is ->add('passDate', 'hidden', array('data'=>null ,'empty_data' => new \DateTime(setPassDate('1950/01/01') )))
|

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.