| src/Pluf/DB/Field.php |
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | foreach (array_keys($def) as $key) { |
| 100 | | if (!in_array($key, array('widget', 'label', 'required', |
| 100 | if (!in_array($key, array('widget', 'label', 'required', 'multiple', |
| 101 | 101 | 'initial', 'choices', 'widget_attrs'))) { |
| 102 | 102 | unset($def[$key]); |
| 103 | 103 | } |
| src/Pluf/DB/Field/Manytomany.php |
| 25 | 25 | { |
| 26 | 26 | public $type = 'manytomany'; |
| 27 | 27 | |
| 28 | | function formField($def, $form_field='Pluf_Form_Field_Varchar') |
| 28 | function formField($def, $form_field='Pluf_Form_Field_Integer') |
| 29 | 29 | { |
| 30 | 30 | $method = 'get_'.$def['name'].'_list'; |
| 31 | | $items = $def['model_instance']->$method(); |
| 32 | | $choices = array(); |
| 33 | | foreach ($items as $item) { |
| 34 | | $choices[(string) $item] = $item->id; |
| 31 | $def['multiple'] = true; |
| 32 | $def['initial'] = array(); |
| 33 | foreach ($def['model_instance']->$method() as $item) { |
| 34 | $def['initial'][(string) $item] = $item->id; |
| 35 | } |
| 36 | $def['choices'] = array(); |
| 37 | foreach (Pluf::factory($def['model'])->getList() as $item) { |
| 38 | $def['choices'][(string) $item] = $item->id; |
| 35 | 39 | } |
| 36 | | $def['choices'] = $choices; |
| 37 | 40 | if (!isset($def['widget'])) { |
| 38 | 41 | $def['widget'] = 'Pluf_Form_Widget_SelectMultipleInput'; |
| 39 | 42 | } |
| src/Pluf/Form/Field.php |
| 51 | 51 | */ |
| 52 | 52 | public $hidden_widget = 'Pluf_Form_Widget_HiddenInput'; |
| 53 | 53 | public $value = ''; /**< Current value of the field. */ |
| 54 | /** |
| 55 | * Returning multiple values (select multiple etc.) |
| 56 | */ |
| 57 | public $multiple = false; |
| 54 | 58 | protected $empty_values = array('', null, array()); |
| 55 | 59 | |
| 56 | 60 | /** |
| ... | ... | |
| 111 | 115 | */ |
| 112 | 116 | function clean($value) |
| 113 | 117 | { |
| 114 | | if ($this->required and in_array($value, $this->empty_values)) { |
| 118 | if (!$this->multiple and $this->required |
| 119 | and in_array($value, $this->empty_values)) { |
| 120 | throw new Pluf_Form_Invalid(__('This field is required.')); |
| 121 | } |
| 122 | if ($this->multiple and $this->required and empty($value)) { |
| 115 | 123 | throw new Pluf_Form_Invalid(__('This field is required.')); |
| 116 | 124 | } |
| 117 | 125 | return $value; |
| 118 | 126 | } |
| 119 | 127 | |
| 120 | 128 | /** |
| 129 | * Set the default empty value for a field. |
| 130 | * |
| 131 | * @param mixed Value |
| 132 | * @return mixed Value |
| 133 | */ |
| 134 | function setDefaultEmpty($value) |
| 135 | { |
| 136 | if (in_array($value, $this->empty_values) and !$this->multiple) { |
| 137 | $value = ''; |
| 138 | } |
| 139 | if (in_array($value, $this->empty_values) and $this->multiple) { |
| 140 | $value = array(); |
| 141 | } |
| 142 | return $value; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Multi-clean a value. |
| 147 | * |
| 148 | * If you are getting multiple values, you need to go through all |
| 149 | * of them and validate them against the requirements. This will |
| 150 | * do that for you. Basically, it is cloning the field, marking it |
| 151 | * as not multiple and validate each value. It will throw an |
| 152 | * exception in case of failure. |
| 153 | * |
| 154 | * If you are implementing your own field which could be filled by |
| 155 | * a "multiple" widget, you need to perform a check on |
| 156 | * $this->multiple. |
| 157 | * |
| 158 | * @see Pluf_Form_Field_Integer::clean |
| 159 | * |
| 160 | * @param array Values |
| 161 | * @return array Values |
| 162 | */ |
| 163 | public function multiClean($value) |
| 164 | { |
| 165 | $field = clone($this); |
| 166 | $field->multiple = false; |
| 167 | reset($value); |
| 168 | while (list($i, $val) = each($value)) { |
| 169 | $value[$i] = $field->clean($val); |
| 170 | } |
| 171 | reset($value); |
| 172 | return $value; |
| 173 | } |
| 174 | |
| 175 | /** |
| 121 | 176 | * Returns the HTML attributes to add to the field. |
| 122 | 177 | * |
| 123 | 178 | * @param object Widget |
| src/Pluf/Form/Field/Integer.php |
| 30 | 30 | public function clean($value) |
| 31 | 31 | { |
| 32 | 32 | parent::clean($value); |
| 33 | | if (in_array($value, $this->empty_values)) { |
| 34 | | $value = ''; |
| 35 | | } |
| 36 | | if (is_array($value)) { |
| 37 | | reset($value); |
| 38 | | while (list($i, $val) = each($value)) { |
| 39 | | if (!preg_match('/[0-9]+/', $val)) { |
| 40 | | throw new Pluf_Form_Invalid(__('The value must be an integer.')); |
| 41 | | } |
| 42 | | $this->checkMinMax($val); |
| 43 | | $value[$i] = (int) $val; |
| 44 | | } |
| 45 | | reset($value); |
| 46 | | return $value; |
| 33 | $value = $this->setDefaultEmpty($value); |
| 34 | if ($this->multiple) { |
| 35 | return $this->multiClean($value); |
| 47 | 36 | } else { |
| 48 | | if (!preg_match('/[0-9]+/', $value)) { |
| 37 | if (!preg_match('/^[\+\-]?[0-9]+$/', $value)) { |
| 49 | 38 | throw new Pluf_Form_Invalid(__('The value must be an integer.')); |
| 50 | 39 | } |
| 51 | 40 | $this->checkMinMax($value); |
| src/Pluf/Form/Model.php |
| 47 | 47 | } |
| 48 | 48 | foreach ($cols as $name=>$def) { |
| 49 | 49 | $db_field = new $def['type']('', $name); |
| 50 | | $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true); |
| 51 | | $def = array_merge($defaults, $def); |
| 50 | $def = array_merge(array('blank' => true, |
| 51 | 'verbose' => $name, |
| 52 | 'help_text' => '', |
| 53 | 'editable' => true), |
| 54 | $def); |
| 52 | 55 | if ($def['editable']) { |
| 53 | 56 | // The 'model_instance' and 'name' are used by the |
| 54 | 57 | // ManyToMany field. |
| src/Pluf/Model.php |
| 196 | 196 | /** |
| 197 | 197 | * Get the raw data of the object. |
| 198 | 198 | * |
| 199 | * For the many to many relations, the value is an array of ids. |
| 200 | * |
| 199 | 201 | * @return array Associative array of the data. |
| 200 | 202 | */ |
| 201 | 203 | function getData() |
| 202 | 204 | { |
| 205 | foreach ($this->_a['cols'] as $col=>$val) { |
| 206 | $field = new $val['type'](); |
| 207 | if ($field->type == 'manytomany') { |
| 208 | $this->_data[$col] = array(); |
| 209 | $method = 'get_'.strtolower($col).'_list'; |
| 210 | foreach ($this->$method() as $item) { |
| 211 | $this->_data[$col][] = $item->id; |
| 212 | } |
| 213 | } |
| 214 | } |
| 203 | 215 | return $this->_data; |
| 204 | 216 | } |
| 205 | 217 | |