Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an automatic solution found in <a href="http://kriswallsmith.net/post/144021872/symfony-denote-required-form-fields" rel="nofollow">Kris Wallsmith's blog</a>:</p> <p>lib/formatter/RequiredLabelsFormatterTable.class.php, this will add a 'required' class to the labels of required fields</p> <pre><code>&lt;?php class RequiredLabelsFormatterTable extends sfWidgetFormSchemaFormatterTable { protected $requiredLabelClass = 'required'; public function generateLabel($name, $attributes = array()) { // loop up to find the "required_fields" option $widget = $this-&gt;widgetSchema; do { $requiredFields = (array) $widget-&gt;getOption('required_fields'); } while ($widget = $widget-&gt;getParent()); // add a class (non-destructively) if the field is required if (in_array($this-&gt;widgetSchema-&gt;generateName($name), $requiredFields)) { $attributes['class'] = isset($attributes['class']) ? $attributes['class'].' '.$this-&gt;requiredLabelClass : $this-&gt;requiredLabelClass; } return parent::generateLabel($name, $attributes); } } </code></pre> <p>lib/form/BaseForm.class.php, this is the common base class for all the forms in your project:</p> <pre><code> protected function getRequiredFields(sfValidatorSchema $validatorSchema = null, $format = null) { if (is_null($validatorSchema)) { $validatorSchema = $this-&gt;validatorSchema; } if (is_null($format)) { $format = $this-&gt;widgetSchema-&gt;getNameFormat(); } $fields = array(); foreach ($validatorSchema-&gt;getFields() as $name =&gt; $validator) { $field = sprintf($format, $name); if ($validator instanceof sfValidatorSchema) { // recur $fields = array_merge( $fields, $this-&gt;getRequiredFields($validator, $field.'[%s]') ); } else if ($validator-&gt;getOption('required')) { // this field is required $fields[] = $field; } } return $fields; } </code></pre> <p>add the following few lines to BaseForm as well, in the <code>__construct()</code> method:</p> <pre><code>$this-&gt;widgetSchema-&gt;addOption("required_fields", $this-&gt;getRequiredFields()); $this-&gt;widgetSchema-&gt;addFormFormatter('table', new RequiredLabelsFormatterTable($this-&gt;widgetSchema) ); </code></pre> <p>After all this, all your labels will have the <code>required</code> class, use whatever css you need to mark it to the user.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload