hous
hous

Reputation: 174

Expected an array

I have a script to add and edit users, adding works fine but the edit page shows me this error:

Expected an array. 500 Internal Server Error - TransformationFailedException

UserType.php

namespace Store\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', 'text')
        ->add('password', 'text')
        ->add('salt', 'text', array('required' => false))
        ->add('roles', 'choice', array(
        'choices'  => array(
            'ROLE_ADMIN'      => 'ROLE_ADMIN',
            'ROLE_USER'       => 'ROLE_USER',
            'ROLE_AUTEUR'     => 'ROLE_AUTEUR',
            'ROLE_MODERATEUR' => 'ROLE_MODERATEUR',
                ),
        'required'    => false,
        'empty_value' => 'Choisissez un ou plusieurs roles',
        'empty_data'  => null,
        'multiple' => true,
        'expanded' => false,
        ));
}   

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Store\UserBundle\Entity\User'
    ));
}

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

UserEditType.php

<?php

namespace Store\UserBundle\Form;

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserEditType extends UserType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options) ;
    $builder->remove('roles') ;
}   

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

Controller

<?php
//...

// modifier
public function modifierAction(User $user)
{
    $em = $this->getDoctrine()->getManager() ;

    $form = $this->createForm(new UserEditType, $user) ;

    $request = $this->get('request');
    if ($request->getMethod() == 'POST')
    {
        $form->bind($request) ;
        if ($form->isValid())
        {
            $em->flush();

            $this->get('session')->getFlashBag()->add('info', 'User bien modifie');

            return $this->redirect( $this->generateUrl('store_produit_index') );
        }
    }

    return $this->render('StoreUserBundle:Security:modifier.html.twig',
    array(
        'form' => $form->createView() ,
        ));
}

//..

?>

form.html.twig

<div class="well">
<form action="" method="post" {{ form_enctype(form) }}>
    <span style="color:red">{{ form_errors(form) }}</span>

        {{ form_widget(form) }}

    <input type="submit" class="btn btn-primary" />
</form>
</div>

If I add $builder-> remove ('roles'); in UserEditType.php , it works but without changing roles in database.

User.php

<?php

namespace Store\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

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

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255, unique=true)
 */
private $username;

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

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

/**
 * @var array
 *
 * @ORM\Column(name="roles", type="array")
 */
private $roles;

public function __construct()
{
    $this->roles = array();
}

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

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

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set roles
 *
 * @param array $roles
 * @return User
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

/**
 * Get roles
 *
 * @return array 
 */
public function getRoles()
{
    return $this->roles;
}

public function eraseCredentials()
{
}

}

Upvotes: 1

Views: 4086

Answers (2)

Zaheer Babar
Zaheer Babar

Reputation: 1676

Form class is expecting that entity will return an array because you are using multiple choices. use type array in .orm file and generate entity again and update schema. Like

roles:
    type: array

Upvotes: 0

loicfavory
loicfavory

Reputation: 837

Could you try to unserialize the returned value of getRoles ?

/**
 * Get roles
 *
 * @return array 
 */
public function getRoles()
{
    return unserialize($this->roles);
}

Upvotes: 1

Related Questions