]> git.andy128k.dev Git - ipf.git/commitdiff
db form fields
authoravl <alex.litovchenko@gmail.com>
Wed, 20 Aug 2008 12:42:31 +0000 (15:42 +0300)
committeravl <alex.litovchenko@gmail.com>
Wed, 20 Aug 2008 12:42:31 +0000 (15:42 +0300)
ipf/form/db/decimal.php [new file with mode: 0644]
ipf/form/db/foreignkey.php [new file with mode: 0644]
ipf/form/field/choice.php [new file with mode: 0644]
ipf/form/field/modelchoice.php [new file with mode: 0644]

diff --git a/ipf/form/db/decimal.php b/ipf/form/db/decimal.php
new file mode 100644 (file)
index 0000000..45d6f3c
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+class IPF_Form_DB_Decimal extends IPF_Form_DB{
+    function formField($def, $form_field='IPF_Form_Field_Float'){
+        $def['widget_attrs'] = array('style'=>'width:40px;');
+        return parent::formField($def, $form_field);
+    }
+}
\ No newline at end of file
diff --git a/ipf/form/db/foreignkey.php b/ipf/form/db/foreignkey.php
new file mode 100644 (file)
index 0000000..6c10b3b
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+class IPF_Form_DB_Foreignkey extends IPF_Form_DB
+{
+    function formField($def, $form_field='IPF_Form_Field_ModelChoice'){
+        $gmodel = new $def['model']();
+        $def['queryset'] = $gmodel->getTable()->findAll();
+        $def['model'] = $gmodel;
+        $def['required'] = true;
+        return parent::formField($def, $form_field);
+    }
+}
diff --git a/ipf/form/field/choice.php b/ipf/form/field/choice.php
new file mode 100644 (file)
index 0000000..a96babe
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+class IPF_Form_Field_Choice extends IPF_Form_Field{
+    public $widget = 'IPF_Form_Widget_SelectInput';
+    protected $_choices = array();
+
+    function __construct($params=array())
+    {
+        parent::__construct($params);
+        if (isset($params['choices']))
+            $this->setChoices($params['choices']);
+    }
+
+    public function clean($value){
+        parent::clean($value);
+        if (in_array($value, $this->empty_values)) {
+            return '';
+        }
+        if (!$this->validValue($value))
+            throw new IPF_Exception_Form(__('Invalid choice'));
+        return $value;
+    }
+    
+    public function setChoices($choices){
+        $this->_choices = $choices;
+        $this->widget->choices = $choices;
+    }
+    
+    public function validValue($value){
+        foreach($this->_choices as $name=>$val)
+            if ($value==$val)
+                return true;
+        return false;
+    }
+}
+
diff --git a/ipf/form/field/modelchoice.php b/ipf/form/field/modelchoice.php
new file mode 100644 (file)
index 0000000..1ce1ce5
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class IPF_Form_Field_ModelChoice extends IPF_Form_Field_Choice{
+
+    protected $_model;
+
+    function __construct($params=array()){
+        parent::__construct($params);
+        $this->model = $params['model'];
+        if (isset($params['queryset'])){
+            $choices = array('--------'=>'');
+            foreach ($params['queryset'] as $item) {
+                $choices[(string)$item] = $item->id;
+            }
+            $this->setChoices($choices);
+        }
+    }
+    
+    public function clean($value){
+        parent::clean($value);
+        if (in_array($value, $this->empty_values)) {
+            return null;
+        }
+        //print_r($this->model);
+        //print $value;
+        //$this->model->get($value);
+        $o = $this->model->getTable()->find($value);
+        return $o;
+    }
+}