表格主题展示图片

时间:2013-03-09 15:45:40

标签: symfony symfony-2.1 symfony-forms

我去了this tutorial。我得到了工作的榜样。但是我没有得到它,因为我不能为我的情况定制它。

我想为我的每个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') ?>
 />

我怎么能传递一个变量,在我的情况下,用户名(facebook的`$ fb_username [0] ='用户名'到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> 

1 个答案:

答案 0 :(得分: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类的实例,而不是数组。

有点晚了,但希望这会有所帮助。

BTW:我正在使用Symfony 2.3