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; list-style-type: none;
} }
} }
label{
&.required{
&:after{
content: "*";
color: red;
margin-left: 1em;
}
}
}
table { table {
width: 100%; width: 100%;
} }

View File

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

View File

@ -2,11 +2,23 @@
namespace AppBundle\Form; 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\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
class ProductType extends AbstractType { class ProductType extends AbstractType {
private $security;
public function __construct( Security $security ) {
$this->security = $security;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -17,9 +29,28 @@ class ProductType extends AbstractType {
[ 'label' => 'URL pour image' ] ) [ 'label' => 'URL pour image' ] )
->add( 'stockCount' ) ->add( 'stockCount' )
->add( 'price') ->add( 'price')
->add( 'comment' ) ->add( 'comment' );
->add( 'category' )// ->add( 'user' ) $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; namespace AppBundle\Form;
use AppBundle\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
class SellRecordType extends AbstractType { class SellRecordType extends AbstractType {
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm( FormBuilderInterface $builder, array $options ) { public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->add( 'products' ); $builder->add( 'products' );
} }
/** /**