【问题标题】:Form themeing show pictures表格主题展示图片
【发布时间】:2013-02-25 02:16:01
【问题描述】:

我去了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') ?>
 />

我怎么能传递一个变量,在我的例子中,用户名(`$fb_username[0] = 'username' of facebook to 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> 

【问题讨论】:

    标签: symfony symfony-2.1 symfony-forms


    【解决方案1】:

    在尝试使用“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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2019-05-11
      相关资源
      最近更新 更多