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 9127223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:14:03+00:00 2026-06-17T07:14:03+00:00

Okay, I’ve got the FormType described below. I use this Form class for both

  • 0

Okay, I’ve got the FormType described below. I use this Form class for both the create and edit forms. I decided to set a default date (from_date and to_date below), using the data attribute in the options array. This does a great job of setting the default date, in fact, too good a job. It also overrides the existing date in the edit form, which is no good at all, really.

How do I set a real ‘default’ value, as opposed to an ‘always’ value?

<?php

namespace TechPeople\InvoiceBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\SecurityContext;


class InvoiceType extends AbstractType
{
    private $user;

    public function __construct(SecurityContext $security_context)
    {
        $this->user = $security_context->getToken()->getUser();
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $lastMonth = new \DateTime();$lastMonth->modify('-1 month');
        $builder
            ->add('month', 'choice', array(
                'data' => $lastMonth->format('F'),
                'choices' => array(
                    "January" => "January",
                    "February" => "February",
                    "March" => "March",
                    "April" => "April",
                    "May" => "May",
                    "June" => "June",
                    "July" => "July",
                    "August" => "August",
                    "September" => "September",
                    "October" => "October",
                    "Novemeber" => "Novemeber",
                    "December" => "December",
                )
            ))
            ->add('year', null, array(
                'data' => $lastMonth->format('Y')
            ))
            ->add('from_date', 'date', array(
                 'label' => 'From',
                 'data' => new \DateTime(),
            ))
            ->add('to_date', 'date', array(
                 'label' => 'To',
                 //'data' => new \DateTime(),
            ))
            ->add('hours')
            ->add('expenses')
            ->add('expense_amount', 'money',
                array(
                    'required' => false,
                ))
            ->add('attachment', 'file',
                array(
                    'path'=>$options['data']->getAttachmentPath(),
                    'required' => false,
                )
            )
        ;
        if($this->user->hasRole('ROLE_ADMIN')){
            $builder->add('vendor');
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TechPeople\InvoiceBundle\Entity\Invoice'
        ));
    }

    public function getName()
    {
        return 'techpeople_invoicebundle_invoicetype';
    }
}
  • 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-06-17T07:14:05+00:00Added an answer on June 17, 2026 at 7:14 am

    Set it on the entity, or any object you will use as form data, in the constructor or as default value !

    class Invoice {
    
        private $month;
        private $year;
        private $from_date;
        private $to_date;
        //...
    
        public function __construct()
        {
            $lastMonth = new \DateTime('now - 1 month');
            $this->month = $lastMonth->format('F');
            $this->year = $lastMonth->format('Y');
            $this->from_date = new \DateTime;
            $this->to_date = new \DateTime;
            //...
        }
    }
    

    It will set up these 2 fields for a creation form, and in the case of a persisted entity, these values will be overriden by stored data at loading.

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

Sidebar

Related Questions

Okay, this is the case: I got a generic base-class which I need to
Okay, so I've got something as simple as this: <audio autoplay='autoplay' loop='loop' id='audio' controls>
Okay, I really know this has GOT to be the long way around doing
Okay, this might be a very silly beginner question, but: I've got an ClassA,
Okay, so if you look at this site in Internet Explorer 7 (I use
Okay so I have to create a radix sort for both unsigned ints and
Okay, first some background, I can't use any javascript library except YUI for this
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess

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.