true, * 'widget'=>'Pluf_Form_Widget_TextInput', * 'initial'=>'your name here', * 'label'=>__('Your name'), * 'help_text'=>__('You are?')); * * @param array Params of the field. */ function __construct($params=array()) { // We basically take the parameters, for each one we grab the // corresponding member variable and populate the $default // array with. Then we merge with the values given in the // parameters and update the member variables. // This allows to pass extra parameters likes 'min_size' // etc. and update the member variables accordingly. This is // practical when you extend this class with your own class. $default = array(); foreach ($params as $key=>$in) { if ($key !== 'widget_attrs') $default[$key] = $this->$key; // Here on purpose it // will fail if a // parameter not needed // for this field is // passed. } $m = array_merge($default, $params); foreach ($params as $key=>$in) { if ($key !== 'widget_attrs') $this->$key = $m[$key]; } // Set the widget to be an instance and not the string name. $widget_name = $this->widget; if (isset($params['widget_attrs'])) { $attrs = $params['widget_attrs']; } else { $attrs = array(); } $widget = new $widget_name($attrs); $attrs = $this->widgetAttrs($widget); if (count($attrs)) { $widget->attrs = array_merge($widget->attrs, $attrs); } $this->widget = $widget; } /** * Validate some possible input for the field. * * @param mixed Value to clean. * @return mixed Cleaned data or throw a Pluf_Form_Invalid exception. */ function clean($value) { if (!$this->multiple and $this->required and in_array($value, $this->empty_values)) { throw new Pluf_Form_Invalid(__('This field is required.')); } if ($this->multiple and $this->required and empty($value)) { throw new Pluf_Form_Invalid(__('This field is required.')); } return $value; } /** * Set the default empty value for a field. * * @param mixed Value * @return mixed Value */ function setDefaultEmpty($value) { if (in_array($value, $this->empty_values) and !$this->multiple) { $value = ''; } if (in_array($value, $this->empty_values) and $this->multiple) { $value = array(); } return $value; } /** * Multi-clean a value. * * If you are getting multiple values, you need to go through all * of them and validate them against the requirements. This will * do that for you. Basically, it is cloning the field, marking it * as not multiple and validate each value. It will throw an * exception in case of failure. * * If you are implementing your own field which could be filled by * a "multiple" widget, you need to perform a check on * $this->multiple. * * @see Pluf_Form_Field_Integer::clean * * @param array Values * @return array Values */ public function multiClean($value) { $field = clone($this); $field->multiple = false; reset($value); while (list($i, $val) = each($value)) { $value[$i] = $field->clean($val); } reset($value); return $value; } /** * Returns the HTML attributes to add to the field. * * @param object Widget * @return array HTML attributes. */ public function widgetAttrs($widget) { return array(); } }