我在Symfony2中有一个表单,其中用户正在提交其信用卡信息。表单可以很好地进行初始显示,但是在提交时,我得到了错误"An exception occured in driver: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)".
我的控制器操作代码:
<?php
/**
* Class: PaymentController
*
* This controller is used to serve an iframe that contains a form for
* a person's credit card information.
*
* @see Controller
*/
class PaymentController extends Controller
{
/**
* indexAction
*
* Display a form for a user to enter their credit card information
*
* @param Request $request
*/
public function subscribeAction(Request $request)
{
$ccForm = new CreditCard();
$form = $this->createForm(new CreditCardType(), $ccForm);
//This function attempts to connect to DB on form submission
$form->handleRequest($request);
//Never gets here
if ($form->isValid()) {
}
//render view here (this part works fine)
}
}
我的问题是为什么要$form->handleRequest($request)
尝试连接到数据库?从堆栈跟踪中,看起来好像Validator类调用了Doctrine ORM,它正在尝试连接到那里的数据库。我不希望它那样做,我只是想将数据保留在内存中并使用它进行一些API调用。
我的实体类上没有任何元数据,这表明Doctrine应该尝试将其保存到数据库中。有任何想法吗?
编辑:如果我从app/config/config.yml
下面删除教义配置,那么我可以在访问完表单对象之后$form-isValid()
# Doctrine Configuration
#doctrine:
# dbal:
# driver: "%database_driver%"
# host: "%database_host%"
# port: "%database_port%"
# dbname: "%database_name%"
# user: "%database_user%"
# password: "%database_password%"
# charset: UTF8
# # if using pdo_sqlite as your database driver:
# # 1. add the path in parameters.yml
# # e.g. database_path: "%kernel.root_dir%/data/data.db3"
# # 2. Uncomment database_path in parameters.yml.dist
# # 3. Uncomment next line:
# # path: "%database_path%"
#
# orm:
# auto_generate_proxy_classes: false
# auto_mapping: false
如果创建CreditCardType()
对象,它将调用您的实体,包括用于检查该对象的学说和连接。您可以尝试在控制器中手动创建表单,如下所示:
public function subscribeAction(Request $request)
{
$form = $this->createCreditCardForm();
$form->handleRequest($request);
if ($form->isValid()) {
$entity = $form->getData();
// do what you want with the $entity
}
//render view here
}
private function createCreditCardForm()
{
return $this->createFormBuilder()->setAction($this->generateUrl('route_here_if_needed'))
->setMethod('GET')
->add('your_field', 'type')
->add('submit', 'submit')
->getForm()
;
}
如果可以成功完成此操作,则可以尝试修改类型而不创建CreditCardType()
对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句