Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation errors are displayed only at the top of the form #1

Open
cve opened this issue Jun 6, 2014 · 7 comments

Comments

Projects
None yet
3 participants
@cve
Copy link

commented Jun 6, 2014

Hi,

when I test this bundle, I see that the validation errors for fields are displayed at the top of the form instead next of invalid field

@cve cve changed the title Validation errors are displayed only on the top of form Validation errors are displayed only at the top of form Jun 6, 2014

@adamquaile

This comment has been minimized.

Copy link
Owner

commented Jun 6, 2014

Could you link to an example? I've definitely had this working for me.

@cve

This comment has been minimized.

Copy link
Author

commented Jun 6, 2014

Sorry, but I test it locally:

Registration entity:


namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Registration
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Registration
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="first_name", type="string")
     * @Assert\NotBlank()
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(name="last_name", type="string")
     * @Assert\NotBlank()
     */
    private $lastName;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Gets the value of firstName.
     *
     * @return string
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Sets the value of firstName.
     *
     * @param string $firstName the first name
     *
     * @return self
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * Gets the value of lastName.
     *
     * @return string
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Sets the value of lastName.
     *
     * @param string $lastName the last name
     *
     * @return self
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }
}

RegistrationType:


namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('registration', 'fieldset', array(
                'label' => false,
                'legend' => 'Rejestracja',
                'fields' => array(
                    array(
                        'name' => 'first_name',
                        'type' => 'text',
                        'attr' => array(
                            'label' => 'First name'
                        )
                    ),
                    array(
                        'name' => 'last_name',
                        'type' => 'text',
                        'attr' => array(
                            'label' => 'Last name'
                        )
                    )
                )
            ))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Registration'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'acme_demobundle_registration';
    }
}

Registration new.html.twig:


{% block body -%}
    <h1>Registration creation</h1>

    {{ form(form) }}

        <ul class="record_actions">
    <li>
        <a href="{{ path('registration') }}">
            Back to the list
        </a>
    </li>
</ul>
{% endblock %}

All of code is symfony-standard edition with basic code for registration from entity and crud generator

@adamquaile

This comment has been minimized.

Copy link
Owner

commented Jun 6, 2014

Thanks! I'll try that when I get home and let you know.

@cve cve changed the title Validation errors are displayed only at the top of form Validation errors are displayed only at the top of the form Jun 6, 2014

@cve

This comment has been minimized.

Copy link
Author

commented Jun 6, 2014

Ok, Thanks

@adamquaile

This comment has been minimized.

Copy link
Owner

commented Jun 7, 2014

When I follow those steps, I do get the error at the top. I just have to figure out what the difference is between that and my projects. If you figure it out in the meantime, let me know.

Thanks.

@ABM-Dan

This comment has been minimized.

Copy link
Contributor

commented Dec 17, 2015

I think I might have just stumbled in the same/similar problem, When running

$form = new Form();
$errors = $form->getFormErrors($this->formContactInformation);
$return = array('errors' => $errors);
return new JsonResponse($return);

I get

{
  "errors": {
    "account_information].children[phone_number": "Phone Number should not be blank.",
    "physical_address].children[address": "Address should not be blank.",
    "physical_address].children[city": "City should not be blank.",
    "physical_address].children[state_id": "State/Region/Province should not be blank.",
    "physical_address].children[zip": "Zip Code should not be blank."
  }
}

Not sure if it's the same issue or a new one. physical_address is the name of the fieldset by the way.

@ABM-Dan

This comment has been minimized.

Copy link
Contributor

commented Dec 17, 2015

Solved my version of this problem by forcing error bubbling by default on all elements. But in my case I WANTED them to be in the form element. Hopefully this was a useful data point none the less.

adamquaile pushed a commit that referenced this issue Oct 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.