pluf2

pluf2 Commit Details


Date:2009-05-13 03:09:21 (15 years 7 months ago)
Author:Loic d'Anterroches
Branch:master
Commit:af00cbc50a919d68ceb6ef390a10e1517caf529e
Parents: 8a7e834931d6c8660dc74120730247856bae5b10
Message:Fixed issue 49, orm generation from model does not fill automatically the selection list for a mant-to-many relation.

Changes:

File differences

src/Pluf/DB/Field.php
9797
9898
9999
100
100
101101
102102
103103
}
}
foreach (array_keys($def) as $key) {
if (!in_array($key, array('widget', 'label', 'required',
if (!in_array($key, array('widget', 'label', 'required', 'multiple',
'initial', 'choices', 'widget_attrs'))) {
unset($def[$key]);
}
src/Pluf/DB/Field/Manytomany.php
2525
2626
2727
28
28
2929
3030
31
32
33
34
31
32
33
34
35
36
37
38
3539
36
3740
3841
3942
{
public $type = 'manytomany';
function formField($def, $form_field='Pluf_Form_Field_Varchar')
function formField($def, $form_field='Pluf_Form_Field_Integer')
{
$method = 'get_'.$def['name'].'_list';
$items = $def['model_instance']->$method();
$choices = array();
foreach ($items as $item) {
$choices[(string) $item] = $item->id;
$def['multiple'] = true;
$def['initial'] = array();
foreach ($def['model_instance']->$method() as $item) {
$def['initial'][(string) $item] = $item->id;
}
$def['choices'] = array();
foreach (Pluf::factory($def['model'])->getList() as $item) {
$def['choices'][(string) $item] = $item->id;
}
$def['choices'] = $choices;
if (!isset($def['widget'])) {
$def['widget'] = 'Pluf_Form_Widget_SelectMultipleInput';
}
src/Pluf/Form/Field.php
5151
5252
5353
54
55
56
57
5458
5559
5660
......
111115
112116
113117
114
118
119
120
121
122
115123
116124
117125
118126
119127
120128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
121176
122177
123178
*/
public $hidden_widget = 'Pluf_Form_Widget_HiddenInput';
public $value = ''; /**< Current value of the field. */
/**
* Returning multiple values (select multiple etc.)
*/
public $multiple = false;
protected $empty_values = array('', null, array());
/**
*/
function clean($value)
{
if ($this->required and in_array($value, $this->empty_values)) {
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
src/Pluf/Form/Field/Integer.php
3030
3131
3232
33
34
35
36
37
38
39
40
41
42
43
44
45
46
33
34
35
4736
48
37
4938
5039
5140
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
$value = '';
}
if (is_array($value)) {
reset($value);
while (list($i, $val) = each($value)) {
if (!preg_match('/[0-9]+/', $val)) {
throw new Pluf_Form_Invalid(__('The value must be an integer.'));
}
$this->checkMinMax($val);
$value[$i] = (int) $val;
}
reset($value);
return $value;
$value = $this->setDefaultEmpty($value);
if ($this->multiple) {
return $this->multiClean($value);
} else {
if (!preg_match('/[0-9]+/', $value)) {
if (!preg_match('/^[\+\-]?[0-9]+$/', $value)) {
throw new Pluf_Form_Invalid(__('The value must be an integer.'));
}
$this->checkMinMax($value);
src/Pluf/Form/Model.php
4747
4848
4949
50
51
50
51
52
53
54
5255
5356
5457
}
foreach ($cols as $name=>$def) {
$db_field = new $def['type']('', $name);
$defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true);
$def = array_merge($defaults, $def);
$def = array_merge(array('blank' => true,
'verbose' => $name,
'help_text' => '',
'editable' => true),
$def);
if ($def['editable']) {
// The 'model_instance' and 'name' are used by the
// ManyToMany field.
src/Pluf/Model.php
196196
197197
198198
199
200
199201
200202
201203
202204
205
206
207
208
209
210
211
212
213
214
203215
204216
205217
/**
* Get the raw data of the object.
*
* For the many to many relations, the value is an array of ids.
*
* @return array Associative array of the data.
*/
function getData()
{
foreach ($this->_a['cols'] as $col=>$val) {
$field = new $val['type']();
if ($field->type == 'manytomany') {
$this->_data[$col] = array();
$method = 'get_'.strtolower($col).'_list';
foreach ($this->$method() as $item) {
$this->_data[$col][] = $item->id;
}
}
}
return $this->_data;
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.07361s using 14 queries.