function isValid()
{
+ if (!$this->is_bound)
+ return false;
+
if ($this->is_valid !== null)
return $this->is_valid;
$this->errors = array();
$form_methods = get_class_methods($this);
- foreach ($this->fields as $name=>$field) {
- $value = $field->widget->valueFromFormData($this->addPrefix($name), $this->data);
+ foreach ($this->fields as $name => $field) {
+ $value = $this->field($name)->value();
try {
$value = $field->clean($value);
$this->cleaned_data[$name] = $value;
public function current()
{
- $field = current($this->fields);
$name = key($this->fields);
- return new IPF_Form_BoundField($this, $field, $name);
+ return $this->field($name);
}
public function key()
public function value()
{
if ($this->form->is_bound)
- return $this->field->widget->valueToFormData($this->html_name, $this->form->data);
+ return $this->field->widget->valueFromFormData($this->html_name, $this->form->data);
else
return $this->form->initial($this->name);
}
return array();
}
- protected function buildAttrs($attrs, $extra_attrs=array())
+ public function valueFromFormData($name, $data)
{
- return array_merge($this->attrs, $attrs, $extra_attrs);
- }
-
- public function valueFromFormData($name, &$data)
- {
- if (isset($data[$name])) {
- return $data[$name];
- }
- return null;
- }
-
- public function valueToFormData($name, $data)
- {
- if (isset($data[$name])) {
- return $data[$name];
- }
- return null;
+ return isset($data[$name]) ? $data[$name] : null;
}
public function idForLabel($id)
return parent::render($name, '', $extra_attrs);
}
- public function valueFromFormData($name, &$data)
+ public function valueFromFormData($name, $data)
{
return isset($data[$name]) && false !== $data[$name] && (string)$data[$name] !== '0' && (string)$data[$name] !== 'off';
}
public function render($name, $value, $extra_attrs=array())
{
+ if ($value)
+ $value = IPF_Format::formatDate($this->format, strtotime($value));
+
$extra_attrs['class'] = 'dateinput';
$extra_attrs['data-format'] = IPF_Format::makeDateFormat($this->format, IPF_Format::$pickerControls);
return parent::render($name, $value, $extra_attrs);
}
- public function valueFromFormData($name, &$data)
+ public function valueFromFormData($name, $data)
{
if (!isset($data[$name]) || !$data[$name]) {
return null;
return null;
}
}
-
- public function valueToFormData($name, $data)
- {
- if (isset($data[$name]) && $data[$name]) {
- return IPF_Format::formatDate($this->format, strtotime($data[$name]));
- } else {
- return null;
- }
- }
}
public function render($name, $value, $extra_attrs=array())
{
+ if ($value)
+ $value = IPF_Format::formatDate($this->format, strtotime($value.' GMT'));
+
$extra_attrs['class'] = 'datetimeinput';
$extra_attrs['data-dateformat'] = IPF_Format::makeDateFormat($this->format, IPF_Format::$pickerControls);
$extra_attrs['data-timeformat'] = IPF_Format::makeTimeFormat($this->format, IPF_Format::$pickerControls);
return parent::render($name, $value, $extra_attrs);
}
- public function valueFromFormData($name, &$data)
+ public function valueFromFormData($name, $data)
{
if (!isset($data[$name]) || !$data[$name]) {
return null;
return null;
}
}
-
- public function valueToFormData($name, $data)
- {
- if (isset($data[$name]) && $data[$name]) {
- // Internally we use GMT, so we convert back to the current timezone.
- return IPF_Format::formatDate($this->format, strtotime($data[$name].' GMT'));
- } else {
- return null;
- }
- }
}
return $sim . parent::render($name, '', $extra_attrs);
}
- public function valueFromFormData($name, &$data)
+ public function valueFromFormData($name, $data)
{
if (!isset($data[$name]))
return null;
return $res;
}
-
- public function valueToFormData($name, $data)
- {
- if (!isset($data[$name]))
- return null;
- $remove = isset($data[$name.'_remove']) && $data[$name.'_remove'] == 1;
- return array('data'=>$data[$name], 'remove'=>$remove);
- }
}
class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget
{
- public $mode = 'textareas';
- public $theme = 'simple';
- public $include_tinymce = true;
- public $force_absolute_urls = false;
+ public $tinymce_url;
+ public $mode;
+ public $theme;
+ public $include_tinymce;
+ public $force_absolute_urls;
+ public $editor_config;
public function __construct($attrs=array())
{
- $defaults = array('cols' => '70',
- 'rows' => '20');
- $config = array('tinymce_url', 'mode', 'theme', 'include_tinymce', 'force_absolute_urls');
- foreach ($config as $cfg) {
- if (isset($attrs[$cfg])) {
- $this->$cfg = $attrs[$cfg];
- unset($attrs[$cfg]);
- }
- }
- $this->attrs = array_merge($defaults, $attrs);
+ $this->tinymce_url = \PFF\Arr::pop($attrs, 'tinymce_url');
+ $this->mode = \PFF\Arr::pop($attrs, 'mode', 'textareas');
+ $this->theme = \PFF\Arr::pop($attrs, 'theme', 'simple');
+ $this->include_tinymce = \PFF\Arr::pop($attrs, 'include_tinymce', true);
+ $this->force_absolute_urls = \PFF\Arr::pop($attrs, 'force_absolute_urls', false);
+ $this->editor_config = \PFF\Arr::pop($attrs, 'editor_config', array());
+
+ parent::__construct($attrs);
}
public function render($name, $value, $extra_attrs=array())
{
if ($value === null) $value = '';
+
$extra_config = '';
- if (isset($this->attrs['editor_config'])) {
- $_ec = $this->attrs['editor_config'];
- unset($this->attrs['editor_config']);
- $_st = array();
- foreach ($_ec as $key=>$val) {
- if (is_bool($val)) {
- if ($val) {
- $_st[] = $key.' : true';
- } else {
- $_st[] = $key.' : false';
- }
- } else {
- $_st[] = $key.' : "'.$val.'"';
- }
- }
- if ($_st) {
- $extra_config = ",\n".implode(",\n", $_st);
+ foreach ($this->editor_config as $key => $val) {
+ $extra_config .= ",\n{$key} : ";
+ if (is_bool($val)) {
+ $extra_config .= $val ? 'true' : 'false';
+ } else {
+ $extra_config .= '"'.$val.'"';
}
}
- $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
return Tag::textarea($final_attrs)
+ ->attr('cols', 70)
+ ->attr('rows', 20)
+ ->attrs($this->attrs)
+ ->attrs($extra_attrs)
+ ->attr('name', $name)
->addClass($this->force_absolute_urls ? 'htmlEditorAbs' : 'htmlEditor')
->append($value)
->html();
{
public function render($name, $value, $extra_attrs=array())
{
- $final_attrs = $this->buildAttrs(array('name' => $name,
- 'type' => $this->input_type),
- $extra_attrs);
+ $tag = Tag::input()
+ ->attrs($this->attrs)
+ ->attrs($extra_attrs)
+ ->attr('name', $name)
+ ->attr('type', $this->input_type);
+
if ($value)
- $final_attrs['value'] = $value;
+ $tag->attr('value', $value);
- return Tag::input($final_attrs);
+ return $tag->html();
}
}
parent::__construct($attrs);
}
- public function render($name, $value, $extra_attrs=array(), $choices=array())
+ public function render($name, $value, $extra_attrs=array())
{
- $output = array();
- if ($value === null) {
+ if ($value === null)
$value = '';
- }
- $output = Tag::ul(); // ->attrs($this->buildAttrs($extra_attrs));
- $choices = $this->choices + $choices;
- $index = 1;
- foreach ($choices as $option_label => $option_value) {
- $optionId = "id_$name-$index";
- $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'id' => $optionId, 'value' => $option_value));
+ $output = Tag::ul()
+ ->attrs($this->attrs)
+ ->attrs($extra_attrs);
+ foreach ($this->choices as $option_label => $option_value) {
+ $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'value' => $option_value));
if ($option_value == $value)
$radio->attr('checked', 'checked');
$output->append(Tag::li(null,
- $radio,
- Tag::label(array('for' => $optionId), $option_label)));
-
- $index++;
+ Tag::label(null, $radio, ' ', $option_label)));
}
return $output->html();
}
+
+ public function idForLabel($id)
+ {
+ return null;
+ }
}
return $select->html();
}
- public function valueFromFormData($name, &$data)
+ public function valueFromFormData($name, $data)
{
if (isset($data[$name]) and is_array($data[$name])) {
return $data[$name];
class IPF_Form_Widget_SelectMultipleInputCheckbox extends IPF_Form_Widget_SelectMultipleInput
{
- public function render($name, $value, $extra_attrs=array(), $choices=array())
+ public function render($name, $value, $extra_attrs=array())
{
if ($value === null || $value == '')
$value = array();
- $final_attrs = $this->buildAttrs($extra_attrs);
- $output = Tag::ul();
- $choices = array_merge($this->choices, $choices);
- $i=0;
- $base_id = $final_attrs['id'];
- foreach ($choices as $option_label => $option_value) {
+
+ $output = Tag::ul()
+ ->attrs($this->attrs)
+ ->attrs($extra_attrs);
+ foreach ($this->choices as $option_label => $option_value) {
$selected = in_array($option_value, $value);
$checkbox = Tag::input($final_attrs)
->attr('type', 'checkbox')
->attr('name', "{$name}[]")
- ->attr('id', $base_id.'_'.$i)
->attr('value', $option_value);
+
if ($selected)
$checkbox->attr('checked', 'checked');
$output->append(Tag::li(null,
Tag::label(null, $checkbox, ' ', $option_label)));
- $i++;
}
return $output->html();
}
public function idForLabel($id)
{
- if ($id) {
- $id += '_0';
- }
- return $id;
+ return null;
}
}
class IPF_Form_Widget_TextareaInput extends IPF_Form_Widget
{
- public function __construct($attrs=array())
- {
- $this->attrs = array_merge(array('cols' => '40', 'rows' => '10'), $attrs);
- }
-
public function render($name, $value, $extra_attrs=array())
{
if ($value === null)
$value = '';
- $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
- return Tag::textarea($final_attrs, $value)->html();
+
+ return Tag::textarea()
+ ->attr('cols', 40)
+ ->attr('rows', 10)
+ ->attrs($this->attrs)
+ ->attrs($extra_attrs)
+ ->attr('name', $name)
+ ->append($value)
+ ->html();
}
}