我有一个EmployeeBasicInfoEntity,其雇员字段使用注释具有一些验证约束。我正在使用此实体进行员工注册。这是我的EmployeeBasicInfoType
<?php
namespace Benerite\EmployeeBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EmployeeBasicInfoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('company', 'entity', array(
'class' => 'BeneriteCompanyBundle:Company',
'choice_label' => 'companyName',
'placeholder' => 'Please select',
))
->add('employeeIdentificationCode')
->add('employeePassword', 'password')
->add('employeeFirstName')
->add('employeeMiddleName')
->add('employeeLastName')
->add('employeeDob','date',array(
'years' => range(date('Y'),date('Y',strtotime('-50 years')) ),
'required' => True,
)
)
->add('employeeGender', 'choice', array(
'choices' => array('m' => 'Male', 'f' => 'Female', 't' => 'Transgender', 'na' => 'Do not want to disclose'),
'expanded' => true,
'multiple' => false, 'required' => true, 'data' => 'na',
)
)
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Benerite\EmployeeBundle\Entity\EmployeeBasicInfo'
));
}
/**
* @return string
*/
public function getName()
{
return 'benerite_employeebundle_employeebasicinfo';
}
}
我使用相同的表单类型创建了一个用户登录控制器,我不希望显示特定的字段。我在控制器中做了一些改动。请检查我的控制器
<?php
namespace Benerite\EmployeeBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Benerite\EmployeeBundle\Entity\EmployeeBasicInfo;
use Benerite\EmployeeBundle\Form\EmployeeBasicInfoType;
/**
* User controller.
*
* @Route("/user")
*/
class UserController extends Controller
{
/**
* Creates a new Company entity.
*
* @Route("/login", name="user_login")
* @Method("POST|GET")
* @Template("BeneriteEmployeeBundle:User:login.html.twig")
*/
public function loginAction(Request $request)
{
$entity = new EmployeeBasicInfo();
$form = $this->createForm(new EmployeeBasicInfoType(), $entity, array(
'action' => $this->generateUrl('user_login'),
'method' => 'POST',
));
$form->remove('company')
->remove('employeeFirstName')
->remove('employeeMiddleName')
->remove('employeeLastName')
->remove('employeeDob')
->remove('employeeGender');
$form->add('submit', 'submit', array('label' => 'Log In'));
$form->handleRequest($request);
if ($form->isValid()) {
}
return array(
'login_form'=>$form->createView()
);
}
}
使用这段代码,我可以删除字段。但是form-&gt; isValid()总是返回false。请告诉我我哪里出错了。这是正确的做法吗?
答案 0 :(得分:0)
对于登录操作,我认为您不必使用FormType。例如,这就是我loginAction()
的样子,你会注意到没有使用任何表格类。
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'UserBundle:Default:login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
这是我的login.html.twig
<form action="{{ path('login_check') }}" method="post" class="form-horizontal transparent-bg">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username:</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="username" name="_username"
value="{{ last_username }}"/>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input class="form-control" type="password" id="password" name="_password"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"
>
<button type="submit" class="btn btn-success">login</button>
</div>
</div>
</form>
这里的关键是
用户名必须具有名称_username,密码必须具有 名称_password。 Reference
这就是创建登录表单所需的全部内容。