AdamQuaileFieldsetBundle icon indicating copy to clipboard operation
AdamQuaileFieldsetBundle copied to clipboard

Validation errors are displayed only at the top of the form

Open cve opened this issue 10 years ago • 7 comments

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 avatar Jun 06 '14 09:06 cve

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

adamquaile avatar Jun 06 '14 10:06 adamquaile

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

cve avatar Jun 06 '14 10:06 cve

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

adamquaile avatar Jun 06 '14 10:06 adamquaile

Ok, Thanks

cve avatar Jun 06 '14 10:06 cve

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.

adamquaile avatar Jun 07 '14 18:06 adamquaile

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 avatar Dec 17 '15 23:12 ABM-Dan

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.

ABM-Dan avatar Dec 17 '15 23:12 ABM-Dan