diff --git a/assets/css/split/formulaires.scss b/assets/css/split/formulaires.scss index 8037a892..25a5570b 100755 --- a/assets/css/split/formulaires.scss +++ b/assets/css/split/formulaires.scss @@ -3,7 +3,15 @@ list-style-type: none; } } - +label{ + &.required{ + &:after{ + content: "*"; + color: red; + margin-left: 1em; + } + } +} table { width: 100%; } diff --git a/src/AppBundle/Controller/ProductController.php b/src/AppBundle/Controller/ProductController.php index d937740f..ae6163a2 100755 --- a/src/AppBundle/Controller/ProductController.php +++ b/src/AppBundle/Controller/ProductController.php @@ -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 ); diff --git a/src/AppBundle/Form/ProductType.php b/src/AppBundle/Form/ProductType.php index c337af83..89c391f9 100755 --- a/src/AppBundle/Form/ProductType.php +++ b/src/AppBundle/Form/ProductType.php @@ -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, + ] ); + } ); + } /** diff --git a/src/AppBundle/Form/SellRecordType.php b/src/AppBundle/Form/SellRecordType.php index d9f88cd9..e38f13e4 100755 --- a/src/AppBundle/Form/SellRecordType.php +++ b/src/AppBundle/Form/SellRecordType.php @@ -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' ); + + } /**