所以我无法从子表单中获取属性。场景是我制作了一个工作表,其中选择了“雇主”。如果用户是应用程序管理员,他们可以选择所有雇主。如果用户是与多个雇主一起工作的工人,他们会在表单中选择雇主。否则(如果他们有一个雇主),我想用雇主集渲染一个隐藏字段。此外,Employer 子表单应自行处理此问题。
我的策略是在雇主子表单中设置一些属性,但是当我将这些属性添加到工作表单时,它们并没有通过(特别是“价值”)。
有谁知道我如何传播这些属性?
EmployerType(子表单)
<?php
namespace Agency\Work\WorkBundle\FormType;
use Agency\Work\WorkBundle\Transformer\EmployerTransformer;
use Agency\Work\WorkBundle\Transformer\SnowflakeTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EmployerType extends AbstractType
{
private $options;
private $usor;
private $single;
private $biller;
// public function __construct($usor) {
// $this->usor=$usor;
public function __construct( array $options=array() ) {
$this->usor=$options['usor'];
$this->em=$options['em'];
$this->single= ( 1 == $this->usor->getEmployer()->count() );
}
public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->create( 'employer'
, 'entity'
, array(
'class' => 'Agency\Work\WorkBundle\Entity\Employer'
, 'query_builder' =>
function ( $repository ) {
$qb = $repository->createQueryBuilder( 'b' )
->addOrderBy( 'b.name' );
if ( ! $this->usor->isAdmin() ) {
$qb->join( 'b.usor', 'u' )->andWhere( "u=$this->usor" );
}
return $qb;
}
)
)
;
if ($this->single) {
$builder->setOption('data'
, $this->single?( $this->usor->getEmployer()->first()->getId() ):''
);
$transformer=new EmployerTransformer( $this->em );
$builder->addModelTransformer($transformer);
}
var_dump($builder->getOptions());
}
/**
*
*
* @param object $resolver
*/
public function setDefaultOptions( OptionsResolverInterface $resolver ) {
$resolver->setDefaults( array(
'class' => 'Agency\Work\WorkBundle\Entity\Employer'
, 'label' => 'Employer'
, 'label_attr' => array( 'class' => 'txtl' )
) );
}
public function getParent() {
return $this->single ? 'hidden' : 'entity';
}
/**
*
*
* @return unknown
*/
public function getName() {
return 'employer';
}
}
WorkType(子表单)
<?php
namespace Agency\Work\WorkBundle\FormType;
use Agency\Work\WorkBundle\Transformer\EmployerTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class WorkType extends AbstractType
{
public function buildForm( FormBuilderInterface $builder, array $options ) {
$securityContext = $options['attr']['securitycontext'];
if ( isset( $options['attr']['em'] ) ) { $em = $options['attr']['em'];} else {$em=null;}
$usor = $securityContext->getToken()->getUser();
$builder->add(
$builder->create ( 'employer'
, new EmployerType( array( 'usor' => $usor, 'em'=>$em ) )
)
)
;
$builder
->add( 'workDate'
, null
, array(
'label' => 'End'
, 'label_attr' => array( 'class' => 'txtl' )
, 'attr' => array( 'class' => 'dateEnd' )
)
);
}
/**
*
*
* @param object $resolver
*/
public function setDefaultOptions( OptionsResolverInterface $resolver ) {
$resolver->setDefaults( array(
'data_class' => 'Agency\Work\WorkBundle\Entity\Consent'
) );
}
/**
*
*
* @return unknown
*/
public function getName() {
return 'jobtype';
}
}