Development

Changeset 6326

You must first sign up to be able to contribute.

Changeset 6326

Show
Ignore:
Timestamp:
12/05/07 15:32:24 (5 years ago)
Author:
fabien
Message:

added option support to sfForm

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/form/sfForm.class.php

    r6196 r6326  
    3737    $taintedValues   = array(), 
    3838    $values          = null, 
    39     $defaults        = array(); 
     39    $defaults        = array(), 
     40    $options         = array(); 
    4041 
    4142  /** 
     
    4344   * 
    4445   * @param array  An array of field default values 
     46   * @param array  An array of options 
    4547   * @param string A CSRF secret (false to disable CSRF protection, null to use the global CSRF secret) 
    4648   */ 
    47   public function __construct($defaults = array(), $CSRFSecret = null) 
     49  public function __construct($defaults = array(), $options = array(), $CSRFSecret = null) 
    4850  { 
    4951    $this->setDefaults($defaults); 
     52    $this->options = $options; 
    5053 
    5154    $this->validatorSchema = new sfValidatorSchema(); 
     
    300303 
    301304  /** 
     305   * Sets an option value. 
     306   * 
     307   * @param string The option name 
     308   * @param mixed  The default value 
     309   */ 
     310  public function setOption($name, $value) 
     311  { 
     312    $this->options[$name] = $value; 
     313  } 
     314 
     315  /** 
     316   * Gets an option value. 
     317   * 
     318   * @param string The option name 
     319   * @param mixed  The default value (null by default) 
     320   * 
     321   * @param mixed  The default value 
     322   */ 
     323  public function getOption($name, $default = null) 
     324  { 
     325    return isset($this->options[$name]) ? $this->options[$name] : $default; 
     326  } 
     327 
     328  /** 
    302329   * Sets a default value for a form field. 
    303330   * 
  • trunk/test/unit/form/sfFormTest.php

    r6197 r6326  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(56, new lime_output_color()); 
     13$t = new lime_test(58, new lime_output_color()); 
    1414 
    1515class FormTest extends sfForm 
     
    3737$t->is($f->getDefaults(), array('first_name' => 'Fabien'), '__construct() can take an array of default values as its first argument'); 
    3838 
    39 $f = new FormTest(array(), 'secret'); 
     39$f = new FormTest(array(), array(), 'secret'); 
    4040$v = $f->getValidatorSchema(); 
    4141$t->ok($f->isCSRFProtected(), '__construct() takes a CSRF secret as its second argument'); 
     
    4343 
    4444sfForm::enableCSRFProtection(); 
    45 $f = new FormTest(array(), false); 
     45$f = new FormTest(array(), array(), false); 
    4646$t->ok(!$f->isCSRFProtected(), '__construct() can disable the CSRF protection by passing false as the second argument'); 
    4747 
    4848$f = new FormTest(); 
    4949$t->ok($f->isCSRFProtected(), '__construct() uses CSRF protection if null is passed as the second argument and it\'s enabled globally'); 
     50 
     51// ->getOption() ->setOption() 
     52$t->diag('->getOption() ->setOption()'); 
     53$f = new FormTest(array(), array('foo' => 'bar')); 
     54$t->is($f->getOption('foo'), 'bar', '__construct takes an option array as its second argument'); 
     55$f->setOption('bar', 'foo'); 
     56$t->is($f->getOption('bar'), 'foo', '->setOption() changes the value of an option'); 
    5057 
    5158sfForm::disableCSRFProtection(); 
     
    7582$t->ok(!$f2->isCSRFProtected(),'::disableCSRFProtection() disables CSRF protection for all future forms'); 
    7683 
    77 $f = new FormTest(array(), false); 
     84$f = new FormTest(array(), array(), false); 
    7885$t->ok(!$f->isCSRFProtected(),'->isCSRFProtected() returns true if the form is CSRF protected'); 
    7986