简体   繁体   中英

Symfony2 form with field not in entity

this may be very easy but i'm new to symfony2, so i thought asking first. i'm creating a login form in a controller:

public function showAction()
{
    $admin = new Administrator();

$form = $this->createFormBuilder($admin)->setAction($this->generateUrl('admin_login_process'))
                 ->setMethod('POST')
                 ->add('username', 'text')
                 ->add('password', 'password')
                 ->add('remember', 'checkbox')
                 ->add('login', 'submit')
                 ->getForm();

    return $this->render('EraAdminBundle:Login:login.html.php', array('form'=>$form->createView()));
}

the username and password fields are part of the administrator entity, but the remember checkbox is not of course. how do i submit it together with the form? cause if i let it as it is i get this error:

Neither the property "remember" nor one of the methods "getRemember()", "isRemember()", "hasRemember()", "__get()" or "__call()" exist and have public access in class "Era\RestoranteBundle\Entity\Administrator".

In reading the symfony 2 doc : http://symfony.com/doc/current/book/forms.html

In your field (in formType), you should add the option 'mapped' to 'false'

$builder->add('task')
    ->add('dueDate', null, array('mapped' => false))
    ->add('save', 'submit');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM