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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:56:05+00:00 2026-06-12T11:56:05+00:00

I’m implementing a custom form type that provides an autocomplete field to select a

  • 0

I’m implementing a custom form type that provides an autocomplete field to select a location (country,city or spot). The form type creates two fields, one text field for the autocomplete search input and one hidden field to hold the selected id of the selected location.

When typing into the text field, a server call is made and results are displayed via jquery autocomplete. If a location is selected, the id of the selected location is written to the hidden field whereas the name of the location is displayed in the text field. On the server, I use a client transformer to lookup the entity of the id passed by the hidden field. The text field is ignored.

My model class defines a location field with a property to write back the location entity annotated with a NotNull validation constraint.

Everything works perfectly fine so far but if I do not select a location, the validation message “This value should not be null.” is displayed two times.

Double validation error

The bundle is public and can be found in my github repo. The relevant classes are the LocationFieldType and the LocationDataTransformer and the form theme.

And now for how I’m integrating the form type into my project. I added the whole code, sorry for the mass;)

In the model, I define the property as following:

class JourneyCreate
{

    /**
     * @Assert\NotNull()
     * @Assert\Choice(choices = {"offer", "request"})
     */
    public $type;

    /**
     * @Assert\NotNull()
     * @Assert\Date()
     */
    public $date;

    /**
     * @Assert\NotNull()
     * @Assert\Time()
     */
    public $time;

    /**
     * @Assert\NotNull()
     *
     */
    public $start;

    /**
     * @Assert\NotNull()
     *
     */
    public $destination;


    public function buildJourney(User $owner)
    {
    switch($this->type)
    {
        case 'offer':
            $journey = new JourneyOffer();
            break;
        case 'request':
            $journey = new JourneyRequest();
            break;
        default:
            throw new \InvalidArgumentException('Invalid journey type');
    }

    $journey->setDate($this->date);
    $journey->setTime($this->time);

    $journey->addStation(new JourneyStation($this->start));
    $journey->addStation(new JourneyStation($this->destination));

    $journey->setOwner($owner);

    return $journey;
    }
}

And in the main form I add the field as following:

    class JourneyCreateType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

    $builder
        ->add('type','choice', array(
            'choices'   => array(
                'offer'   => 'Driver',
                'request' => 'Passanger',
            ),
            'empty_value'=>'',
            'multiple'  => false,
            'expanded'  => true,
        ))
        ->add('date','date',array(
            'widget' => 'single_text',
            'format' => $this->getDateFormat(\IntlDateFormatter::TRADITIONAL),
        ))
        ->add('time','time',array(
            'widget' => 'single_text',
        ))
        ->add('start','room13_geo_location')
        ->add('destination','room13_geo_location')
    ;

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
    $resolver->setDefaults(array(
        'data_class' => 'Acme\DemoBundle\Form\Model\JourneyCreate',
    ));
    }


    public function getName()
    {
    return 'journey_create';
    }
}

And the controller code:

/**
* @Route("/create/{type}", defaults={"type" = null})
* @Template()
*/
public function createAction($type=null)
{

    if($type !== null && !in_array($type,array('request','offer')))
    {
        throw new NotFoundHttpException();
    }

    $journeyCreate = new JourneyCreate();
    $journeyCreate->type = $type;

    $form = $this->createForm(new JourneyCreateType(),$journeyCreate);

    if($this->isPost())
    {
        $form->bind($this->getRequest());

        if($form->isValid())
        {
          $journeyCreate = $form->getData();
          $journey = $journeyCreate->buildJourney($this->getCurrentUser());
          $this->persistAndFlush($journey);
          return $this->redirect($this->generateUrl('acme_demo_journey_edit',array('id'=>$journey->getId())));
        }
    }

    return array(
        'form'  => $form->createView(),
    );
}

And finaly the template code to display the form:

{% block page_body %}
    <form class="form-horizontal" action="{{ path('acme_demo_journey_create') }}" method="post" novalidate>
    {{form_widget(form)}}
    <div class="form-actions">
        <button class="btn btn-primary" type="submit">{{'form.submit'|trans}}</button>
        <a href="{{path("acme_demo_journey_index")}}" class="btn">{{'form.cancel'|trans}}</a>
    </div>
    </form>
{% endblock %}

I’m having the theory that this could be because I use two form fields but don’t know how to fix this. Any suggestions about how to solve this more elegant are welcome.

  • 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-12T11:56:06+00:00Added an answer on June 12, 2026 at 11:56 am

    As complicated as this question might look, the answer is as simple as removing the {{form_errors(form)}} from the widget template block. Because the *form_row* block looks like:

    {% block form_row %}
    {% spaceless %}
        <div class="form_row">
            {{ form_label(form) }}
            {{ form_errors(form) }}
            {{ form_widget(form) }}
        </div>
    {% endspaceless %}
    {% endblock form_row %}
    

    The error was simply outputted two times.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group

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.