I am having two entity files one as user.php and another as usertype.php. now i want to display a login form with 3 fields viz username, password and usertype. the usertype will be a selection that will fetch data from usertype table. here is the code that i wrote inside user.php to create a manytoone field for usertype_id
/**
* @ORM\ManyToOne(targetEntity="Usertype")
*/
protected $usertype;
Below is my Form generation Code
class LoginForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('login', 'text', array('label' => 'Username',));
$builder->add('password');
}
}
Now I need to add one more field to my form builder that will be a selection of usertype table.
...
use Acme\YourBundle\Entity\Usertype;
class LoginForm extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('usertype', 'entity',
array(
'class' => 'AcmeYourBundle:Usertype'
'label' => 'User Type',
)
);
}
}
You can read more informations about the entity field type wich will give you the options available for this type of field.
Don't forget to add a __toString() method to your model to tell the form builder what to display.
namespace Acme\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Usertype
{
public function __toString()
{
return $this->getName();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With