2

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

如果有什么方法可以在不手动覆盖每个字段的标签的情况下做到这一点?

4

4 回答 4

1

这是在Kris Wallsmith 的博客中找到的自动解决方案:

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

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

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

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

于 2011-03-01T08:01:18.880 回答
1

原始食谱中更简单的解决方案怎么样 - 树枝中只有几行:

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

于 2013-03-15T08:04:15.907 回答
0

您可以将字段的类设置sfWidget

IE

$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;
}
于 2011-03-01T02:37:28.153 回答
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>');
        }
    }
});
于 2014-09-17T16:07:03.687 回答