Symfony 4 Form handling
I am not quite sure how symfony is providing the possibility of different forms for different users/views.
For my understanding you have the action:
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
Which is generating the form with the, in this case OrdersType. And then the view gets loaded in the new.html.twig file. But some users only are allowed on the action "add" which should not show the option of calculated price or what ever.
How do I do that?
forms symfony
add a comment |
I am not quite sure how symfony is providing the possibility of different forms for different users/views.
For my understanding you have the action:
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
Which is generating the form with the, in this case OrdersType. And then the view gets loaded in the new.html.twig file. But some users only are allowed on the action "add" which should not show the option of calculated price or what ever.
How do I do that?
forms symfony
add a comment |
I am not quite sure how symfony is providing the possibility of different forms for different users/views.
For my understanding you have the action:
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
Which is generating the form with the, in this case OrdersType. And then the view gets loaded in the new.html.twig file. But some users only are allowed on the action "add" which should not show the option of calculated price or what ever.
How do I do that?
forms symfony
I am not quite sure how symfony is providing the possibility of different forms for different users/views.
For my understanding you have the action:
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
Which is generating the form with the, in this case OrdersType. And then the view gets loaded in the new.html.twig file. But some users only are allowed on the action "add" which should not show the option of calculated price or what ever.
How do I do that?
forms symfony
forms symfony
asked Nov 22 '18 at 20:25
IsengoIsengo
801724
801724
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
<a href="{{path('edit_order')}}">Edit an order</a>
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437639%2fsymfony-4-form-handling%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
<a href="{{path('edit_order')}}">Edit an order</a>
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
add a comment |
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
<a href="{{path('edit_order')}}">Edit an order</a>
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
add a comment |
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
<a href="{{path('edit_order')}}">Edit an order</a>
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
<a href="{{path('edit_order')}}">Edit an order</a>
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...
edited Nov 23 '18 at 19:38
answered Nov 22 '18 at 21:33
GulvarGulvar
114
114
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
add a comment |
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
Hi Gulvar, but this way I am still using the same builder fields. I need to know how I can create two different builder types, otherwise I would have to create the whole form over and over again in the twig file.
– Isengo
Nov 23 '18 at 17:29
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
I've added an example on how to user logged user role in form builder.
– Gulvar
Nov 23 '18 at 19:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437639%2fsymfony-4-form-handling%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown