<?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;
}
}