我在Silex中遇到有关表单管理的问题。
我有一个表单(RegionType
),允许我填写并修改我的域(实体)信息(例如,有关Region
名称和描述的信息)。此表单从Symfony扩展AbstractType
。
我尝试向其中注入一个选项(validation_groups
为false),以便能够重复使用该表单来获取数据并使用此数据从我的数据库中进行搜索。
由于搜索表单和编辑表单将使用相同类型的数据相同的字段,我不想复制我的类。
以下是我的RegionType
表单:
<?php
namespace Easytrip2\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RegionType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name', 'text', array (
'label' => 'Region code'
) );
$builder->add ( 'description', 'text', array (
'label' => 'Region description'
) );
}
public function getName() {
return 'region';
}
public static function getScriptsIdsArray() {
return array (
array ()
);
}
}
我可以将它用于我的搜索,只需复制它并添加以下方法:
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults ( array (
'validation_groups' => false
) );
}
我的控制器用于在Type版本中编辑表单(标准的Create Update thing):
public function regionUpdateByIdAction($id, Request $request, Application $app) {
if (/*check the user rights*/) {
//find the object being updated
$obj = $app ['dao.region']->findById ( $id );
//create form and handle request
$form = $app ['form.factory']->create ( new RegionType ( $app ), $obj );
$form->handleRequest ( $request );
if ($form->isSubmitted () && $form->isValid ()) {
if ($app ['dao.region']->save ( $obj )) {
//warn user and redirect
$app ['session']->getFlashBag ()->add ( 'success', 'The region was succesfully updated.' );
return $app->redirect ( $app ['url_generator']->generate ( 'region' ) );
} else {
$app ['session']->getFlashBag ()->add ( 'error', 'Something went wrong...' );
}
}
//generate the form view
return $app ['twig']->render ( 'form.html.twig', array (
'form' => $form->createView (),
'title' => 'Edit region'
) );
} else {
$app ['session']->getFlashBag ()->add ( 'error', 'Don\'t have the rights...' );
return $app->redirect ( $app ['url_generator']->generate ( 'home' ) );
}
}
我想做的是使用以下一行
form = $app ['form.factory']->create ( new RegionType ( $app ), $obj );
并在其中添加一个选项,允许传递选项,例如
$form = $app ['form.factory']->create ( new RegionType ( $app ), $obj, array (
'validation_groups' => false
) );
调试时,我发现我的选项归结为规范化器,validation_groups
规范化器将其从false
更改为空array
,OptionsResolver.php
的第962行,使用以下堆栈跟踪(不会发生错误,只是为了显示调用堆栈)。
C:/path/to/app/Easytrip2/vendor/symfony/options-resolver/OptionsResolver.php.Symfony\Component\OptionsResolver\OptionsResolver->offsetGet(): lineno 944
C:/path/to/app/Easytrip2/vendor/symfony/options-resolver/OptionsResolver.php.Symfony\Component\OptionsResolver\OptionsResolver->resolve(): lineno 791
C:/path/to/app/Easytrip2/vendor/symfony/form/ResolvedFormType.php.Symfony\Component\Form\ResolvedFormType->createBuilder(): lineno 104
C:/path/to/app/Easytrip2/vendor/symfony/form/Extension/DataCollector/Proxy/ResolvedTypeDataCollectorProxy.php.Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy->createBuilder(): lineno 83
C:/path/to/app/Easytrip2/vendor/symfony/form/FormFactory.php.Symfony\Component\Form\FormFactory->createNamedBuilder(): lineno 87
C:/path/to/app/Easytrip2/vendor/symfony/form/FormFactory.php.Symfony\Component\Form\FormFactory->createBuilder(): lineno 67
C:/path/to/app/Easytrip2/vendor/symfony/form/FormFactory.php.Symfony\Component\Form\FormFactory->create(): lineno 39
C:/path/to/app/Easytrip2/src/Controller/RegionController.php.Easytrip2\Controller\RegionController->regionSearchAction(): lineno 39
C:/path/to/app/Easytrip2/vendor/symfony/http-kernel/HttpKernel.php.call_user_func_array:{D:\users\F001780\Desktop\soft\wamp\www\Easytrip2\vendor\symfony\http-kernel\HttpKernel.php:139}(): lineno 139
C:/path/to/app/Easytrip2/vendor/symfony/http-kernel/HttpKernel.php.Symfony\Component\HttpKernel\HttpKernel->handleRaw(): lineno 139
C:/path/to/app/Easytrip2/vendor/symfony/http-kernel/HttpKernel.php.Symfony\Component\HttpKernel\HttpKernel->handle(): lineno 62
C:/path/to/app/Easytrip2/vendor/silex/silex/src/Silex/Application.php.Silex\Application->handle(): lineno 586
C:/path/to/app/Easytrip2/vendor/silex/silex/src/Silex/Application.php.Silex\Application->run(): lineno 563
C:/path/to/app/Easytrip2/web/index.php.{main}(): lineno 10
有人可以解释这种行为吗?我在机械设备中稍微迷失了......
我忘记了我的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.*",
"symfony/monolog-bridge": "*",
"box/spout": "*"
},
"require-dev": {
"phpunit/phpunit": "*",
"symfony/browser-kit": "*",
"symfony/css-selector": "*",
"silex/web-profiler": "*"
},
"autoload":{
"psr-4":{"Easytrip2\\": "src"}
},
"autoload-dev":{
"psr-4":{"Easytrip2\\": "tests"}
}
}
答案 0 :(得分:0)
好的,我在这里犯了一个错误...... 感谢Max P.,我找到了它。
实际上,使用此配置,服务器不会进行任何验证。除了validation_groups
设置为false(禁用服务器验证)之外,我还必须将require
选项设置为false,以确保没有必要的标记发送到浏览器。
所以更换:
$form = $app ['form.factory']->create ( new RegionType ( $app ), $obj, array (
'validation_groups' => false
) );
by:
$form = $app ['form.factory']->create ( new RegionType ( $app ), $obj, array (
'validation_groups' => false,
'required' => false
) );
在浏览器和服务器端都有效。
谢谢!