I start to use Symfony about 5-7 days ago, if my question is very simple, sorry, but I can't find a solution of my problem.
I've created 2 form classes - UserType and ClientType. Difference between them is that in ClientType form exists few additional fields.
Here is Usertype class:
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('username', 'text');
$builder->add('email', 'email', array(
'attr' => array('class'=>'email') ) );
$builder->add('Password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
$builder->add( 'roles', 'choice', array(
'choices' => array('ROLE_SYSTEM_ADMIN' => 'System Administrator', 'ROLE_ACCOUNT_ADMIN' => 'Account Manager', 'ROLE_UPLOADER' => 'Uploader'),
'mapped' => false ) );
$builder->add('is_active', 'checkbox', array(
'label' => 'Active (user can log in)',
'required' => false,
));
$builder->add('add_user', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\User'
));
}
public function getName()
{
return 'user';
}
}
And at last ClientType class. All the same, except this part:
Update:
<?php
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ClientType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('username', 'text');
$builder->add('email', 'email', array(
'attr' => array('class'=>'email') ) );
$builder->add('Password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
$builder->add('address', 'text');
$builder->add('telephone', 'text');
$builder->add('internalContactName', 'text');telephone
$builder->add( 'roles', 'checkbox', array(
'choices' => array('label' => 'Client can upload', ),
'required' => false ) );
$builder->add('is_active', 'checkbox', array(
'label' => 'Active (user can log in)',
'required' => false,
));
$builder->add('add_client', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\Client'
));
}
public function getName()
{
return 'client';
}
}
And after this steps I've used Usertype class in user controller successfully with this statement:
$objUser = new User();
$objForm = $this->get( 'form.factory')->create( new UserType(), $objUser );
But when I tried to use ClientType in Client Controller with similar syntax:
Update: ClientController
use Acme\Gyvfiles\GyvefileBundle\Entity\Permission;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Util\StringUtils;
use Acme\Gyvfiles\GyvefileBundle\Entity\Client;
use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType;
class ClientController extends Controller
{
public function addclientAction()
{
$em = $this->get( 'doctrine' )->getEntityManager();
$objClient = new Client();
$objForm = $this->get( 'form.factory')->create( new ClientType(), $objClient );
//$objForm->add('address', 'text');
return $this->render( 'AcmeGyvfilesGyvefileBundle:Client:addclient.html.twig', array(
'form' => $objForm->createView(),
));
}
}
appears Class not found exception.
"ClassNotFoundException: Attempted to load class "ClientType" from namespace "Acme\Gyvfiles\GyvefileBundle\Form\Type" in C:\wamp\www\Symfony\src\Acme\Gyvfiles\GyvefileBundle\Controller\ClientController.php line 22. Do you need to "use" it from another namespace?"
Can anyone help my with this problem? Thanks!
I just encountered the same issue. It was the wrong name of the file containing the "Type" class. It MUST be the same as the contained class.
Edit:
It's about PSR-0 autoloader. It tries to include file based on the namespace of needed class. So given Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType namespace there MUST be a ClientType class in the {pathToSymfony}/Acme/Gyvfiles/GyvefileBundle/Form/Type/ folder declared with
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
at the begining of the file.
That was the isue in my case. Hope it helps!
I have encountered this as you described, but it went away when I took an underscore out of the class name I'm instantiating. You don't have an underscore in yours, but try changing the name anyway.
For me helped composer dump-autoload --optimize, because previously I executed composer dump-autoload --optimize
I can only assume that you are calling new ClientType or the likes in your controller? If so, you should add use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType just under your namespace declaration as otherwise it is trying to find the class ClientType in the same folder as the calling class.
If you are using namespaces then calling a class without a namespace or without it being declared as use'd means it is being called relatively (so in the same directory).
In your controller ( or in defautController-> indexAction-> of your bundle), if you use a render call with your bundle name like this: "$this->render('MyBundle:Default:index.html.twig');"
you need to add a use Bundle like this : "use PathBundle\MyBundle as MyBundle at ";
The time I got that error my entity was like the folowing:
/**
* InspectionPlanting
*
* #ORM\Table(name="inspectionplanting")
* #ORM\Entity(repositoryClass="Pencode\ForestryBundle\Entity\ClientProfileRepository")
All I did was to change it to without including my repository:
/**
* InspectionPlanting
*
* #ORM\Table(name="inspectionplanting")
* #ORM\Entity
The last one worked.
Hope this helps someone
When I had similar error:
Attempted to load class \"CreateImportJobHandler\" from namespace \"NG\Command\".\nDid you forget a \"use\" statement for another namespace?
I was having correct use statement in the class which was using CreateImportJobHandler, but in xml service configuration was bad.
Had to be
<parameter key="ng.command.handler.create_import_job_handler.class">NG\Command\Groups\CreateImportJobHandler</parameter>
In my case I had a typo in class file name: filename was Syncroniser but the classname was Synchronizer.
Related
I don't really know which title to give this thread but, i'm working on a Symfony project in v3.1.6 and using the select2 plugin in field of my form with ajax.
I want to use the event (submit and pre_set_data) of the form component for creation and editing to change the field dynamically like this part of the symfony doc. Everything work fine until when i submit the form and give an error Notice: Undefined variable: options
My form type code here
namespace Xxx\ArticleBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Xxx\ArtistBundle\Repository\ArtistRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityManager;
class ArticleType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//$em = $options['em']; //this give me same error
$builder
->add('artist', EntityType::class, array(
'class' => 'XxxArtistBundle:Artist',
'choice_label' => 'artist_name',
'multiple' => false,
'expanded' => false))
->add('title', TextType::class)
->add('categories', EntityType::class, array(
'class' => 'XxxArticleBundle:Category',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true))
->add('image', ChoiceType::class, array(
'expanded' => false,
'multiple' => false))
->add('intro', CKEditorType::class)
->add('content', CKEditorType::class);
$formModifier = function (FormInterface $form, $image) {
$listImages = $options['em']->getRepository('XxxAppBundle:Image')->find($image)); //this line give me the error
if (!$listImages) {
return; //i will add a FormError in the field
}
$listImages = array();
die(var_dump($listImages));
$form->add('image', EntityType::class, array(
'class' => 'XxxAppBundle:Image',
'choices' => $listImages,
));
};
$builder->get('image')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$image = $event->getForm()->getData();
//die(var_dump($image)); //select2 field, returned null (but it's not the question
//die(var_dump($options)); returned error 500 Notice: Undefined variable: options
$formModifier($event->getForm()->getParent(), $image);
}
);
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Xxx\ArticleBundle\Entity\Article',
));
$resolver->setRequired('em');
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'xxx_articlebundle_article';
}
}
And i also want to say the goal is to have a UI similar w/ Wordpress when create a post from the dashboard, for set an image in an article and when image is selected throw it in a tag to the user and without entityType because it will have thousand so i choose an choiceType to use with the select2 plugin but if someone got a better solution i'm on it.
Thx in advance for the help
When you're using a variable from a parent scope inside a closure, you should pass it to 'use' language construct.
$formModifier = function (FormInterface $form, $image) use ($options) {
$listImages = $options['em']->getRepository('XxxAppBundle:Image')->find($image)); //this line give me the error
if (!$listImages) {
return; //i will add a FormError in the field
}
$listImages = array();
die(var_dump($listImages));
$form->add('image', EntityType::class, array(
'class' => 'XxxAppBundle:Image',
'choices' => $listImages,
));
};
I'm trying to build a form in Symfony 3 and am stuck with some issue which I think should be trivial to solve, but I guess I'm not looking at the correct place.
I have 2 objects, Entity and Supplier. 1 entity does have 1 supplier associated to it. 1 supplier can have many entities related to it. (One to Many association).
I'm trying to build the form for the Entity class, with a drop down list providing the supplier it has to be associated with.
Here is the Entity form class:
<?php
namespace VP\SupplierBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use VP\SupplierBundle\Entity\Supplier;
use VP\SupplierBundle\Repository\SupplierRepository;
class EntityType extends AbstractType {
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('supplier', Supplier::class, array(
'class' => 'VPSupplierBundle:Supplier',
'property' => 'name',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
->add('submitnew', SubmitType::class, array(
'label' => 'Add Entity',
'attr' => array(
'class' => 'btn btn-primary',
)));
}
Here is my SupplierRepository which is supposed to be called:
<?php
namespace VP\SupplierBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* SupplierRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class SupplierRepository extends EntityRepository {
public function findAllByIsDeleted($isDeleted) {
$qb = $this->createQueryBuilder('s');
$qb
->where('s.isDeleted = :isDeleted')
->setParameter('isDeleted', $isDeleted);
return $qb;
}
}
Still, I get the following error:
Could not load type "VP\SupplierBundle\Entity\Supplier"
Any idea where it could come from? Tried to look at the official symfony doc and some forum topics but no luck so far...
Thanks a lot for your help!
Working solution provided by #Jeet
//
use Symfony\Bridge\Doctrine\Form\Type\EntityType as DoctrineEntityType;
//
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('supplier', DoctrineEntityType::class, array(
'class' => 'VPSupplierBundle:Supplier',
'query_builder' => function (EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
->add('submitnew', SubmitType::class, array(
'label' => 'Add Entity',
'attr' => array(
'class' => 'btn btn-primary',
)));
}
Thanks!
You are doing it here wrong :
->add('supplier', Supplier::class, array(
'class' => 'VPSupplierBundle:Supplier',
'property' => 'name',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
You are loading a Supplier::class which unknown to Form Type. I guess you should instead load a EntityType::class form type.
Check here the document. You want to show a choice Field with association to entity, hence you need to have EntityType::class. To recognize the Entity Type, you are already providing your Entity identity through class option. So it makes clear.
Hope it helps!
Maybe i have a very dumb question, but i'm new in Symfony2 and i was wondering if i can build a Search Form with Symfony Form Component, the same way I do with a Registration Form for example.
My Search form will have a Country select field, a Club select field, a Gender radio buttons field a Level select field and the submit button.
Is it possible to do that with the Form Component or to do something like this is better to just build the search form directly in the view?
I've been searching for information about this, but I didn't find anything.
Here is how my SearchPlayerType.php looks like.
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityRepository;
class SearchPlayersType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('country', 'entity', array(
'placeholder' => 'Choose a country',
'class' => 'AppBundle:Country',
'property' => 'name',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')->orderBy('c.name', 'ASC');
},
))
->add('club', 'entity', array(
'placeholder' => 'Choose a club',
'class' => 'AppBundle:Club',
'property' => 'name',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')->orderBy('c.name', 'ASC');
},
))
->add('gender', 'entity', array(
'class' => 'AppBundle:Gender',
'property' => 'name',
'expanded' => true
))
->add('level', 'rating', array(
'label' => 'Playing Level',
'stars' => 5))
;
}
public function getName()
{
return 'SearchPlayer';
}
If it is possible to do it this way I don´t know what Entity my data_class needs to be
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Usuario'
));
}
}
?>
You can go two ways with this: you can either create a form specific model for your search form which will either be a really thin object with public properties; or you can remove the entry for data_class in your form options which will switch your form into returning an array rather than an object (documentation).
The former is the more OO way of doing things and allows you to add validation annotations without embedding those within the form, this way also means you can add getters and setters that transform your search data very easily and not cluttering up your controllers. So your model would look something like:
namespace MyBundle\Form\Model;
class SearchModel
{
public $country;
public $club;
// ...
}
The alternate is just:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
// don't set 'data_class' in here
));
}
Then when you do $form->getData() you'll just get an array back rather than an object.
I have a PersonType form and then I have LegalPersonType and NaturalPersonType forms and both extends from PersonType since they have a common field on that form (mapped at Entity level). For example, this is the code for NaturalPersonType.php
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\CIType;
use Tanane\FrontendBundle\Form\Type\PersonType;
class NaturalPersonType extends PersonType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('identification_type', 'choice', array(
'label' => 'Número de Cédula',
'choices' => CIType::getChoices()
))
->add('ci', 'number', array(
'required' => true,
'label' => false,
'attr' => array(
'maxlength' => 8,
))
)
->add('lives_in_ccs', 'checkbox', array(
'label' => false,
'required' => false,
'value' => 1,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
public function getName()
{
return 'natural_person';
}
}
Then at SaveFormController/orderAction() I'm doing this:
$order = new Orders();
$orderForm = $this->createForm(new OrdersType(array($type)), $order, array('action' => $this->generateUrl('save_order')));
But any time I try to render the form I get this error:
Neither the property "nat" nor one of the methods "getNat()", "nat()",
"isNat()", "hasNat()", "__get()" exist and have public access in class
"Tanane\FrontendBundle\Entity\Orders".
Relationship are at Entity level, how I fix that error?
Thanks in advance
1st possible solution
Following suggestions from user here I change, in OrderType.php Form my code to this:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\SFType;
class OrdersType extends AbstractType {
/**
* #var string
*/
protected $register_type;
public function __construct($register_type)
{
$this->register_type = $register_type;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// here goes $builder with default options remove for see less code
if ($this->register_type[0] == "natural")
{
$builder->add('nat', new NaturalPersonType(), array(
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
elseif ($this->register_type[0] == "legal")
{
$builder->add('leg', new LegalPersonType(), array(
'data_class' => 'Tanane\FrontendBundle\Entity\LegalPerson'
));
}
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
'render_fieldset' => FALSE,
'show_legend' => FALSE
));
}
public function getName()
{
return 'orders';
}
}
I've fixed by adding 'mapped' => FALSE on each new FormType I add in OrdersType but I don't know if this is the right. Also, if I'm defining the data_class here, and NaturalType will never be access directly just trough OrdersType should I remove the default options from that form or should I leave them there? How can I fix the problem now? What I'm missing?
This is not a direct answer to your question but maybe could solve some problem before that happens...
I don't remember to have seen it's possible to extend a form like this instead of extend AbstractType, but as explained in the docs, if you have common fields to share between different types of forms you should use the native framework modularity offered by inherit_data.
If you need something more specific (some special methods to execute on some field) you can create a new field type or extend an existing one using AbstractTypeExtension.
EDIT:
I don't know exactly why you are using this approach (that I never used in my projects) but IMO PersonType, NaturalPersonType and LegalPersonType should be only "FormType/FieldType" initialized with inherit_data (and not entities like in your code) that contains the fields related to their use, while OrdersType should be composed with the block of forms needed to the type of person who fills it and with data_class setted on the UNIQUE entity that store the data outputted by the form.
I'm using Symfony2 form component to build and validate forms. Now I need to setup validator groups based on a single field value, and unfortunately it seems that every example out there is based on entities - which im not using for several reasons.
Example:
If task is empty, all constraint validators should be removed, but if not, it should use the default set of validators (or a validator group).
In other words, what I'm trying to achieve is making subforms optional, but still be validated if a key field is populated.
Can someone possible give me an example how to configure it?
<?php
namespace CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
use CoreBundle\Form\Type\ItemGroupOption;
class ItemGroup extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text', array(
'label' => 'Titel',
'attr' => array('class' => 'span10 option_rename'),
'required' => false
));
$builder->add('max_selections', 'integer', array(
'label' => 'Max tilvalg',
'constraints' => array(new Assert\Type('int', array('groups' => array('TitleProvided')))),
'attr' => array('data-default' => 0)
));
$builder->add('allow_multiple', 'choice', array(
'label' => 'Tillad flere valg',
'constraints' => array(new Assert\Choice(array(0,1))),
'choices' => array(0 => 'Nej', 1 => 'Ja')
));
$builder->add('enable_price', 'choice', array(
'label' => 'Benyt pris',
'constraints' => array(new Assert\Choice(array(0,1))),
'choices' => array(0 => 'Nej', 1 => 'Ja'),
'attr' => array('class' => 'option_price')
));
$builder->add('required', 'choice', array(
'label' => 'Valg påkrævet',
'constraints' => array(new Assert\Choice(array(0,1))),
'choices' => array(0 => 'Nej', 1 => 'Ja')
));
$builder->add('options', 'collection', array(
'type' => new ItemGroupOption(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
)
);
$builder->add('sort', 'hidden');
}
public function getName()
{
return 'item_group';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
global $app;
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) use ($app) {
// Get submitted data
$data = $form->getData();
if (count($app['validator']->validateValue($data['title'], new Assert\NotBlank())) == 0) {
return array('TitleProvided');
} else {
return false;
}
},
));
}
}
If you are using 2.1 you may want to have a look at "Groups based on submitted data".
Update
Example using the demo contact page /demo/contact in the default AcmeDemoBundle provided with Symfony Standard Edition :
The form type with conditional validation groups :
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
class ContactType extends AbstractType
{
// Inject the validator so we can use it in the closure
/**
* #var Validator
*/
private $validator;
/**
* #param Validator $validator
*/
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email');
$builder->add('message', 'textarea', array(
// Added a constraint that will be applied if an email is provided
'constraints' => new Assert\NotBlank(array('groups' => array('EmailProvided'))),
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
// This is needed as the closure doesn't have access to $this
$validator = $this->validator;
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) use ($validator) {
// Get submitted data
$data = $form->getData();
$email = $data['email'];
// If email field is filled it will not be blank
// Then we add a validation group so we can also check message field
if (count($validator->validateValue($email, new Assert\NotBlank())) == 0) {
return array('EmailProvided');
}
},
));
}
public function getName()
{
return 'contact';
}
}
Don't forget to inject the validator service in the form type :
<?php
namespace Acme\DemoBundle\Controller;
//...
class DemoController extends Controller
{
// ...
public function contactAction()
{
$form = $this->get('form.factory')->create(new ContactType($this->get('validator')));
// ...
}
}
As you can see validation of the message field will be triggered only if the email field is filled.
Use a diff tool to catch the differences.