我在symfony 2.8.x中使用表单类型。 我需要在视图中使用表单属性文本。
表单类型:
$builder->add( 'category_id', ChoiceType::class,
array( 'label'=>'Category',
'constraints'=>array( new Regex(array("pattern"=>"'([^0-9]*)$'si",
"message"=>"Required field!"))
),
'choices'=>$categoriesRepo->getAllActive(),
'choices_as_values'=>true,
'choice_label' => 'getName',
'choice_value' => 'getId',
'attr'=>array( 'class'=>'form-control',
'help'=>'Help message.',
)));
在视图中:
<?php echo $view->render("XXXBundle:XXX:form_element.html.php", array('form'=>$templateForm['category_id']))?>
form_element.html.php
<div class="form-group">
<?php echo $view['form']->label($form, null, array('required'=>false, 'label_attr'=>array('class'=>'col-md-3 control-label'))) ?>
<div class="col-md-4">
<?php echo $view['form']->widget($form, array('attr'=>array('help'=>false))) ?>
<span class="help-block error"><?php echo $view['form']->errors($form) ?></span>
<span class="help-block"> HELP_MESSAGE </span>
</div>
</div>
我需要“帮助”属性来查看 HELP_MESSAGE 。 有这个问题的解决方案吗?
谢谢!
答案 0 :(得分:0)
您是我见过的第一个使用PHP文件而不是twig的Symfony用户。祝贺。
您尚未发布代码的真实业务部分,因此很难给出明确的答案,但在控制器操作功能底部的某处会有一个电话到$this->render('XXXBundle:XXX:template.html.php');
Symfony方式&#39;是传递帮助信息:
$help_message = 'My Help Message';
$this->render('XXXBundle:XXX:template.html.php', array('help_message' => $help_message));
在模板文件中:
<?php echo($view['help_message']);?>