Symfony1 输出symfony表单所需的字段指示符
我在symfony中配置了一些表单。我需要的一件事是在必填字段旁边有一个星号(*)或其他指示符。在表单框架中,所有字段都设置为必填字段,并且在提交表单时返回“this field is required”错误,但在提交表单之前,我需要一个指示符Symfony1 输出symfony表单所需的字段指示符,symfony1,symfony-forms,Symfony1,Symfony Forms,我在symfony中配置了一些表单。我需要的一件事是在必填字段旁边有一个星号(*)或其他指示符。在表单框架中,所有字段都设置为必填字段,并且在提交表单时返回“this field is required”错误,但在提交表单之前,我需要一个指示符 如果有任何方法可以在不手动覆盖每个字段的标签的情况下执行此操作?您可以将字段的类设置为sfWidget i、 e 注意:这是假设您不在古老的sfForms(ala 1.0)上 更新 下面是techchorus.net中的一些CSS代码,用于显示所需的星号
如果有任何方法可以在不手动覆盖每个字段的标签的情况下执行此操作?您可以将字段的类设置为
sfWidget
i、 e
注意:这是假设您不在古老的sfForms(ala 1.0)上
更新
下面是techchorus.net中的一些CSS代码,用于显示所需的星号
.required
{
background-image:url(/path/to/your/images/dir/required-field.png);
background-position:top right;
background-repeat:no-repeat;
padding-right:10px;
}
以下是一个自动解决方案: lib/formatter/RequiredLabelsFormatterTable.class.php,这将向必填字段的标签添加一个“required”类
<?php
class RequiredLabelsFormatterTable extends sfWidgetFormSchemaFormatterTable
{
protected
$requiredLabelClass = 'required';
public function generateLabel($name, $attributes = array())
{
// loop up to find the "required_fields" option
$widget = $this->widgetSchema;
do {
$requiredFields = (array) $widget->getOption('required_fields');
} while ($widget = $widget->getParent());
// add a class (non-destructively) if the field is required
if (in_array($this->widgetSchema->generateName($name), $requiredFields)) {
$attributes['class'] = isset($attributes['class']) ?
$attributes['class'].' '.$this->requiredLabelClass :
$this->requiredLabelClass;
}
return parent::generateLabel($name, $attributes);
}
}
在\u construct()
方法中,也将以下几行添加到BaseForm:
$this->widgetSchema->addOption("required_fields", $this->getRequiredFields());
$this->widgetSchema->addFormFormatter('table',
new RequiredLabelsFormatterTable($this->widgetSchema)
);
在所有这些之后,您的所有标签都将具有
必需的类,使用您需要的任何css将其标记给用户。那么原始食谱中更简单的解决方案呢?只有几行字:
我是用Javascript做的:
$('form').find('select, input, textarea').each(function(){
if($(this).attr('required') == 'required'){
$label = $('label[for='+ $(this).attr('id') +']');
if($label.find('.required-field').length == 0){
$label.append('<span class="required-field">*</span>');
}
}
});
$('form')。查找('select,input,textarea')。每个(函数(){
if($(this).attr('required')=='required'){
$label=$('label[for='+$(this.attr('id')+']);
if($label.find('.required field')。长度==0){
$label.append('*');
}
}
});
类不会给我一个视觉指示器,除非我让字段本身看起来不同,但那会很奇怪。另外,如果我想为每个字段添加一行,我只需设置标签即可。@MrGlass您可以使用css更改所有元素的外观。我已经用css示例更新了答案。另外,对于浏览器(IE7不是浏览器),您可以使用css伪选择器:after和属性内容来为必填字段添加asterix。所需标签:在{内容:“*”}或类似内容之后。+1良好的解决方案;我想我在克里斯·沃史密斯的博客上看到了这个或类似的东西。非常容易使用。您也可以将最后一个代码片段添加到egBaseFormDoctrine::u构造(…)
中,这样所有模型表单都可以自动使用它。@richsage这里是。我就是从这里得到它的!我记得是在什么地方找到的,现在是从我以前的一个项目中得到的。谢谢你的消息来源。
$this->widgetSchema->addOption("required_fields", $this->getRequiredFields());
$this->widgetSchema->addFormFormatter('table',
new RequiredLabelsFormatterTable($this->widgetSchema)
);
$('form').find('select, input, textarea').each(function(){
if($(this).attr('required') == 'required'){
$label = $('label[for='+ $(this).attr('id') +']');
if($label.find('.required-field').length == 0){
$label.append('<span class="required-field">*</span>');
}
}
});