Reputation: 387
FormType
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('status', 'choice', array(
'choices' => array(
'A' => 'A',
'B' => 'B',
'C' => 'C',
),
'required' => true
));
// ...
}
// ...
}
View : This is no problem.
{{ form_widget(form.status) }}
{{ form_widget(form.name) }}
<input type="Submit" value="Submit" />
View : Problem is this case.
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
{{ form_widget(form.status) }}
{% endif %}
{{ form_widget(form.name) }}
<input type="Submit" value="Submit" />
It has register blank status value if not login.
MY intention is to no change status value if there is no status field.
Do I have to switch another "FormType" in this case?
Upvotes: 0
Views: 587
Reputation: 4293
You can do like below.
Change your FormType class to:
class ArticleType extends AbstractType
{
private $isRemembered;
protected function __construct($isRemembered) {
$this->isRemembered = $isRemembered;
parent::__construct();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
if($this->isRemembered) {
$builder->add('status', 'choice', array(
'choices' => array(
'A' => 'A',
'B' => 'B',
'C' => 'C',
),
'required' => true
));
}
// ...
}
}
And in the controller where you are instantiating the form type,
$isRemembered = $this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED');
$form = $this->createForm(new ArticleType($isRemembered), $entity);
and remove the condition check in twig file.
Upvotes: 0
Reputation: 2570
You have to check the role in the form builder too, and you have 2 solutions to do that.
Solution 1
The most elegant is to create a custom form type as a service, depending on the security context:
class ArticleType extends AbstractType
{
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
// If the user is granted
if($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'))
{
$builder->add('status', 'choice', array(
'choices' => array(
'A' => 'A',
'B' => 'B',
'C' => 'C',
),
'required' => true
));
}
// ...
}
// ...
}
Mark the form type as a service :
services:
form.type.article:
class: Foo\BarBundle\Form\Type\ArticleType
arguments: ["@security.context"]
tags:
- { name: form.type, alias: article_type }
Now, instead of calling new ArticleType()
in your controller, call this new service :
$form = $this->createForm($this->get('form.type.article'), $data);
Solution 2
The second solution is to pass the security context to the ArticleType
, no need to create a service. In your controller :
$form = $this->createForm(new ArticleType($this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')), $article);
And in your form type :
class ArticleType extends AbstractType
{
private $isGranted;
public function __construct($isGranted)
{
$this->isGranted = $isGranted;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
// If the user is granted
if($this->isGranted)
{
$builder->add('status', 'choice', array(
'choices' => array(
'A' => 'A',
'B' => 'B',
'C' => 'C',
),
'required' => true
));
}
// ...
}
// ...
}
Upvotes: 1