Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:59:56+00:00 2026-05-27T10:59:56+00:00

I’m trying to get the collection prototype to have a set of default values

  • 0

I’m trying to get the collection prototype to have a set of default values instead of blank values. Ideally I’d like to be able to define those default somewhere either in the model class or the form definition class, but I cannot find a way to do this anywhere.

As an example:

I’ve created an AbstractType for my form which contains a nested collection of Person rows (relevant code shown below):

public function buildForm(FormBuilder $builder, array $options)
{
    ...
    $builder->add('people', 'collection', array(
            'type'         => new PersonType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype'    => true,
        ));
    ...
}

The PersonType class contains the following code:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('name', 'text');
    $builder->add('date_of_birth', 'date');
    $builder->add('age', 'number');

    // This would be great if I could do this but I can't:
    //$builder->add('date_of_birth', 'date', array('empty_value' => new \DateTime(...))); // some default value defined here
}

The best I’ve been able to come up with so far is shown below in the view file (the code shown is used to render the collection prototype):

...
<tr>
    <td> {{ form_widget(person.name) }} </td>

    {# THIS DOES NOT WORK (I just get the default selected date) #}
    <td> {{ form_widget(person.date_of_birth, {'value': person.date_of_birth.get('value')|default({'year':2010, 'month':10, 'day':15})} }} </td>

    {# THIS WORKS (the field contains '0' instead of being empty) #}
    <td> {{ form_widget(person.age, {'value': person.age.get('value')|default(0)} }} </td>
</tr>
...
  • It only seems to work with simple types like text and number. It doesn’t work with the date type.
  • This anyway doesn’t feel like the right approach. I should be able to define a default/empty value either in the underlying model (e.g. protected $age = 10; inside the model class), or else in the form definition (AbstractType) class (e.g. array(’empty_value’ => new DateTime()), but neither are currently possible.

So in summary, my question is:

How can I define default values for a model class that will be set automatically on the client when adding new items to a form ‘collection’ (instead of just getting blanks).

Does anyone know of a good way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:59:56+00:00Added an answer on May 27, 2026 at 10:59 am

    In the constructor of the entity that the form is being used for, you simply set the date with a \DateTime object, like so:

    class MyEntity {
        private $myDate;
    
        public function __construct() {
            $this->myDate = new \DateTime('today'); 
        }
    }
    

    You can also use \DateTime(‘now’) or \DateTime(‘tomorrow’), as described in the discussion below

    http://groups.google.com/group/symfony2/browse_thread/thread/18a5b20aca485dc4/e9947d0f06d6519d

    Edit: Actually, this information is in the symfony2 documentation:

    http://symfony.com/doc/2.0/book/forms.html#building-the-form

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.