i want to use more than one entity to create form in createformbuilder . forexample i want to have a form with many fields from many entity and i want to check condition for view fields
userEntity -> email , password
resselerEntity - > (userEntity fields) + managerName , managerFamily
leaderEntity - > (userEntity fields) + credit
and if i want to show resseler fields , must show all fields of userEntity
and resselerEntity
if want to show userEntity
, must show all fields of userEntity
and etc,
so how can i solve this solution ? Thanks in advance!
Most common solution is to create single forms for (in your case):
Then, create a new form that have two fields of userEntityFormType and ressellerEntityFormType.
In that way you can:
Something like that
class UserEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstField')
->add('secondField')
[...]
->lastField;
}
public function getName()
{
return 'UserEntityType ';
}
}
class RessellerEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstField')
->add('secondField')
[...]
->lastField;
}
public function getName()
{
return 'RessellersEntityType ';
}
}
class AggregateEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('userEntityField',UserEntityType,array('multiple'=>true)
->add('ressellersEntityField',RessellersEntityType,array('multiple'=>true));
}
public function getName()
{
return 'AggregateEntityType ';
}
}
I think Don got you most of the way there. Add a construct argument to your UserType
public function __construct($otherEntityType) // Reseller, Leader etc.
Then use otherEntityType to determine which fields are created for UserType.
As far as I understand your problem, you should use type inheritance:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('password')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '\Hamid\User',
));
}
public function getName()
{
return 'hamid_user';
}
}
class ResellerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('managerName')
->add('managerFamily')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '\Hamid\Reseller',
));
}
public function getName()
{
return 'hamid_reseller';
}
public function getParent()
{
return 'hamid_user';
}
}
Then use the right form for each entity. If, for whatever reason, you need a single form that adapts to the class of the entity set to the form, you need to use form events, as explained in the documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.