/
sfWidgetFormEmbeddedForm.class.php
57 lines (48 loc) · 1.79 KB
/
sfWidgetFormEmbeddedForm.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
52
53
54
55
56
57
<?php
/**
* sfWidgetFormEmbeddedForm represents an embedded form
*
* @author Brent Shaffer <bshafs at gmail dot com>
*/
class sfWidgetFormEmbeddedForm extends sfWidgetForm
{
/**
* options:
* * form: Required sfForm instance rendered by this widget
* * archiver: provide an archiver to sanitize results into a specific format (XML, YAML, etc). Default is array.
* * global_attributes: if specified, attributes are passed as if to a widget. Apply to ALL widgets
*
* @param string $options
* @param string $attributes
* @return null
* @author Brent Shaffer
*/
public function configure($options = array(), $attributes = array())
{
$this->addRequiredOption('form');
$this->addOption('archiver');
$this->addOption('global_attributes', true);
return parent::configure($options, $attributes);
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$form = $this->getOption('form');
$form->getWidgetSchema()->setNameFormat($name.'[%s]');
if($archiverClass = $this->getOption('archiver')) {
$archiver = new $archiverClass();
$value = $archiver->isAsleep($value) ? $archiver->wake($value) : $value;
}
$form->setDefaults($value);
if ($this->getOption('global_attributes')) {
foreach ($form->getWidgetSchema()->getFields() as $name => $widget) {
$widget->setAttributes($attributes);
}
}
$html = $form->render($attributes);
// Decorate the form
if ($format = $form->getWidgetSchema()->getFormFormatter()->getDecoratorFormat()) {
return strtr($format, array('%content%' => $html));
}
return $html;
}
}