2017-06-29 44 views
0

我有兩個字段:年份的選項和Articles的輸入字段。當用戶不在輸入字段中編寫任何文章選擇時,這意味着它應該在一年內顯示所有文章。我通過多種選擇來改變這個輸入字段,我只是添加了我的文章的名稱。在此修改後,我不添加選項ALL。這下從我的路小碼:如何爲多項選擇添加empty_value

舊代碼:

->add('lru', 'text', array(
    'data' => '', 
    'required' => FALSE)) 

新代碼:

->add('lru', 'choice', array(
'choices' => array(
'\'ATSU\'' => 'ATSU', 
. 
. 
. 
), 
'required' => FALSE, 
'empty_value' => 'ALL', 
'empty_data' => NULL, 
'multiple' => TRUE 

我沒有「empty_value」的研究,我發現,此選項僅適用如果兩個擴展和多個選項設置爲false。我改變了

'multiple' => FALSE 

它變得很好。 我的圖書館是:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Session\Session; 

這下我的form小碼:

$user = $app['security']->getToken()->getUser(); 
     $default = 'ALL'; 
     $form = $app['form.factory']->createBuilder('form')->setMethod('GET') 
       ->add('currency', 'choice', array(
        'choices' => array(
         'USD' => 'Dollar', 
         'EUR' => 'Euro'), 
       )) 
       ->add('nature', 'choice', array(
        'choices' => array(.....), 

       ->add('year', 'choice', array(
        'choices' => array(date('Y') => date('Y'), 
         date('Y') - 1 => date('Y') - 1, 
         date('Y') - 2 => date('Y') - 2, 
         date('Y') - 3 => date('Y') - 3, 
         date('Y') - 4 => date('Y') - 4, 
         'Yearly' => 'Yearly'), 
        'required' => TRUE, 
       )) 
        ->add('lru', 'choice', array(
        'choices' => array(

         // I want add the default value ALL here but I don't understand how 


        '\'ATSU\'' => 'ATSU', ....) 


        'required' => FALSE, 
        'empty_value' => 'ALL', 
        'empty_data' => NULL, 
        'multiple' => TRUE 
        )) 

請告訴我我怎樣才能改變我的代碼,我把我的選擇。multiple choice。謝謝。

回答

1

只需傳遞一個默認對象或數組以形成構建器。

看看https://symfony.com/doc/current/components/form.html#setting-default-values

在你的情況下,它可以是這樣的:

<?php 

$user = $app['security']->getToken()->getUser(); 
     $default = 'ALL'; 
     $form = $app['form.factory']->createBuilder('form',['lru' => [$default]])->setMethod('GET') 
       ->add('currency', 'choice', array(
        'choices' => array(
         'USD' => 'Dollar', 
         'EUR' => 'Euro'), 
       )) 
       ->add('nature', 'choice', array(
        'choices' => array(.....), 

       ->add('year', 'choice', array(
        'choices' => array(date('Y') => date('Y'), 
         date('Y') - 1 => date('Y') - 1, 
         date('Y') - 2 => date('Y') - 2, 
         date('Y') - 3 => date('Y') - 3, 
         date('Y') - 4 => date('Y') - 4, 
         'Yearly' => 'Yearly'), 
        'required' => TRUE, 
       )) 
        ->add('lru', 'choice', array(
        'choices' => array(

         // I want add the default value ALL here but I don't understand how 

        $default => $default, 

        '\'ATSU\'' => 'ATSU', ....) 


        'required' => FALSE, 
        'empty_value' => 'ALL', 
        'empty_data' => NULL, 
        'multiple' => TRUE 
        )) 

我希望這有助於。

+0

謝謝你的回答,這似乎是一個很好的解決方案,但我很抱歉,我不明白我應該如何添加它。因爲我想添加一個默認值「ALL」。我改變了我的問題上面你可以看到我的表格,請建議我怎麼做。 – vero

+0

請給我一些建議。 – vero

+0

檢查出它,回答更新 –

相關問題