Development

Changeset 11383

You must first sign up to be able to contribute.

Changeset 11383

Show
Ignore:
Timestamp:
09/09/08 00:06:56 (5 years ago)
Author:
Kris.Wallsmith
Message:

sfFormtasticPlugin: Added ->toJson() method to error schema class.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfFormtasticPlugin/trunk/lib/form/sfFormtasticBase.class.php

    r11336 r11383  
    104104   * @see sfForm 
    105105   */ 
     106  public function bind(array $taintedValues = null, array $taintedFiles = null) 
     107  { 
     108    parent::bind($taintedValues, $taintedFiles); 
     109     
     110    // replace sfValidatorErrorSchema 
     111    $this->errorSchema = new sfValidatornatorErrorSchema($this->validatorSchema, $this->errorSchema); 
     112  } 
     113   
     114  /** 
     115   * @see sfForm 
     116   */ 
    106117  public function render($attributes = array()) 
    107118  { 
  • plugins/sfFormtasticPlugin/trunk/lib/validator/sfValidatornatorErrorSchema.class.php

    r10206 r11383  
    1414class sfValidatornatorErrorSchema extends sfValidatornatorErrorSchemaBase 
    1515{ 
     16  /** 
     17   * Returns the current error schema serialized as JSON. 
     18   *  
     19   * @return  string 
     20   */ 
     21  public function toJson() 
     22  { 
     23    $callable = sfConfig::get('app_sf_formtastic_plugin_json_encode_callable', 'json_encode'); 
     24    if (!is_callable($callable)) 
     25    { 
     26      throw new RuntimeException(sprintf('The json_encoder_callable "%s" does not exist.', var_export($callable, true))); 
     27    } 
     28     
     29    // build an array of scalars 
     30    $data = array(); 
     31    foreach ($this->globalErrors as $error) 
     32    { 
     33      if (!isset($data['_global'])) 
     34      { 
     35        $data['_global'] = array(); 
     36      } 
     37      $data['_global'][] = $error->getMessage(); 
     38    } 
     39    foreach ($this->getNamedErrors() as $name => $error) 
     40    { 
     41      if (!isset($data[$name])) 
     42      { 
     43        $data[$name] = array(); 
     44      } 
     45      $data[$name][] = $error->getMessage(); 
     46    } 
     47     
     48    return call_user_func($callable, $data); 
     49  } 
    1650} 
  • plugins/sfFormtasticPlugin/trunk/test/unit/sfFormtasticBaseTest.php

    r11337 r11383  
    33include dirname(__FILE__).'/../bootstrap/unit.php'; 
    44 
    5 $t = new lime_test(19, new lime_output_color); 
     5$t = new lime_test(21, new lime_output_color); 
    66 
    77sfForm::enableCSRFProtection('secretastic'); 
     
    5858$form->setIdFormat(false); 
    5959$t->unlike($form->render(), '/id="/', '"->setIdFormat(false)" removes id attribute'); 
     60 
     61$t->diag('->bind()'); 
     62$form->bind(array('name' => 'xyztastic')); 
     63$t->isa_ok($form->getErrorSchema(), 'sfValidatornatorErrorSchema', '->bind() copies native error schema'); 
     64$t->is(count($form->getErrorSchema()), 1, '->bind() copies the correct number of errors');