首先,祝大家新年快乐!
我遇到了一个我无法自行解决的问题。
我正在使用Silex(~1.3
)应用程序。我在我的域类上编写了简单的CRUD。我也创建了一些Type表单,以便能够修改我的基本域类。在这种情况下,我必须在State
内管理Country
的概念。每个都是特定的类,State
有一个Country
属性。
在我的表单中,我声明了一些文本字段,并且Choice
字段可以选择国家/地区(表单类在下面复制)。
我的问题是,当我尝试使用以下控制器修改现有State
时,文本字段name
,code
,unloc
将填充来自数据库,但不是选择country
也不是hub
(控制器类在下面复制)。
请注意,我不是在使用Doctrine,而是使用自制的(非常基本的)DAO。
这是我的代码和一些信息:
使用树枝进行视图,使用标准'可以在此处找到bootstrap示例:Form Customization,使用Bootstrap layout和Form layout:
{% extends 'layout.html.twig' %} {% block title %}{{ title }}{% endblock %}
{% block content %}
{% if form and is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_ADMIN')%}
{% form_theme form 'bootstrap_3_layout.html.twig' %} {{ form_start(form) }}
<div class="form-group">
{{ form_errors(form) }}
{{ form_widget(form) }}
<input type="submit" class="btn btn-primary" value={% if button is not defined %} "Save"{% else %}"{{ button }}"{% endif %} />
</div>
{{ form_end(form) }}
{% else %}
<div>
<p>Ask an admin to add/modify information.</p>
</div>
{% endif %}
{% endblock %}
composer.json内容:
{
"require": {
"silex/silex": "~1.3",
"doctrine/dbal": "2.5.*",
"symfony/security": "2.7.*",
"twig/twig": "1.21.*",
"symfony/twig-bridge": "2.7.*",
"symfony/form": "2.7.*",
"symfony/translation": "2.7.*",
"symfony/config": "2.7.*",
"jasongrimes/silex-simpleuser": "*",
"twig/extensions": "1.3.*",
"symfony/validator": "2.*",
"phpoffice/phpexcel": "1.*"
},
"require-dev": {
"phpunit/phpunit": "*",
"symfony/browser-kit": "*",
"symfony/css-selector": "*",
"silex/web-profiler": "*",
"symfony/monolog-bridge": "*"
},
"autoload":{
"psr-4":{"Easytrip2\\": "src"}
}
}
表格代码:
<?php
namespace Easytrip2\Form\Type;
use Easytrip2\DAO\CountryDAO;
use Easytrip2\DAO\GeopointDAO;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class StateType extends AbstractType {
/**
* @CountryDAO
* \Easytrip2\DAO\CountryDAO
* allow to find the Country for the form.
*/
private $countryDAO;
/**
* @GeopointDAO
* \Easytrip2\DAO\GeopointDAO
* allow to find the Country for the form.
*/
private $geopointDAO;
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name', 'text', array (
'label' => 'State name'
) );
$builder->add ( 'code', 'text', array (
'label' => 'State code'
) );
$builder->add ( 'unloc', 'text', array (
'label' => 'State code'
) );
$countries = $this->countryDAO->findAll ();
$choices = array ();
$labels = array ();
foreach ( $countries as $value ) {
$choices [] = $value;
$labels [] = $value->getName ();
}
$builder->add ( 'country', 'choice', array (
'choice_list' => new ChoiceList ( $choices, $labels )
) );
$hubs = $this->geopointDAO->findAllHubs ();
$choices = array ();
$labels = array ();
foreach ( $hubs as $value ) {
$choices [] = $value;
$labels [] = $value->getName ();
}
$builder->add ( 'hub', 'choice', array (
'choice_list' => new ChoiceList ( $choices, $labels ),
'required' => false
) );
}
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\AbstractType::configureOptions()
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults ( array (
'data_class' => 'Easytrip2\Domain\State'
) );
}
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\FormTypeInterface::getName()
*/
public function getName() {
return 'state';
}
/**
* allow use of a CountryDAO
*/
public function __construct(CountryDAO $countryDAO, GeopointDAO $geopointDAO) {
$this->geopointDAO = $geopointDAO;
$this->countryDAO = $countryDAO;
}
}
控制器内容:
public function stateUpdateByIdAction($id, Request $request, Application $app) {
if ($app ['security.authorization_checker']->isGranted ( 'IS_AUTHENTICATED_FULLY' ) and $app ['security.authorization_checker']->isGranted ( 'ROLE_ADMIN' )) {
$obj = $app ['dao.state']->findById ( $id );
$form = $app ['form.factory']->create ( new StateType ( $app ['dao.country'], $app ['dao.geopoint'] ), $obj );
$form->handleRequest ( $request );
if ($form->isSubmitted () && $form->isValid ()) {
if ($app ['dao.state']->save ( $obj )) {
$app ['session']->getFlashBag ()->add ( 'success', 'The state was succesfully updated.' );
return $app->redirect ( $app ['url_generator']->generate ( 'state' ) );
} else {
$app ['session']->getFlashBag ()->add ( 'error', 'Something went wrong...' );
}
}
return $app ['twig']->render ( 'form.html.twig', array (
'form' => $form->createView (),
'title' => 'Edit states'
) );
} else {
$app ['session']->getFlashBag ()->add ( 'error', 'Don\'t have the rights...' );
return $app->redirect ( $app ['url_generator']->generate ( 'home' ) );
}
}
答案 0 :(得分:0)
假设选项未填充来自DB的数据,因为country和hub id未传输到ChoiceList
$choices = array();
foreach ($countries as $value) {
$choices[$value->getId()] = $value->getName();
}
$builder->add('country', 'choice', array(
'choices' => $choices
));
答案 1 :(得分:0)
我设法找到了一个解决方案(可能不是最好的,但它确实有效)。
我理解为:我们将对象设为choices
,将其用作值,然后使用闭包来获取ids
和labels
,而不是自己完成工作将“即用型”数据提供给表单。
有更清洁的方法吗?
$obj = $this->countryDAO->findAll ();
$list = array ();
foreach ( $obj as $value ) {
$list [$value->getId ()] = $value;
}
$builder->add ( 'country', 'choice', array (
'choices' => $list,
'choices_as_values' => true,
'choice_label' => function ($value) {
return $value->getName ();
},
'choice_value' => function ($value) {
// you mightwant to check for null here, is your form concern
// a attribute that can be null, as the closure appear to be called
// on the attribute, and not only on the $obj contents;
return $value->getId ();
},
'placeholder' => 'Select a country'
) );
答案 2 :(得分:0)
将此行添加到您选择的选项中:
'choices_as_values' => true,
激活新的选择类型API至关重要 http://symfony.com/doc/current/reference/forms/types/choice.html#example-usage