我正在实施自定义表单类型,该表单类型提供自动填充字段以选择位置(国家,城市或地点)。表单类型创建两个字段,一个用于自动完成搜索输入的文本字段和一个用于保存所选位置的选定ID的隐藏字段。
在文本字段中键入时,将进行服务器调用,并通过jquery自动完成显示结果。如果选择了某个位置,则所选位置的ID将写入隐藏字段,而该位置的名称将显示在文本字段中。在服务器上,我使用客户端转换器来查找隐藏字段传递的id的实体。文本字段被忽略。
我的模型类定义了一个位置字段,该字段具有一个属性,用于回写使用NotNull验证约束注释的位置实体。
到目前为止,所有内容都完美无缺,但如果我没有选择位置,则验证消息“此值不应为null”。显示两次。
捆绑包是公开的,可以在我的github仓库中找到。相关的课程是LocationFieldType和LocationDataTransformer以及form theme。
现在我将如何将表单类型集成到我的项目中。我添加了整个代码,对于质量而言很抱歉;)
在模型中,我将属性定义如下:
class JourneyCreate
{
/**
* @Assert\NotNull()
* @Assert\Choice(choices = {"offer", "request"})
*/
public $type;
/**
* @Assert\NotNull()
* @Assert\Date()
*/
public $date;
/**
* @Assert\NotNull()
* @Assert\Time()
*/
public $time;
/**
* @Assert\NotNull()
*
*/
public $start;
/**
* @Assert\NotNull()
*
*/
public $destination;
public function buildJourney(User $owner)
{
switch($this->type)
{
case 'offer':
$journey = new JourneyOffer();
break;
case 'request':
$journey = new JourneyRequest();
break;
default:
throw new \InvalidArgumentException('Invalid journey type');
}
$journey->setDate($this->date);
$journey->setTime($this->time);
$journey->addStation(new JourneyStation($this->start));
$journey->addStation(new JourneyStation($this->destination));
$journey->setOwner($owner);
return $journey;
}
}
在主要表单中,我添加了以下字段:
class JourneyCreateType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type','choice', array(
'choices' => array(
'offer' => 'Driver',
'request' => 'Passanger',
),
'empty_value'=>'',
'multiple' => false,
'expanded' => true,
))
->add('date','date',array(
'widget' => 'single_text',
'format' => $this->getDateFormat(\IntlDateFormatter::TRADITIONAL),
))
->add('time','time',array(
'widget' => 'single_text',
))
->add('start','room13_geo_location')
->add('destination','room13_geo_location')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Form\Model\JourneyCreate',
));
}
public function getName()
{
return 'journey_create';
}
}
控制器代码:
/**
* @Route("/create/{type}", defaults={"type" = null})
* @Template()
*/
public function createAction($type=null)
{
if($type !== null && !in_array($type,array('request','offer')))
{
throw new NotFoundHttpException();
}
$journeyCreate = new JourneyCreate();
$journeyCreate->type = $type;
$form = $this->createForm(new JourneyCreateType(),$journeyCreate);
if($this->isPost())
{
$form->bind($this->getRequest());
if($form->isValid())
{
$journeyCreate = $form->getData();
$journey = $journeyCreate->buildJourney($this->getCurrentUser());
$this->persistAndFlush($journey);
return $this->redirect($this->generateUrl('acme_demo_journey_edit',array('id'=>$journey->getId())));
}
}
return array(
'form' => $form->createView(),
);
}
最后显示表单的模板代码:
{% block page_body %}
<form class="form-horizontal" action="{{ path('acme_demo_journey_create') }}" method="post" novalidate>
{{form_widget(form)}}
<div class="form-actions">
<button class="btn btn-primary" type="submit">{{'form.submit'|trans}}</button>
<a href="{{path("acme_demo_journey_index")}}" class="btn">{{'form.cancel'|trans}}</a>
</div>
</form>
{% endblock %}
我有理论认为这可能是因为我使用了两个表单字段,但不知道如何解决这个问题。欢迎任何关于如何解决这个问题的建议。
答案 0 :(得分:2)
这个问题看起来很复杂,答案就像从widget模板块中删除{{form_errors(form)}}一样简单。因为* form_row *块看起来像:
{% block form_row %}
{% spaceless %}
<div class="form_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endspaceless %}
{% endblock form_row %}
错误输出两次。