2013-03-09 56 views
0

我去了this tutorial。我得到了榜样去工作。但我不明白,因为我无法为我的情况定制它。形式主题展示图片

我想为我的每个脸书朋友显示一张图片,并将脸谱资料图片的地址传递给复选框元素前面的图片标签。

我的行动更新

部分:

//the array is on part of my update. 
$choice_test = array(); 
$choice_test[] = array('id' => 1, 'username' => 'test'); 
$choice_test[] = array('id' => 2, 'username' => 'test2'); 

->add('friend', 'choice', array(
    'required' => true, 
    'expanded' => true, 
    'choice_list' => $choice_test, //this is my update 
    'choices' => $fb_friends_form, //$fb_friends_form[1]= 'First Lastane'; 
    'multiple' => true, 
    'constraints' => array(new CheckChoicesFbFriends(array('fb_friends_form' => $fb_friends_form))), 
    'mapped' => true 
    )) 

return $this->render('FrontendChancesBundle::createrequest.html.php', array(
       'form' => $form->createView())); 

模板:

<?php $view['form']->setTheme($form, array('FrontendDemoBundle:Form')) ;?> 
<?php echo $view['form']->widget($form['friend'])?> 
在FrontendDemoBundle/Ressources

/视图/表格/ form_widget_pics.html.php:

<input 
type="<?php echo isset($type) ? $view->escape($type) : 'checkbox' ?>" 
<?php if (!empty($value)): ?>value="<?php echo $view->escape($value) ?>"<?php endif ?> 
<?php echo $view['form']->block($form, 'checkbox_widget') ?> 
/> 

我怎么能传递一个变量,在我的情况下,用户名('$ fb_username [0] ='username'of facebook to from_widget_pics.html.php,以及如何使用表单bulider在我的aciton中显示它:

<img src="www.facebook.com/+FirstLastname> 
<input type="checkbox" 
    id="form_friend_0" 
    name="form[friend][]" 
    value="1"/> 
<label for="form_friend_0" >First Lastname</label> 
<img src="www.facebook.com/+nextfriend> 

回答

0

当尝试使用'choice_list'选项将自定义选定对象的数组或集合传递给表单时,我有非常相同的异常消息。

我解决这种方式(简化的示例):

控制器代码:

use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; 


class UsedCarsController extends Controller { 

..... action code: 

$usedCars = $carRepository->findAllUsed(); 

$choiceList = new SimpleChoiceList($usedCars); 

$form = $this->createForm(new UsedCarsType($choiceList),null, array('label' => 'Used cars form')); 

... now render view, etc. 

UsedCarsType:

class UsedCarsType extends AbstractType 
{ 
    private $choicesList; 

public function __construct($choicesList) 
{ 
    $this->choicesList = $choicesList; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('usedCars', 'choice',array('label'=>'Used cars', 
            'required'=>true, 
            'choice_list' => $this->choicesList, 
            'empty_value' => '-- used car --')) 
     ; 
} 

Choice_list选项必须是SimpleChoiceList类的一个实例,而不是一个阵列。

有点晚了,但希望这有助于。

顺便说一句:我正在使用Symfony 2.3