You have to use Validation Constraints NotNull or NotBlank ( http://symfony.com/doc/current/reference/constraints.html ).
My guess is that, default "required=true" on fields isn't working because You are recreating request.
Question
I create a form type UserBundle/form/UserType.php.
$form->isValid()
returns true if the username or the email exist even if other fields are empty.
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';
}
}
And my controller
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()
));
}
}
Solution 2
You have to use Validation Constraints NotNull or NotBlank ( http://symfony.com/doc/current/reference/constraints.html ).
My guess is that, default "required=true" on fields isn't working because You are recreating request.
OTHER TIPS
tip:
You shouldn't try to re-create the Request
inside a controller. It is definitely a bad practice and can lead to undesired behavior of your application - as you're currently experiencing.
symfony will pass the request automatically to your controller actions if you add it as a method parameter:
public function createAction(Request $request)
{
// $request is holding the current request
}
Further you can get the current request from the container inside a ContainerAware
class like a controller.
$request = $this->getRequest();
$request = $this->get('request');
$request = $this->container->get('request');
Further please stick with the symfony coding standards. There are multiple violations in your code.
Method names should be lower camelCased not CamelCased.
UserBundle/form
should be UserBundle/Form
.
A method's opening curly brackets/braces belong to the next line.
public function __construct($type) // {
{
$this->type = $type;
}