为symfony表单输出必需的字段指示符

时间:2011-03-01 01:34:16

标签: symfony1 symfony-forms

我在symfony中配置了一些表单。我需要的一件事是在所需的字段旁边有一个星号(*)或其他指示符。这些字段都被设置为表单框架中的必需字段,并在提交表单时返回“此字段是必需的”错误,但在提交表单之前我想要一个指示符。

如果有办法在不手动覆盖每个字段的标签的情况下执行此操作?

4 个答案:

答案 0 :(得分:1)

这是Kris Wallsmith's blog中的自动解决方案:

lib / formatter / RequiredLabelsFormatterTable.class.php,这将为必填字段的标签添加“必需”类

<?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);
  }
}

lib / form / BaseForm.class.php,这是项目中所有表单的公共基类:

  protected function getRequiredFields(sfValidatorSchema $validatorSchema = null, $format = null)
  {
    if (is_null($validatorSchema)) {
      $validatorSchema = $this->validatorSchema;
    }

    if (is_null($format)) {
      $format = $this->widgetSchema->getNameFormat();
    }

    $fields = array();

    foreach ($validatorSchema->getFields() as $name => $validator) {
      $field = sprintf($format, $name);
      if ($validator instanceof sfValidatorSchema) {
        // recur
        $fields = array_merge(
          $fields,
          $this->getRequiredFields($validator, $field.'[%s]')
        );
      } else if ($validator->getOption('required')) {
        // this field is required
        $fields[] = $field;
      }
    }

    return $fields;
  }  

__construct()方法中将以下几行添加到BaseForm:

$this->widgetSchema->addOption("required_fields", $this->getRequiredFields());
$this->widgetSchema->addFormFormatter('table',
  new RequiredLabelsFormatterTable($this->widgetSchema)
);   

毕竟,所有标签都将包含required类,请使用您需要的任何css标记给用户。

答案 1 :(得分:0)

您可以将字段的类设置为sfWidget

constructor的一部分

$this->widgetSchema['form_field'] = new sfWidgetFormInput(array(), array('class' => 'required_field'));

注意:这假设你没有使用古代的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;
}

答案 2 :(得分:0)

原始食谱中更简单的解决方案怎么样 - 只需几条线:

http://symfony.com/doc/2.1/cookbook/form/form_customization.html#adding-a-required-asterisk-to-field-labels

答案 3 :(得分:0)

我是用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>');
        }
    }
});