访问Symfony在树枝视图中声明
问题描述:
尝试获取视图中实体中定义的断言值。访问Symfony在树枝视图中声明
使用: Symfony的2.8 奏管理员:3.1
我在模型中这样定义的断言:
/**
* @var string
* @Assert\Length(
* min = 8,
* max = 60,
* minMessage = "Longer than {{ limit }}",
* maxMessage = "Shorter than {{ limit }}"
*)
* @ORM\Column(name="title", type="string", length=60)
*/
private $title;
管理形式使用定制formtype加载定制视图:
<?php
namespace CustomBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TextCountType extends AbstractType
{
public function getParent()
{
return TextType::class;
}
}
而且视图:
{% block text_count_widget %}
{% spaceless %}
{{ block('form_widget_simple') }}
<script>
// some custom live validation
// HERE i need access to the Asserts
</script>
{% endspaceless %}
{% endblock %}
我的问题是: 有没有办法访问树枝代码中的断言? 我不想在每个实体的管理员类中重新定义它。
谢谢你的帮助。
答
很难说如果你没有附加form builder,它将如何完全看起来像,但获取这些变量(如果它们存在)的最佳方法是转储表单对象并观察所有变量。
{{ dump(form) }}
无论如何,你不应该遵循这种方法。最好的选择是编写一个类formBuilder,并在attrs中添加验证约束。取决于类型,它们可能是there和内部类型文档中可能获得的完整类型列表,应该有可用于所选类型的完整属性列表。