Foros del Web » Programando para Internet » PHP » Symfony »

[SOLUCIONADO] Symfony 2.4.1:This form should not contain extra fields

Estas en el tema de Symfony 2.4.1:This form should not contain extra fields en el foro de Symfony en Foros del Web. Que tal amigos de foros del web .Estoy aprendiendo Symfony y estoy tratando de crear un login. ..Cree mi propio bundle y genero bien mis ...
  #1 (permalink)  
Antiguo 03/02/2014, 15:01
Avatar de koloinotzente  
Fecha de Ingreso: febrero-2012
Ubicación: Panama
Mensajes: 4
Antigüedad: 12 años, 2 meses
Puntos: 0
Pregunta Symfony 2.4.1:This form should not contain extra fields

Que tal amigos de foros del web .Estoy aprendiendo Symfony y estoy tratando de crear un login. ..Cree mi propio bundle y genero bien mis cruds pero al tratar de ingresar un usuario nuevo en el formulario de usuarios me sale este mensaje:
This form should not contain extra fields
y no me deja guardar. alguna idea de que pueda ser esto?

adjunto el codigo de lo que tengo , muchas gracias de antemano.


Entidad
Código:
   

/**
  * @ORM\Entity
  * @ORM\Table(name="admin_user")
  */
class User implements UserInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * @ORM\Column(type="string", length=255)
    */
    protected $username;

    /**
     * @ORM\Column(name="password", type="string", length=255)
     */
    protected $password;

    /**
     * @ORM\Column(name="salt", type="string", length=255)
     */
    protected $salt;

    /**
     * se utilizó user_roles para no hacer conflicto al aplicar ->toArray en getRoles()
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="user_role",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $user_roles;

    public function __construct()
    {
        $this->user_roles = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * Get salt
     *
     * @return string
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Add user_roles
     *
     * @param Login\LoginBundle\Entity\Role $userRoles
     */
    public function addRole(\Login\LoginBundle\Entity\Role $userRoles)
    {
        $this->user_roles[] = $userRoles;
    }

    public function setUserRoles($roles) {
        $this->user_roles = $roles;
    }

    /**
     * Get user_roles
     *
     * @return Doctrine\Common\Collections\Collection
     */
    public function getUserRoles()
    {
        return $this->user_roles;
    }

    /**
     * Get roles
     *
     * @return Doctrine\Common\Collections\Collection
     */
    public function getRoles()
    {
        return $this->user_roles->toArray(); //IMPORTANTE: el mecanismo de seguridad de Sf2 requiere ésto como un array
    }

    /**
     * Compares this user to another to determine if they are the same.
     *
     * @param UserInterface $user The user
     * @return boolean True if equal, false othwerwise.
     */
    public function equals(UserInterface $user) {
        return md5($this->getUsername()) == md5($user->getUsername());

    }

    /**
     * Erases the user credentials.
     */
    public function eraseCredentials() {

    }

    /**
     * Add user_roles
     *
     * @param \Login\LoginBundle\Entity\Role $userRoles
     * @return User
     */
    public function addUserRole(\Login\LoginBundle\Entity\Role $userRoles)
    {
        $this->user_roles[] = $userRoles;

        return $this;
    }

    /**
     * Remove user_roles
     *
     * @param \Login\LoginBundle\Entity\Role $userRoles
     */
    public function removeUserRole(\Login\LoginBundle\Entity\Role $userRoles)
    {
        $this->user_roles->removeElement($userRoles);
    }
}
Metodos Usercontroller.php donde crea la forma que inserta

Código:
 private function createCreateForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('admin_user_create'),
            'method' => 'POST',
           
            
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new User entity.
     *
     * @Route("/new", name="admin_user_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new User();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }
forma :

Código:
class UserType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username')
            ->add('password')
            //->add('salt') //No necesitamos que salt sea mostrado ------------
            ->add('user_roles')
         
        ;   
        
        
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Login\LoginBundle\Entity\User'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'login_loginbundle_user';
    }
}
  #2 (permalink)  
Antiguo 03/02/2014, 16:03
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Symfony 2.4.1:This form should not contain extra fields

Googleaste el error ?, en cualquier caso postea como lo estas rendereando y comprueba lo siguente http://symfony.com/doc/current/cookb...-the-prototype

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)
  #3 (permalink)  
Antiguo 04/02/2014, 13:47
Avatar de koloinotzente  
Fecha de Ingreso: febrero-2012
Ubicación: Panama
Mensajes: 4
Antigüedad: 12 años, 2 meses
Puntos: 0
Respuesta: Symfony 2.4.1:This form should not contain extra fields

Si Ya habia googleado el problema, era puntual en mi controlador: se debia llamar al metodo createcreateform en vez de createform.

Código:
    public function createAction()
    {
        $entity  = new User();
        $request = $this->getRequest();
        //$form    = $this->createForm(new UserType(), $entity);
         // --------------
        $form = $this->createCreateForm($entity);
        $form->submit($request);
        
      

/*
*/
        if ($form->isValid()) {
            //establecemos la contraseña: --------------------------
            $this->setSecurePassword($entity);

            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('admin_user_show', array('id' => $entity->getId())));

        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView()
        );
    }

Etiquetas: extra, fields, form
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:55.