function formField($def, $form_field='IPF_Form_Field_Varchar')
{
- $defaults = array('required' => !$def['blank'],
- 'label' => IPF_Utils::humanTitle($def['verbose']),
- 'help_text' => $def['help_text']);
+ $defaults = array(
+ 'required' => !$def['blank'],
+ 'label' => IPF_Utils::humanTitle($def['verbose']),
+ 'help_text' => $def['help_text'],
+ 'type'=>$this->type,
+ );
+
unset($def['blank'], $def['verbose'], $def['help_text']);
if (isset($def['default'])) {
$defaults['initial'] = $def['default'];
unset($def['default']);
}
+ /*
if (isset($def['choices'])) {
$defaults['widget'] = 'IPF_Form_Widget_SelectInput';
if (isset($def['widget_attrs'])) {
unset($def[$key]);
}
}
+ */
$params = array_merge($defaults, $def);
return new $form_field($params);
}
<?php
class IPF_Form_DB_Integer extends IPF_Form_DB{
-
- public $widget = 'IPF_Form_Widget_TextInput';
- public $max = null;
- public $min = null;
-
- 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 IPF_Exception_Form(__('The value must be an integer.'));
- }
- $this->checkMinMax($val);
- $value[$i] = (int) $val;
- }
- reset($value);
- return $value;
- } else {
- if (!preg_match('/[0-9]+/', $value)) {
- throw new IPF_Exception_Form(__('The value must be an integer.'));
- }
- $this->checkMinMax($value);
- }
- return (int) $value;
- }
-
- protected function checkMinMax($value)
- {
- if ($this->max !== null and $value > $this->max) {
- throw new IPF_Exception_Form(sprintf(__('Ensure that this value is not greater than %1$d.'), $this->max));
- }
- if ($this->min !== null and $value < $this->min) {
- throw new IPF_Exception_Form(sprintf(__('Ensure that this value is not lower than %1$d.'), $this->min));
- }
+ function formField($def, $form_field='IPF_Form_Field_Integer'){
+ $def['widget_attrs'] = array('style'=>'width:40px;');
+ return parent::formField($def, $form_field);
}
}
\ No newline at end of file
public function clean($value)
{
parent::clean($value);
+
+ if (($this->required==false) and in_array($value, $this->empty_values)) {
+ return null;
+ }
+
if (in_array($value, $this->empty_values)) {
$value = '';
}
$user_fields = $this->fields();
$db_columns = $this->model->getTable()->getColumns();
+ $db_relations = $this->model->getTable()->getRelations();
if ($user_fields===null){
foreach($db_columns as $name=>$col){
$this->addDBField($name,$col);
}
+ foreach($db_relations as $name => $relation){
+ $this->addDBRelation($name,$relation);
+ }
}
else{
foreach($user_fields as $uname){
if (array_key_exists($uname,$db_columns))
$this->addDBField($uname,$db_columns[$uname]);
+ elseif (array_key_exists($uname,$db_relations))
+ $this->addDBRelation($uname,$db_relations[$uname]);
else{
$add_method = 'add__'.$uname.'__field';
$this->$add_method();
$defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true);
$type = $col['type'];
+
if (isset($col['notblank']))
- $defaults['blank'] = false;
+ if ($col['notblank'])
+ $defaults['blank'] = false;
+ else
+ $defaults['blank'] = true;
if (isset($col['length']))
$defaults['max_length'] = (int)($col['length']);
if (isset($col['email']))
$type = 'email';
$cn = 'IPF_Form_DB_'.$type;
- $db_field = new $cn('', $name);
+ $db_field = new $cn('', $name);
//echo $name;
//print_r($defaults);
$this->fields[$name] = $form_field;
}
}
-
+
+ function addDBRelation($name,$relation){
+ if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE){
+ //$name .= "_id";
+ $db_field = new IPF_Form_DB_Foreignkey('',$name);
+ $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
+ $form_field = $db_field->formField($defaults);
+ $this->fields[$name] = $form_field;
+ }
+ }
+
function fields(){ return $this->user_fields; }
function save($commit=true)
{
if ($this->isValid()) {
+
+ //print_r($this->cleaned_data);
+
+ //print ($this->cleaned_data['category']);
+
$this->model->SetFromFormData($this->cleaned_data);
//print_r($this->model->data);
-
/*
if ($commit && $this->model->id) {
$this->model->update();
$this->model->create();
}
*/
- print_r($this->model->save());
+
+ $this->model->save();
return $this->model;
}
throw new Exception(__('Cannot save the model from an invalid form.'));
{
$sql = $this->getForeignKeyBaseDeclaration($definition);
$sql .= $this->getAdvancedForeignKeyOptions($definition);
-
return $sql;
}
function SetFromFormData($cleaned_values)
{
+ //$relations = $this->getTable()->getRelations();
foreach ($cleaned_values as $key=>$val) {
- $this->$key = $val;
+ /*
+ if (array_key_exists($key,$relations)){
+
+
+ }else*/
+ $this->$key = $val;
}
}
}
\ No newline at end of file
}
$options['primary'] = $primary;
-
+
return array('tableName' => $this->getOption('tableName'),
'columns' => $columns,
'options' => array_merge($this->getOptions(), $options));