Symfony2 -> Twig -> Form -> Field -> Set rendered = true
Am I missing the question here? If you want to set a field as rendered even though it is not the simple call is: {% do form.x.setRendered %} If I misunderstood, my apologies.
Am I missing the question here? If you want to set a field as rendered even though it is not the simple call is: {% do form.x.setRendered %} If I misunderstood, my apologies.
I’ve got it… <?php echo $view[‘form’]->label($form[‘first_name’], ‘First Name’, array( ‘label_attr’ => array(‘class’ => ‘control-label’), )) ?> Apparently “attr” is “label_attr” for labels fields. P.S. Corresponding twig code {{ form_label(form.first_name, ‘First Name’, { ‘label_attr’: {‘class’: ‘control-label’} }) }}
This post is not up to date with Symfony 2.3 read comments bellow A new version is comming ! Validating non mapped fields in Form (Symfony 2.1.2) This is a global response for some stackoverflow questions about current way to validate unbounded or non mapped field in forms. validation on unbound form field how to … Read more
An update for Symfony 2.1: property_path has been deprecated and instead you should use mapped. The syntax remains the same: ->add(‘password_confirmation’, ‘password’, array(‘mapped’ => false))
Thanks for your time! i resolved this myself: I removed parameter from NewsType constructor and added data to postedBy form field using $options array, and passed data to $options array from controller, please check following: NewsType public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add(‘postedBy’, HiddenType::class, array( ‘data’ => $options[‘postedBy’] ) ) ; } // … Read more
better way I found, is to use this kind of code {% if not form.vars.valid %} <div class=”alert alert-error”> {{ form_errors(form) }} </div> {% endif %}
You can define the default value from the ‘data’ attribute. This is part of the Abstract “field” type (http://symfony.com/doc/2.0/reference/forms/types/field.html) $form = $this->createFormBuilder() ->add(‘status’, ‘choice’, array( ‘choices’ => array( 0 => ‘Published’, 1 => ‘Draft’ ), ‘data’ => 1 )) ->getForm(); In this example, ‘Draft’ would be set as the default selected value.
ObjectManager is an interface and EntityManager is its ORM implementation. It’s not the only implementation; for example, DocumentManager from MongoDB ODM implements it as well. ObjectManager provides only the common subset of all its implementations. If you want your form type to work with any ObjectManager implementation, then use it. This way you could switch … Read more
You can do $form->get(‘locationRadius’)->addError(new FormError(‘error message’)); As form elements are also of FormInterface type.