0

我按照这里的描述制作了表格。

但它总是 $form->isValid() 总是返回 false。

$searchTime = new SearchTime();//SearchTime is my entity name

    $form = $this->createFormBuilder($searchTime)
            ->add('fromDate','datetime')
            ->add('toDate','datetime')             
            ->getForm(); 

    $request = $this->get('request');

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

         var_dump($form->isValid());
        if ($form->isValid()) {

我认为 $request->getMethod() 正确返回 'POST',但 $form->bindRequest($request) 不知何故不起作用。

有人对此有提示吗?

我的 $request 转储在这里

object(Symfony\Component\HttpFoundation\Request)#6 (20) { ["attributes"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#9 (1) { ["parameters":protected]=> array(3) { ["_controller"]=> string(67) "Acme\MemberBundle\Controller\DefaultController::searchtimerepAction" ["_route"]=> string(25) "acme_member_searchtimerep" ["_route_params"]=> array(0) { } } } ["request"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) { ["parameters":protected]=> array(1) { ["form"]=> array(2) { ["fromDate"]=> array(2) { ["date"]=> array(3) { ["year"]=> string(2) "13" ["month"]=> string(1) "1" ["day"]=> string(1) "1" } ["time"]=> array(2) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "0" } } ["toDate"]=> array(2) { ["date"]=> array(3) { ["year"]=> string(2) "13" ["month"]=> string(1) "1" ["day"]=> string(1) "1" } ["time"]=> array(2) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "0" } } } } } ["query"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#8 (1) { ["parameters":protected]=> array(0) { } } ["server"]=> object(Symfony\Component\HttpFoundation\ServerBag)#12 (1) { ["parameters":protected]=> array(37) { ["HTTP_HOST"]=> string(9) "localhost" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["CONTENT_LENGTH"]=> string(3) "405" ["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" ["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ["HTTP_ORIGIN"]=> string(16) "http://localhost" ["HTTP_USER_AGENT"]=> string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36" ["CONTENT_TYPE"]=> string(33) "application/x-www-form-urlencoded" ["HTTP_REFERER"]=> string(53) "http://localhost/mutor/web/app_dev.php/member/newclass" ["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip,deflate,sdch" ["HTTP_ACCEPT_LANGUAGE"]=> string(23) "en-US,en;q=0.8,ja;q=0.6" ["HTTP_COOKIE"]=> string(36) "PHPSESSID=19i20qa86ia4n7qqgfecfra9s6" ["PATH"]=> string(29) "/usr/bin:/bin:/usr/sbin:/sbin" ["SERVER_SIGNATURE"]=> string(0) "" ["SERVER_SOFTWARE"]=> string(86) "Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch mod_ssl/2.2.22 OpenSSL/0.9.8r" ["SERVER_NAME"]=> string(9) "localhost" ["SERVER_ADDR"]=> string(3) "::1" ["SERVER_PORT"]=> string(2) "80" ["REMOTE_ADDR"]=> string(3) "::1" ["DOCUMENT_ROOT"]=> string(22) "/Users/whitebear/httproot" ["SERVER_ADMIN"]=> string(15) "you@example.com" ["SCRIPT_FILENAME"]=> string(43) "/Users/whitebear/httproot/mutor/web/app_dev.php" ["REMOTE_PORT"]=> string(5) "64695" ["REMOTE_USER"]=> string(4) "mutor" ["AUTH_TYPE"]=> string(5) "Basic" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["REQUEST_METHOD"]=> string(4) "POST" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(42) "/mutor/web/app_dev.php/member/searchtimerep" ["SCRIPT_NAME"]=> string(21) "/mutor/web/app_dev.php" ["PATH_INFO"]=> string(21) "/member/searchtimerep" ["PATH_TRANSLATED"]=> string(43) "/Users/whitebear/httproot/member/searchtimerep" ["PHP_SELF"]=> string(42) "/mutor/web/app_dev.php/member/searchtimerep" ["PHP_AUTH_USER"]=> string(4) "mutor" ["PHP_AUTH_PW"]=> string(8) "mutormutor" ["REQUEST_TIME"]=> int(1370746992) } } ["files"]=> object(Symfony\Component\HttpFoundation\FileBag)#11 (1) { ["parameters":protected]=> array(0) { } } ["cookies"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#10 (1) { ["parameters":protected]=> array(1) { ["PHPSESSID"]=> string(26) "19i20qa86ia4n7qqgfecfra9s6" } } ["headers"]=> object(Symfony\Component\HttpFoundation\HeaderBag)#13 (2) { ["headers":protected]=> array(16) { ["host"]=> array(1) { [0]=> string(9) "localhost" } ["connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["content-length"]=> array(1) { [0]=> string(3) "405" } ["cache-control"]=> array(1) { [0]=> string(9) "max-age=0" } ["accept"]=> array(1) { [0]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" } ["origin"]=> array(1) { [0]=> string(16) "http://localhost" } ["user-agent"]=> array(1) { [0]=> string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36" } ["content-type"]=> array(1) { [0]=> string(33) "application/x-www-form-urlencoded" } ["referer"]=> array(1) { [0]=> string(53) "http://localhost/mutor/web/app_dev.php/member/newclass" } ["accept-encoding"]=> array(1) { [0]=> string(17) "gzip,deflate,sdch" } ["accept-language"]=> array(1) { [0]=> string(23) "en-US,en;q=0.8,ja;q=0.6" } ["cookie"]=> array(1) { [0]=> string(36) "PHPSESSID=19i20qa86ia4n7qqgfecfra9s6" } ["php-auth-user"]=> array(1) { [0]=> string(4) "mutor" } ["php-auth-pw"]=> array(1) { [0]=> string(8) "mutormutor" } ["authorization"]=> array(1) { [0]=> string(26) "Basic bXV0eTptdXR5bXV0eQ==" } ["x-php-ob-level"]=> array(1) { [0]=> int(1) } } ["cacheControl":protected]=> array(1) { ["max-age"]=> string(1) "0" } } ["content":protected]=> NULL ["languages":protected]=> NULL ["charsets":protected]=> NULL ["acceptableContentTypes":protected]=> NULL ["pathInfo":protected]=> string(21) "/member/searchtimerep" ["requestUri":protected]=> string(42) "/mutor/web/app_dev.php/member/searchtimerep" ["baseUrl":protected]=> string(21) "/mutor/web/app_dev.php" ["basePath":protected]=> NULL ["method":protected]=> string(4) "POST" ["format":protected]=> NULL ["session":protected]=> object(Symfony\Component\HttpFoundation\Session\Session)#86 (3) { ["storage":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage)#85 (5) { ["bags":protected]=> array(2) { ["attributes"]=> object(Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag)#81 (3) { ["name":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=> string(10) "attributes" ["storageKey":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=> string(15) "_sf2_attributes" ["attributes":protected]=> &array(1) { ["_security_main"]=> string(561) "C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":473:{a:3:{i:0;N;i:1;s:4:"main";i:2;s:433:"a:4:{i:0;C:27:"Acme\UserBundle\Entity\User":226:{a:9:{i:0;s:88:"HdHpg1j7fuEKJwBG3A7cbTVv3yydr/b9B1inKtO/diIj35QPiKt1L4U/wmPA6uhLO4SY0ICol26yVFPcTPEdfA==";i:1;s:31:"23l9rnvin55w4cw8cg4sg08woggk4cc";i:2;s:7:"student";i:3;s:7:"student";i:4;b:0;i:5;b:0;i:6;b:0;i:7;b:1;i:8;i:22;}}i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:9:"ROLE_USER";}}i:3;a:0:{}}";}}" } } ["flashes"]=> object(Symfony\Component\HttpFoundation\Session\Flash\FlashBag)#80 (3) { ["name":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> string(7) "flashes" ["flashes":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> &array(0) { } ["storageKey":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> string(12) "_sf2_flashes" } } ["started":protected]=> bool(true) ["closed":protected]=> bool(false) ["saveHandler":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy)#82 (3) { ["wrapper":protected]=> bool(false) ["active":protected]=> bool(false) ["saveHandlerName":protected]=> string(5) "files" } ["metadataBag":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)#83 (4) { ["name":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> string(10) "__metadata" ["storageKey":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> string(9) "_sf2_meta" ["meta":protected]=> &array(3) { ["u"]=> int(1370746993) ["c"]=> int(1370707495) ["l"]=> string(1) "0" } ["lastUsed":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> int(1370746973) } } ["flashName":"Symfony\Component\HttpFoundation\Session\Session":private]=> string(7) "flashes" ["attributeName":"Symfony\Component\HttpFoundation\Session\Session":private]=> string(10) "attributes" } ["locale":protected]=> string(2) "ja" ["defaultLocale":protected]=> string(2) "ja" }

我有点孤立这个问题。

$form = $this->createFormBuilder($searchTime, array(
                'csrf_protection' => false,     // disables CSRF for the form
            ))               
        ->add('fromDate','datetime')
        ->add('toDate','datetime')             
        ->getForm(); 


$request = $this->get('request');
var_dump($request);// it contains PostData correctly,I can see there is 'fromDate' and value. 

$form->bindRequest($request);//I am trying to bind postdata with form...
$form->get('fromDate')->getData(); // it returns null......

额外的

var_dump($request->request) 似乎返回表单数据....

object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) {
      ["parameters":protected]=>
      array(1) {
        ["form"]=>
        array(2) {
          ["fromDate"]=>
          array(2) {
            ["date"]=>
            array(3) {
              ["year"]=>
              string(2) "13"
              ["month"]=>
              string(1) "1"
              ["day"]=>
              string(1) "1"
            }
            ["time"]=>
            array(2) {
              ["hour"]=>
              string(1) "0"
              ["minute"]=>
              string(1) "0"
            }
          }
          ["toDate"]=>
          array(2) {
            ["date"]=>
            array(3) {
              ["year"]=>
              string(2) "13"
              ["month"]=>
              string(1) "1"
              ["day"]=>
              string(1) "1"
            }
            ["time"]=>
            array(2) {
              ["hour"]=>
              string(1) "0"
              ["minute"]=>
              string(1) "0"
            }
          }
        }
      }
    }

问题已解决

制作表格时

    $years = array(date("y", strtotime("now")),date("y", strtotime("+1 year")));
    $form = $this->createFormBuilder($searchTime,array(
'csrf_protection' => false,     // disables CSRF for the form
        ))
        ->add('fromDate',null,array(
            'label' => 'start time',
            'years' => $years
            'minutes' => array('0','30')
            ))

我使用 $years = array(date("y", strtotime("now")),date("y", strtotime("+1 year"))); 将年份选项限制为 2013,2014

但它返回 array(13,14) 而不是 (2013,2014)

所以 form->bindrequest 不起作用

4

1 回答 1

2

您可能默认启用了 CSRF 保护,导致您的表单无效,请尝试

$form = $this->createFormBuilder($searchTime, array(
    'csrf_protection' => false,     // disables CSRF for the form
))->add(...)

config.yml 中的常规配置设置可以在framework.csrf_protection下找到

framework:
    # ...
    csrf_protection:
        enabled:      false

否则调试你得到哪个错误:

 $form->getErrors()

确保您提交正确的值...

["year"]=>string(2) "13"     // year 13 ? .... 2013?

应该是

["year"]=>string(2) "2013"  
于 2013-06-08T21:59:47.950 回答