In Symfony2, if you’re using AbstractType to generate web forms, you may need to generate errors specific to one of the form’s input elements. Below is an example app with a warning message added to the session FlashBag if a resource doesn’t exist when the checkbox is ticked. The focus will be on MyController.php line 24 and its call to $form->get('yourCheckbox')->getConfig()->getOption('label') so you don’t have to hard-code the label in a controller action method.

This source code assumes you’re using Doctrine as an ORM and the Twig templating language. I’ve omitting Symfony2 routing, bundle, and app configurations.

src/MyBundle/Form/MyClass.php

add('myCheckbox', 'checkbox',
            array(
                'label' => 'Check to enable resource',
                'required' => false
            )
        );
    }

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

src/MyBundle/Entity/MyModel.php

id;
    }

    /**
     * Set myCheckbox
     *
     * @param boolean $myCheckbox
     * @return MyModel
     */
    public function setMyCheckbox($myCheckbox) {
        $this->myCheckbox= $myCheckbox;

        return $this;
    }

    /**
     * Get myCheckbox
     *
     * @return boolean 
     */
    public function getMyCheckbox() {
        return $this->myCheckbox;
    }
}

src/MyBundle/Resources/views/modify.html.twig

{% for flashMessage in app.session.flashbag.get('warning') %} {% endfor %}
{{ form_widget(form.myCheckbox) }}
{{ form_errors(form.myCheckbox) }} {{ form_label(form.myCheckbox) }}


src/MyBundle/Controller/MyController.php

 0) {
            $myObject = $this->getDoctrine()
                ->getRepository('MyBundle:MyModel')
                ->find($id);
        } else {
            $myObject = new MyModel();
        }

        $form = $this->createForm(new MyForm(), $myObject);

        if($myOjbect->getMyCheckbox() && !$this->_isResourceUploaded($myObject)) {
            $message = '"' .
                $form->get('myCheckbox')->getConfig()->getOption('label') .
                '" is ticked but has no resources assigned. Please re-upload.';
            $this->get('session')->getFlashBag()->add('warning', $message);
        }

        return $this->render('MyBundle:modify.html.twig',
            array('form' => $form->createView(), 'obj' => $myObject));
    }

    protected function _isResourceUploaded(MyModel $myObject) {
       // TODO insert business logic to validate resource for this object.
       // Ideally this resides in a helper class.
       return false;
    }
}

When visiting the above action method and _isResourceUploaded() returns false, the view will display:

Warning “Check to enable resource” is ticked but has no resources assigned. Please re-upload.