0

我去了这个教程。我得到了这个例子。但我不明白,因为我无法为我的情况定制它。

我想为我的每个 facebook 朋友显示一张图片,并将个人资料图片的地址从 facebook 传递到复选框元素前面的图像标签。

更新

我行动的一部分:

//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/views/Form/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 到 from_widget_pics.html.php 以及如何在我的 aciton 中使用表单 bulider 显示它:

<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> 
4

1 回答 1

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.

二手车类型:

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

于 2013-07-02T08:59:54.807 回答