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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:54:25+00:00 2026-06-08T12:54:25+00:00

I’ve got a simple model (simplified of source): class Collection { public $page; public

  • 0

I’ve got a simple model (simplified of source):

class Collection
{
    public $page;
    public $limit;
}

And a form type:

class CollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('page', 'integer');
        $builder->add('limit', 'integer');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FSC\Common\Rest\Form\Model\Collection',
        ));
    }
}

My controller:

public function getUsersAction(Request $request)
{
    $collection = new Collection();
    $collection->page = 1;
    $collection->limit = 10;

    $form = $this->createForm(new CollectionType(), $collection)
    $form->bind($request);

    print_r($collection);exit;
}

When i POST /users/?form[page]=2&form[limit]=20, the response is what i expect:

Collection Object
(
    [page:public] => 2
    [limit:public] => 20
)

Now, when i POST /users/?form[page]=3, the response is:

Collection Object
(
    [page:public] => 3
    [limit:public] =>
)

limit becomes null, because it was not submitted.

I wanted to get

Collection Object
(
    [page:public] => 3
    [limit:public] => 10 // The default value, set before the bind
)

Question: How can i change the form behaviour, so that it ignores non submitted values ?

  • 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-08T12:54:28+00:00Added an answer on June 8, 2026 at 12:54 pm

    If is only a problem of parameters (GET parameters) you can define the default value into routing file

    route_name:
    pattern: /users/?form[page]={page}&form[limit]={limit}
    defaults: { _controller: CompanyNameBundleName:ControllerName:ActionName, 
                             limit:10 }
    

    An alternative way could be to use a hook (i.e. PRE_BIND) and update manually that value into this event. In that way you haven’t the “logic” spreaded into multi pieces of code.

    Final code – suggested by Adrien – will be

    <?php
    
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormFactoryInterface;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\Form\FormEvents;
    
    class IgnoreNonSubmittedFieldSubscriber implements EventSubscriberInterface
    {
        private $factory;
    
        public function __construct(FormFactoryInterface $factory)
        {
            $this->factory = $factory;
        }
    
        public static function getSubscribedEvents()
        {
            return array(FormEvents::PRE_BIND => 'preBind');
        }
    
        public function preBind(FormEvent $event)
        {
            $submittedData = $event->getData();
            $form = $event->getForm();
    
            // We remove every child that has no data to bind, to avoid "overriding" the form default data
            foreach ($form->all() as $name => $child) {
                if (!isset($submittedData[$name])) {
                    $form->remove($name);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.