2011-03-01 7 views
1

私はsymfonyでいくつかのフォームを設定しました。私が必要とするものは、必要なフィールドの隣にアスタリスク(*)またはその他のインジケータを付けることです。フィールドはすべてフレームワークを構成する必須のintに設定され、フォームの送信時には「このフィールドが必要です」というエラーが返されますが、フォームが送信される前にインジケータが必要です。symfonyフォームに必要なフィールドインジケータを出力する

手動で各フィールドのラベルを上書きせずにこれを行う方法があれば、あなたはsfWidget

すなわち

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

ノートのconstructorの一部としてフィールドのクラスを設定することができます

答えて

1

はここKris Wallsmith's blogで見つかった自動ソリューションです:

のlib /フォーマッタ/ 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に/フォーム/ 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; 
    } 

はBに次の数行を追加aseFormは、同様に、__construct()方法で:

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

このすべての後に、すべてのラベルは、ユーザーにそれをマークする必要があるものは何でもCSS requiredクラス、使用しています。

+0

+1良い解決策;私がこれをKris Wallsmithのブログで見たと思います。使い方が簡単ではありません。最後のスニペットを 'BaseFormDoctrine :: __ construct(...)'に追加することもできます。つまり、すべてのモデルフォームで自動的に使用することができます。 – richsage

+0

@richsageここに、[Kris Wallsmithの主題に関するブログ記事](http://kriswallsmith.net/post/144021872/symfony-denote-required-form-fields)があります。 –

+0

それは私がそれを得たところです!私はそれをどこかに見つけることを思い出しました、私は今、私の古いプロジェクトの1つからそれを得ました。ソースに感謝します。 – Maerlyn

0

:これはあなたが

(1.0 ALA)古代sfForms上じゃないと仮定しています

更新 ここにtechcorus.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; 
} 
+0

クラスは私にビジュアルを与えることはありません私がフィールド自体を違ったものにしない限り、それは変わってしまいます。また、各フィールドに行を追加したい場合は、ラベルを設定するだけです。 – MrGlass

+1

@MrGlass cssを使って、すべての要素の外観を変更することができます。私はCSSの例で答えを更新しました – Patrick

+0

また、ブラウザ(IE7はブラウザではありません)のCSS疑似セレクタを使用することができます。 。必要なラベル:{ の内容: "*" } またはそのようなもの。 – maectpo

0

$('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>'); 
     } 
    } 
}); 
関連する問題