Php Symfony2:$form->;isValid()始终返回true
我创建了一个表单类型UserBundle/form/UserType.phpPhp Symfony2:$form->;isValid()始终返回true,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,我创建了一个表单类型UserBundle/form/UserType.php $form->isValid()如果用户名或电子邮件存在,即使其他字段为空,也返回true class UserType extends AbstractType { private $type; const TYPE_CREATE = 1; public function __construct($type) { $this->type = $type; }
$form->isValid()
如果用户名或电子邮件存在,即使其他字段为空,也返回true
class UserType extends AbstractType
{
private $type;
const TYPE_CREATE = 1;
public function __construct($type) {
$this->type = $type;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
switch ($this->type){
case self::TYPE_CREATE :
$builder
->add('email', 'email', array('label' => 'user.email'))
->add('username', null, array('label' => 'user.name'))
;
break;
default :
$builder
->add('username')
->add('usernameCanonical')
->add('email')
->add('emailCanonical')
->add('enabled')
->add('salt')
->add('password')
->add('lastLogin')
->add('locked')
->add('expired')
->add('expiresAt')
->add('confirmationToken')
->add('passwordRequestedAt')
->add('roles')
->add('credentialsExpired')
->add('credentialsExpireAt')
;
break;
}
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\UserBundle\Entity\User'
));
}
public function getName()
{
return 'project_userbundle_user';
}
}
还有我的控制器
class UserController extends Controller
{
public function CreateAction()
{
//create user
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
//create form
$form = $this->createForm(new UserType(UserType::TYPE_CREATE),$user);
//form submit
$request = Request::createFromGlobals();
if($request->getMethod() === "POST"){
$form->submit($request);
//test if form is valid
if($form->isValid()){
//generate random password
$password = User::randomPassword();
$user->setPassword($password);
//save user
$userManager->updateUser($user);
}
}
return $this->render('Project:User:create.html.twig',array(
'form' => $form->createView()
));
}
}
必须使用验证约束NotNull或NotBlank()
我的猜测是,字段上的默认“required=true”不起作用,因为您正在重新创建请求。您必须使用验证约束NotNull或NotBlank()
我的猜测是,字段上的默认“required=true”不起作用,因为您正在重新创建请求。您必须使用验证约束NotNull或NotBlank()
我的猜测是,字段上的默认“required=true”不起作用,因为您正在重新创建请求。您必须使用验证约束NotNull或NotBlank() 我的猜测是,字段上的默认“required=true”不起作用,因为您正在重新创建请求。提示: 您不应该尝试在控制器内重新创建
请求
。这无疑是一种不好的做法,可能会导致应用程序出现不希望出现的行为—正如您当前所经历的那样
如果将请求作为方法参数添加,symfony将自动将请求传递给控制器操作:
public function createAction(Request $request)
{
// $request is holding the current request
}
此外,您还可以像控制器一样从containerware
类中的容器获取当前请求
$request = $this->getRequest();
$request = $this->get('request');
$request = $this->container->get('request');
此外,请继续使用。您的代码中存在多个冲突 方法名称应为小写的camelCased,而不是camelCased
UserBundle/form
应该是UserBundle/form
方法开头的花括号/大括号属于下一行
public function __construct($type) // {
{
$this->type = $type;
}
提示:
您不应该尝试在控制器内重新创建请求
。这无疑是一种不好的做法,可能会导致应用程序出现不希望出现的行为—正如您当前所经历的那样
如果将请求作为方法参数添加,symfony将自动将请求传递给控制器操作:
public function createAction(Request $request)
{
// $request is holding the current request
}
此外,您还可以像控制器一样从containerware
类中的容器获取当前请求
$request = $this->getRequest();
$request = $this->get('request');
$request = $this->container->get('request');
此外,请继续使用。您的代码中存在多个冲突 方法名称应为小写的camelCased,而不是camelCased
UserBundle/form
应该是UserBundle/form
方法开头的花括号/大括号属于下一行
public function __construct($type) // {
{
$this->type = $type;
}
提示:
您不应该尝试在控制器内重新创建请求
。这无疑是一种不好的做法,可能会导致应用程序出现不希望出现的行为—正如您当前所经历的那样
如果将请求作为方法参数添加,symfony将自动将请求传递给控制器操作:
public function createAction(Request $request)
{
// $request is holding the current request
}
此外,您还可以像控制器一样从containerware
类中的容器获取当前请求
$request = $this->getRequest();
$request = $this->get('request');
$request = $this->container->get('request');
此外,请继续使用。您的代码中存在多个冲突 方法名称应为小写的camelCased,而不是camelCased
UserBundle/form
应该是UserBundle/form
方法开头的花括号/大括号属于下一行
public function __construct($type) // {
{
$this->type = $type;
}
提示:
您不应该尝试在控制器内重新创建请求
。这无疑是一种不好的做法,可能会导致应用程序出现不希望出现的行为—正如您当前所经历的那样
如果将请求作为方法参数添加,symfony将自动将请求传递给控制器操作:
public function createAction(Request $request)
{
// $request is holding the current request
}
此外,您还可以像控制器一样从containerware
类中的容器获取当前请求
$request = $this->getRequest();
$request = $this->get('request');
$request = $this->container->get('request');
此外,请继续使用。您的代码中存在多个冲突 方法名称应为小写的camelCased,而不是camelCased
UserBundle/form
应该是UserBundle/form
方法开头的花括号/大括号属于下一行
public function __construct($type) // {
{
$this->type = $type;
}
为什么要创建请求对象???在方法参数中询问它有什么错?或者从
$this->getRequest()
获取它?或者从$this->get('request')
?$form->handleRequest($request)在哪里?为什么要创建请求对象???在方法参数中询问它有什么错?或者从$this->getRequest()
获取它?或者从$this->get('request')
?$form->handleRequest($request)在哪里?为什么要创建请求对象???在方法参数中询问它有什么错?或者从$this->getRequest()
获取它?或者从$this->get('request')
?$form->handleRequest($request)在哪里?为什么要创建请求对象???在方法参数中询问它有什么错?或者从$this->getRequest()
获取它?或者从$this->get('request')
?哪里是$form->handleRequest($request)