/
sfValidatorEmbeddedForm.class.php
51 lines (44 loc) · 1.5 KB
/
sfValidatorEmbeddedForm.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* sfValidatorEmbeddedForm saves form results into a single array
*
* @author Brent Shaffer <bshafs at gmail dot com>
*/
class sfValidatorEmbeddedForm extends sfValidatorBase
{
/**
* options:
* * form: Required sfForm instance to validate
* * archiver: provide an archiver to sanitize results into a specific format (XML, YAML, etc). Default is array.
* * throw_form_errors: whether or not to include validation errors in output
*
* messages:
* * invalid: error message to throw if validation fails
*
* @param string $options
* @param string $messages
* @return null
* @author Brent Shaffer
*/
protected function configure($options = array(), $messages = array())
{
$this->addRequiredOption('form');
$this->addOption('throw_form_errors', true);
$this->addOption('archiver');
$this->addMessage('invalid', 'Your form contains some errors');
parent::configure($options, $messages);
}
protected function doClean($value)
{
$form = $this->getOption('form');
$form->bind($value);
if ($form->isValid()) {
if($archiverClass = $this->getOption('archiver')) {
$archiver = new $archiverClass();
return $archiver->sleep($form->getValues());
}
return $form->getValues();
}
throw $this->getOption('throw_form_errors') ? $form->getErrorSchema() : new sfValidatorErrorSchema($this);
}
}