Development

Changeset 11814

You must first sign up to be able to contribute.

Changeset 11814

Show
Ignore:
Timestamp:
09/27/08 12:42:29 (6 years ago)
Author:
nicolas
Message:

new method added to the class, allowing to render the form using a particular widget schema formatter within a template

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r11805 r11814  
    336336    <?php endif; ?> 
    337337 
     338A new `renderUsing` method has been added to the `sfForm` class, allowing to  
     339render the form using a particular widget schema formatter. This allows to 
     340use a formatter directly within a template: 
     341 
     342    [php] 
     343    // in a template, the form will be rendered using the "list" form formatter  
     344    echo $form->renderUsing('list'); 
     345 
    338346Validators 
    339347---------- 
  • branches/1.2/lib/form/sfForm.class.php

    r11708 r11814  
    120120 
    121121  /** 
     122   * Renders the widget schema using a specific form formatter 
     123   * 
     124   * @param  string  $formatterName  The form formatter name 
     125   * @param  array   $attributes     An array of HTML attributes 
     126   * 
     127   * @return string The rendered widget schema 
     128   */ 
     129  public function renderUsing($formatterName, $attributes = array()) 
     130  { 
     131    $currentFormatterName = $this->widgetSchema->getFormFormatterName(); 
     132 
     133    $formatterClass = sprintf('sfWidgetFormSchemaFormatter%s', ucfirst($formatterName)); 
     134 
     135    if (!class_exists($formatterClass)) 
     136    { 
     137      throw new InvalidArgumentException(sprintf('Formatter "%s" (class "%s") does not exist', $formatterName, $formatterClass)); 
     138    } 
     139 
     140    $this->widgetSchema->setFormFormatterName($formatterName); 
     141 
     142    $output = $this->render($attributes); 
     143 
     144    $this->widgetSchema->setFormFormatterName($currentFormatterName); 
     145 
     146    return $output; 
     147  } 
     148 
     149  /** 
    122150   * Renders global errors associated with this form. 
    123151   * 
  • branches/1.2/test/unit/form/sfFormTest.php

    r11701 r11814  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(106, new lime_output_color()); 
     13$t = new lime_test(108, new lime_output_color()); 
    1414 
    1515class FormTest extends sfForm 
     
    321321$t->is($f->render(array('first_name' => array('class' => 'foo'))), $output, '->render() renders the form as HTML'); 
    322322 
     323// renderUsing() 
     324$t->diag('->renderUsing()'); 
     325$f = new sfForm(); 
     326$f->setWidgets(array('name' => new sfWidgetFormInput())); 
     327$output = <<<EOF 
     328<li> 
     329  <label for="name">Name</label> 
     330  <input type="text" name="name" id="name" /> 
     331</li> 
     332 
     333EOF; 
     334$t->is($f->renderUsing('list'), $output, 'renderUsing() renders the widget schema using the given form formatter'); 
     335$t->is($f->getWidgetSchema()->getFormFormatterName(), 'table', 'renderUsing() does not persist form formatter name for the current form instance'); 
     336 
    323337// ->embedForm() 
    324338$t->diag('->embedForm()');