1

I need to implement form validation depending on submitted data. While data object's invoice property is true then validation_groups array should contain not only 'add' validation but also 'company'.

I've found "Groups based on Submitted Data" chapter in Symfony Docs https://github.com/symfony/symfony-docs/blob/master/book/forms.rst.

The problem is that :

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Strict\PublicBundle\Entity\Booking',
        'validation_groups' => function(FormInterface $form)
        {
              return array('booking');
        },
    );
}

throws this error:

Warning: Illegal offset type in /var/www/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php line 101 500 Internal Server Error - ErrorException

Any ideas what can be wrong?

3 Answers 3

3

According to this pull request using callbacks for validation_groups will be posible in Symfony 2.1 (not yet released, currently master branch).

Are you sure you are using master branch? If you are using current stable (2.0.x), it has no support for Groups based on Submitted Data, you have to use arrays only. See proper documentation on http://symfony.com/doc/current/book/forms.html#book-forms-validation-groups.

Sign up to request clarification or add additional context in comments.

Comments

2

I've got an alternative: If you're able to determine the condition prior to binding the form, you can simply override the default list of validation groups when you create the form.

In my case I've got an order object in session that gets updated across multiple form pages. Order can be "Delivery" or "Pickup" and if delivery is selected on a previous screen I need to validate address details on this screen:

if ($order->getOrderType() == "Delivery")
    {
        $validationGroups = array('step3', 'delivery');
    }
    else
    {
        $validationGroups = array('step3');
    }

    $formType = new Form\Order3Type();
    $form = $this->createForm($formType, $order, array("validation_groups" => $validationGroups));
    $form->bindRequest($request);

If your condition is in the form and not already in session, you could always just pull the value straight from the request object.

Comments

1
// MyFormType.php
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => 'Strict\PublicBundle\Entity\Booking',
        'validation_groups' => function (FormInterface $form) {
            $data = $form->getData();

            $groups = ['booking'];

            if ($data->invoice) {
                $groups[] = 'company';
            }

            return $groups;
        },
    ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.