limit categories to owned one

This commit is contained in:
Kayn Ty 2018-05-24 14:41:43 +02:00
parent d772f478b5
commit b386d886e6
No known key found for this signature in database
GPG Key ID: 55B09AA0ED327CD3
4 changed files with 59 additions and 5 deletions

View File

@ -3,7 +3,15 @@
list-style-type: none;
}
}
label{
&.required{
&:after{
content: "*";
color: red;
margin-left: 1em;
}
}
}
table {
width: 100%;
}

View File

@ -39,6 +39,8 @@ class ProductController extends Controller {
public function newAction( Request $request ) {
$product = new Product();
$product->setUser( $this->getUser() );
$product->setPrice( 1 );
$product->setStockCount( 10 );
$form = $this->createForm( 'AppBundle\Form\ProductType', $product );
$form->handleRequest( $request );

View File

@ -2,11 +2,23 @@
namespace AppBundle\Form;
use AppBundle\Entity\ProductCategory;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
class ProductType extends AbstractType {
private $security;
public function __construct( Security $security ) {
$this->security = $security;
}
/**
* {@inheritdoc}
*/
@ -16,10 +28,29 @@ class ProductType extends AbstractType {
null,
[ 'label' => 'URL pour image' ] )
->add( 'stockCount' )
->add( 'price' )
->add( 'comment' )
->add( 'category' )// ->add( 'user' )
;
->add( 'price')
->add( 'comment' );
$user = $this->security->getUser();
if ( ! $user ) {
throw new \LogicException(
'The SellRecordType cannot be used without an authenticated user!'
);
}
$builder->addEventListener( FormEvents::PRE_SET_DATA,
function ( FormEvent $event ) {
$form = $event->getForm();
$categories = $this->security->getUser()->getCategories();
// ... add a choice list of friends of the current application user
$form->add( 'category',
EntityType::class,
[
'class' => 'AppBundle:ProductCategory',
'placeholder' => '--- catégories ---',
'choices' => $categories,
] );
} );
}
/**

View File

@ -2,16 +2,29 @@
namespace AppBundle\Form;
use AppBundle\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
class SellRecordType extends AbstractType {
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->add( 'products' );
}
/**