我在项目中使用Propel,并且尝试设置使用ModelType输入的表单的默认值,并且我需要设置以这种形式存储在会话中的默认值,并且该会话中用于存储的值不为null这种功能。
这是我的表格:
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Fcl\VitrinellisBundle\Model\ProfileVariety',
'name' => 'profile_variety_search',
'locales' => ['fr'],
'session' => null
));
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', ModelType::class, array(
'class' => 'Fcl\VitrinellisBundle\Model\ProfileVariety',
'query' => ProfileVarietyQuery::create()->orderById(),
'property' => 'name',
'label' => 'Profil recherché',
'expanded' => false,
'multiple' => false,
'required' => false,
'placeholder' => '- Filtrer par profil -',
'attr' => array(
'onchange' => 'submit()',
'class' => 'col s3'
)
))
;
}
这是我的治疗方法:
public function listAction(Request $request = null)
{
$pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
$profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
$session = $request->getSession();
$profileVariety = new ProfileVariety();
$models = null;
$form = $this->createForm(ProfileVarietySearchType::class, $profileVariety);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (null != $form['name']->getData()) {
$models = $pModelManager->getWebsiteByModel($form['name']->getData()->getName());
$session->set('profileVarietySearch', $form['name']->getData()->getName());
} else {
$models = $pModelManager->getList();
}
} else {
if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
$models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
} else {
$models = $pModelManager->getList();
}
}
return $this->render('console\p_model_list.html.twig', array(
'objArray' => $models,
'form' => $form->createView()
));
}
我尝试使用'data'选项和表单中的PRE_SET_DATA事件设置默认数据,但结果令人满意。
答案 0 :(得分:1)
在EntityType中,传入的默认“数据”必须是正确类型的对象。我首先尝试看看我们是否有传入的表单数据。
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Check form data
$formDataEntity = $builder->getData();
// Check if it has the field filled in
if ($formDataEntity && $formDataEntity->getName()) {
$objToSet = $formDataEntity->getName();
} else {
$objToSet = $options['incomingDefaultObject'];
)
$builder
->add('name', ModelType::class, array(
class => 'Fcl\VitrinellisBundle\Model\ProfileVariety',
data => $objToSet,
...
然后是解析器
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'incomingDefaultObject' => null,
));
}
然后您在控制器中使用默认选项调用表单
$form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));
警告:如果用户决定将该字段留空,则此代码将始终设置默认值。
答案 1 :(得分:0)
我还有其他解决方法。
创建一个新模型,其名称为ProfileVarietySearch,如下所示:
class ProfileVarietySearch
{
/** @var null|ProfileVariety $profileVariety */
private $profileVariety;
/**
* @return bool
*/
public function is_empty()
{
return is_null($this->profileVariety);
}
/**
* @return null|ProfileVariety
*/
public function getProfileVariety()
{
return $this->profileVariety;
}
/**
* @param $profileVariety
*
* @return ProfileVarietySearch
*/
public function setProfileVariety($profileVariety): self
{
$this->profileVariety = $profileVariety;
return $this;
}
}
在控制器中,编写以下代码:
public function listAction(Request $request = null)
{
$pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
$profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
$session = $request->getSession();
$profileVarietySearch = new ProfileVarietySearch();
$models = null;
if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
$profileVarietySearch->setProfileVariety(
$profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
);
}
$form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (null != $form->getData()->getProfileVariety()) {
$models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
$session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
} else {
$session->set('profileVarietySearch', null);
$models = $pModelManager->getList();
}
} else {
if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
$models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
} else {
$models = $pModelManager->getList();
}
}
return $this->render($this->view_list, array(
'objArray' => $models,
'form' => $form->createView()
));
}
在ProfileVarietySearchType中,输入以下内容:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Fcl\VitrinellisBundle\Form\Model\ProfileVarietySearch',
'name' => 'profile_variety_search'
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('profileVariety', ModelType::class, array(
'class' => 'Fcl\VitrinellisBundle\Model\ProfileVariety',
'query' => ProfileVarietyQuery::create()->orderById(),
'property' => 'name',
'label' => 'Profil recherché',
'expanded' => false,
'multiple' => false,
'required' => false,
'placeholder' => '- Filtrer par profil -',
'attr' => array(
'onchange' => 'submit()',
'class' => 'col s3'
)
));
}