All pastes #2056983 Raw Edit

Mine

public text v1 · immutable
#2056983 ·published 2011-05-11 19:44 UTC
rendered paste body
<?php
class ProductGalleryValidatorSchema extends sfValidatorSchema
{
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('title', 'The title is required.');
    $this->addMessage('pic', 'The pic is required.');
  }

  protected function doClean($values)
  {
    $errorSchema = new sfValidatorErrorSchema($this);

    foreach($values as $key => $value)
    {
      $errorSchemaLocal = new sfValidatorErrorSchema($this);

      // pic is filled but no title
      if ($value['pic'] && !$value['title'])
      {
        $errorSchemaLocal->addError(new sfValidatorError($this, 'required'), 'title');
      }

      // title is filled but no pic
      if ($value['title'] && !$value['pic'])
      {
        $errorSchemaLocal->addError(new sfValidatorError($this, 'required'), 'pic');
      }

      // no title and no pic, remove the empty values
      if (!$value['pic'] && !$value['title'])
      {
        unset($values[$key]);
      }

      // some error for this embedded-form
      if (count($errorSchemaLocal))
      {
        $errorSchema->addError($errorSchemaLocal, (string) $key);
      }
    }

    // throws the error for the main form
    if (count($errorSchema))
    {
      throw new sfValidatorErrorSchema($this, $errorSchema);
    }

    return $values;
  }
}