所以我试图通过各自的服务将FormType与FormHandler一起使用,以获得非常干净的Form流程。
现在问题是我在Symfony 3.0中收到错误:
Expected argument of type "string", "AppBundle\Form\Type\CurrencyType" given
我不确定如何在新版本中使用它。
FormType:
/**
* Class CurrencyType
* @package AppBundle\Form\Type
*/
class CurrencyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('displayName', TextType::class)
->add('symbol', TextType::class)
->add('save', SubmitType::class, array('label' => 'Voeg Currency toe'))
;
}
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Currency'
));
}
}
表单处理程序:
class CurrencyFormHandler
{
/**
* @var Request
*/
protected $requestStack;
/**
* @var CurrencyManager
*/
protected $currencyManager;
/**
* @var FormInterface
*/
protected $form;
/**
* CurrencyFormHandler constructor.
* @param FormInterface $form
* @param RequestStack $requestStack
* @param CurrencyManager $currencyManager
*/
public function __construct(FormInterface $form, RequestStack $requestStack, CurrencyManager $currencyManager)
{
$this->form = $form;
$this->requestStack = $requestStack;
$this->currencyManager = $currencyManager;
}
/**
* Process the form
*
* @param Currency|null $currency
* @return bool
*/
public function process(Currency $currency = null)
{
if (null === $currency) {
$holiday = $this->currencyManager->create();
}
$this->form->setData($currency);
$this->form->handleRequest($this->requestStack->getCurrentRequest());
if ($this->form->isValid()) {
$this->onSuccess($currency);
return true;
}
return false;
}
/**
* When the form is succesfully posted
*
* @param Currency $currency
*/
public function onSuccess(Currency $currency)
{
$this->currencyManager->save($currency);
}
}
服务:
<service id="app.currency.form" class="Symfony\Component\Form\Form">
<argument>app_currency</argument>
<argument type="service" id="app.currency.form.type" />
<factory method="createNamed" service="form.factory" />
</service>
<service id="app.currency.form.type" class="AppBundle\Form\Type\CurrencyType">
<tag name="form.type" alias="currencyform"/>
</service>
<service id="app.currency.form.handler"
class="AppBundle\Form\Handler\CurrencyFormHandler">
<argument type="service" id="app.currency.form" />
<argument type="service" id="request_stack" />
<argument type="service" id="app.currency.manager"/>
</service>
控制器:
/**
* Class AddController
* @package AppBundle\Controller\Currency
*/
class AddController extends Controller
{
/**
* @Route("/currency/add", name="currency_add")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
$form = $this->get('app.currency.form');
$formHandler = $this->get('app.currency.form.handler');
$process = $formHandler->process();
if ($process) {
$this->get('session')->getFlashBag()->add('success', 'Currency succesvol toegevoegd');
return $this->redirect($this->generateUrl('currency_overview'));
}
return $this->render(':currency:add.html.twig', [
'form' => $form->createView(),
]);
}
}
那么到底出了什么问题呢?这曾经在Symfony 2中一直有效。
答案 0 :(得分:0)
您的表单处理程序看起来不正确。 你已宣布:
/**
* @var CurrencyManager
*/
protected $currencyManager;
但我相信默认情况下是一个字符串。 然后你这样做:
public function onSuccess(Currency $currency)
{
$this->currencyManager->save($currency);
}
尝试将其设置为&#34; Currency&#34;而不是&#34;字符串&#34;!