How to set form field name in Symfony 2?

Asked
Active3 hr before
Viewed126 times

4 Answers

field
90%

already has test as name. Of course there is still the root form name called form in your case. Removing this is not really recommended, because when you read the request to populate form data you can identify the form by its form name.,The change is just moving name={{full_name}} from the first line to the last one, so when you add the name attr to the form builder add element, the name will be no longer ignored.,How to set name attribute of rendered field in symfony 2?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

<input type="text" name="form[test]" value="test" />
load more v
88%

When you create a form, each field initially displays the value of the corresponding property of the form's domain data (e.g. if you bind an object to the form). If you want to override this initial value for the form or an individual field, you can set it in the data option:,Fields are rendered in the same order as they are included in the form. This option changes the field rendering priority, allowing you to display fields earlier or later than their original order.,With customized error mapping, you can do better: map the error to the city field so that it displays above it:,The FormType predefines a couple of options that are then available on all types for which FormType is the parent.

1
2
# replace 'FooType'
by the class name of your form type
$ php bin / console debug: form FooType
load more v
72%

php app/console doctrine:generate:entity ,php app/console doctrine:generate:form AppBundle:Employee --no-interaction,This can be done via the simple console command that generates the entity through interactive mode. The console asks for the entity name and for the name and type of each field. ,If it is needed to make some customisation and styling on the elements, each element can be separately inserted in the twig file.

< ? php
// file: AppBundle/Entity/Employee.php

namespace AppBundle\ Entity;

use AppBundle\ Entity\ Role;
use Doctrine\ ORM\ Mapping as ORM;
use Doctrine\ Common\ Collections\ ArrayCollection;

/**
 * Employee entity.
 *
 * @ORM\Table(name="employee")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\EmployeeRepository")
 */
class Employee extends Person {
   /**
    * @var ArrayCollection
    *
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Role")
    * @ORM\JoinTable(name="employees_roles",
    *    joinColumns={@ORM\JoinColumn(name="employee_id", referencedColumnName="id")},
    *    inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
    * )
    */
   protected $roles;

   /**
    * @var string
    *
    * @ORM\Column(type="string", length=255, nullable=true)
    */
   protected $salary;

   /**
    * Constructor.
    */
   public
   function __construct() {
      parent::__construct();
      $this - > roles = new ArrayCollection();
   }

   /**
    * Get roles as array of strings.
    *
    * @return array
    */
   public
   function getRoles() {
      $roleNames = [];
      foreach($this - > roles as $role) {
         $roleNames[] = $role - > getName();
      }
      return $roleNames;
   }

   /**
    * Get roles ass ArrayCollection.
    *
    * @return ArrayCollection
    */
   public
   function getRolesCollection() {
      return $this - > roles;
   }

   /**
    * Returns TRUE if employee has ROLE_ADMIN.
    *
    * @return bool
    */
   public
   function isAdmin() {
      return in_array('ROLE_ADMIN', $this - > getRoles(), TRUE);
   }

   /**
    * Set roles.
    *
    * @param ArrayCollection $roles
    * @return Employee
    */
   public
   function setRolesCollection($roles) {
      $this - > roles = $roles;
      return $this;
   }

   /**
    * Add roles
    *
    * @param Role $roles
    * @return Employee
    */
   public
   function addRolesCollection(Role $roles) {
      $this - > roles[] = $roles;

      return $this;
   }

   /**
    * Remove roles
    *
    * @param Role $roles
    */
   public
   function removeRolesCollection(Role $roles) {
      $this - > roles - > removeElement($roles);
   }

   /**
    * Set salary
    *
    * @param string $salary
    * @return Employee
    */
   public
   function setSalary($salary) {
      $this - > salary = $salary;

      return $this;
   }

   /**
    * Get salary
    *
    * @return string
    */
   public
   function getSalary() {
      return $this - > salary;
   }
}
load more v
65%

Form Events When you create the form you set default data when creating the fields. You override this use PreSetData event listener. The problem with this is that you're duplicating the form workload / duplicating code and making it harder to maintain / understand.,If you need to set default value and your form relates to the entity, then you should use following approach:,If you store it at the form level, is that obvious when you come to maintain the code later?,You can set the default for related field in your model class (in mapping definition or set the value yourself).

Can be use during the creation easily with :

- > add('myfield', 'text', array(
   'label' => 'Field',
   'empty_data' => 'Default value'
))
load more v

Other "field-undefined" queries related to "How to set form field name in Symfony 2?"