Permalink
Browse files

Separate field classes into individual files

TODO: test the case compatibility
  • Loading branch information...
1 parent 1fd79ea commit 075d93f26d37bd9169898c3c045c67adce2994a5 @romaninsh romaninsh committed Jan 10, 2013
View
@@ -42,24 +42,24 @@ class Controller_MVCForm extends AbstractController {
public $field_associations=array();
public $type_associations=array(
- 'string'=>'line',
- 'text'=>'text',
- 'int'=>'line',
- 'numeric'=>'line',
- 'money'=>'line',
- 'real'=>'line',
+ 'string'=>'Line',
+ 'text'=>'Text',
+ 'int'=>'Line',
+ 'numeric'=>'Line',
+ 'money'=>'Line',
+ 'real'=>'Line',
'date'=>'DatePicker',
'datetime'=>'DatePicker',
'daytime'=>'timepickr',
- 'boolean'=>'checkbox',
- 'reference'=>'readonly',
- 'reference_id'=>'dropdown',
- 'password'=>'password',
- 'list'=>'dropdown',
+ 'boolean'=>'Checkbox',
+ 'reference'=>'Readonly',
+ 'reference_id'=>'Dropdown',
+ 'password'=>'Password',
+ 'list'=>'Dropdown',
'radio'=>'Radio',
- 'readonly'=>'readonly',
- 'image'=>'image',
- 'file'=>'upload',
+ 'readonly'=>'Readonly',
+ 'image'=>'Image',
+ 'file'=>'Upload',
);
function setActualFields($fields){
$this->importFields($this->owner->model,$fields);
View
@@ -16,7 +16,6 @@
See LICENSE or LICENSE_COM for more information
=====================================================ATK4=*/
// Field bundle
-if(!class_exists('Form_Field',false))include_once'Form/Field.php';
/**
* This class implements generic form, which you can actually use without
* redeclaring it. Just add fields, buttons and use execute method.
View
@@ -412,173 +412,6 @@ function addField(){
///////// Because many fields are really simple extenions of the base-line field, they are
///////// defined here.
-class Form_Field_Line extends Form_Field {
- function getInput($attr=array()){
- return parent::getInput(array_merge(array('type'=>'text'),$attr));
- }
-}
// Visually different fields
-class Form_Field_Search extends Form_Field {
- // WARNING: <input type=search> is safari extention and is will not validate as valid HTML
- function getInput($attr=array()){
- return parent::getInput(array_merge(array('type'=>'search'),$attr));
- }
-}
-class Form_Field_Checkbox extends Form_Field {
- public $true_value=1;
- public $false_value=0;
- function init(){
- parent::init();
- $this->default_value='';
- }
- function getInput($attr=array()){
- $this->template->trySet('field_caption','');
- $this->template->tryDel('label_container');
- if(strpos('<',$this->caption)!==false){
- // HTML in label
- $label=$this->caption;
- }else{
- $label='<label for="'.$this->name.'">'.$this->caption.'</label>';
- }
- return parent::getInput(array_merge(
- array(
- 'type'=>'checkbox',
- 'value'=>$this->true_value,
- 'checked'=>(boolean)($this->true_value==$this->value)
- ),$attr
- )).$label;
- }
- function loadPOST(){
- if(isset($_POST[$this->name])){
- $this->set($this->true_value);
- }else{
- $this->set($this->false_value);
- }
- }
-}
-class Form_Field_Password extends Form_Field {
- function normalize(){
- // user may have entered spaces accidentally in the password field.
- // Clean them up.
- $this->set(trim($this->get()));
- parent::normalize();
- }
- function getInput($attr=array()){
- return parent::getInput(array_merge(
- array(
- 'type'=>'password',
- ),$attr
- ));
- }
-}
-class Form_Field_Hidden extends Form_Field {
- function getInput($attr=array()){
- return parent::getInput(array_merge(
- array(
- 'type'=>'hidden',
- ),$attr
- ));
- }
- function render(){
- if($this->owner == $this->form){
- $this->form->template_chunks['form']->appendHTML('Content',$this->getInput());
- }else $this->output($this->getInput());
- }
-}
-class Form_Field_Readonly extends Form_Field {
- function init(){
- parent::init();
- $this->setNoSave();
- }
-
- function getInput($attr=array()){
- return nl2br(isset($this->value_list) ? $this->value_list[$this->value] : $this->value);
- }
- function loadPOST(){
- // do nothing, readonly field
- }
- function setValueList($list){
- $this->value_list = $list;
- return $this;
- }
-
-}
-class Form_Field_Time extends Form_Field {
- function getInput($attr=array()){
- return parent::getInput(array_merge(array('type'=>'text',
- 'value'=>format_time($this->value)),$attr));
- }
-}
-class Form_Field_Date extends Form_Field {
- private $sep = '-';
- private $is_valid = false;
-
- /*function getInput($attr=array()){
- return parent::getInput(array_merge(array('type'=>'text',
- 'value'=>($this->is_valid ? date('Y-m-d', $this->value) : $this->value)),$attr));
- }*/
- private function invalid(){
- return $this->displayFieldError('Not a valid date');
- }
- function validate(){
- //empty value is ok
- if($this->value==''){
- $this->is_valid=true;
- return parent::validate();
- }
- //checking if there are 2 separators
- if(substr_count($this->value, $this->sep) != 2){
- $this->invalid();
- }else{
- $c = explode($this->sep, $this->value);
- //day must go first, month should be second and a year should be last
- if(strlen($c[0]) != 4 ||
- $c[1] <= 0 || $c[1] > 12 ||
- $c[2] <= 0 || $c[2] > 31)
- {
- $this->invalid();
- }
- //now attemting to convert to date
- if(strtotime($this->value)==''){
- $this->invalid();
- }else{
- //$this->set(strtotime($this->value));
- $this->set($this->value);
- $this->is_valid=true;
- }
- }
- return parent::validate();
- }
-}
-class Form_Field_Text extends Form_Field {
- function init(){
- $this->attr=array('rows'=>5);
- parent::init();
- }
- function setFieldHint($text){
- return parent::setFieldHint($text);
- }
- function getInput($attr=array()){
-
- return
- parent::getInput(array_merge(array(''=>'textarea'),$attr)).
- htmlspecialchars($this->value,ENT_COMPAT,'ISO-8859-1',false).
- $this->getTag('/textarea');
- }
-}
-class Form_Field_Number extends Form_Field_Line {
- function normalize(){
- $v=$this->get();
-
- // get rid of TODO
-
- $this->set($v);
- }
-}
-class Form_Field_Money extends Form_Field_Line {
- function getInput($attr=array()){
- return parent::getInput(array_merge(array('value'=>number_format($this->value,2)),$attr));
- }
-}
@@ -0,0 +1,33 @@
+<?php
+class Form_Field_Checkbox extends Form_Field {
+ public $true_value=1;
+ public $false_value=0;
+ function init(){
+ parent::init();
+ $this->default_value='';
+ }
+ function getInput($attr=array()){
+ $this->template->trySet('field_caption','');
+ $this->template->tryDel('label_container');
+ if(strpos('<',$this->caption)!==false){
+ // HTML in label
+ $label=$this->caption;
+ }else{
+ $label='<label for="'.$this->name.'">'.$this->caption.'</label>';
+ }
+ return parent::getInput(array_merge(
+ array(
+ 'type'=>'checkbox',
+ 'value'=>$this->true_value,
+ 'checked'=>(boolean)($this->true_value==$this->value)
+ ),$attr
+ )).$label;
+ }
+ function loadPOST(){
+ if(isset($_POST[$this->name])){
+ $this->set($this->true_value);
+ }else{
+ $this->set($this->false_value);
+ }
+ }
+}
View
@@ -0,0 +1,42 @@
+<?php
+class Form_Field_Date extends Form_Field {
+ private $sep = '-';
+ private $is_valid = false;
+
+ /*function getInput($attr=array()){
+ return parent::getInput(array_merge(array('type'=>'text',
+ 'value'=>($this->is_valid ? date('Y-m-d', $this->value) : $this->value)),$attr));
+ }*/
+ private function invalid(){
+ return $this->displayFieldError('Not a valid date');
+ }
+ function validate(){
+ //empty value is ok
+ if($this->value==''){
+ $this->is_valid=true;
+ return parent::validate();
+ }
+ //checking if there are 2 separators
+ if(substr_count($this->value, $this->sep) != 2){
+ $this->invalid();
+ }else{
+ $c = explode($this->sep, $this->value);
+ //day must go first, month should be second and a year should be last
+ if(strlen($c[0]) != 4 ||
+ $c[1] <= 0 || $c[1] > 12 ||
+ $c[2] <= 0 || $c[2] > 31)
+ {
+ $this->invalid();
+ }
+ //now attemting to convert to date
+ if(strtotime($this->value)==''){
+ $this->invalid();
+ }else{
+ //$this->set(strtotime($this->value));
+ $this->set($this->value);
+ $this->is_valid=true;
+ }
+ }
+ return parent::validate();
+ }
+}
View
@@ -0,0 +1,15 @@
+<?php
+class Form_Field_Hidden extends Form_Field {
+ function getInput($attr=array()){
+ return parent::getInput(array_merge(
+ array(
+ 'type'=>'hidden',
+ ),$attr
+ ));
+ }
+ function render(){
+ if($this->owner == $this->form){
+ $this->form->template_chunks['form']->appendHTML('Content',$this->getInput());
+ }else $this->output($this->getInput());
+ }
+}
View
@@ -0,0 +1,6 @@
+<?php
+class Form_Field_Line extends Form_Field {
+ function getInput($attr=array()){
+ return parent::getInput(array_merge(array('type'=>'text'),$attr));
+ }
+}
View
@@ -0,0 +1,6 @@
+<?php
+class Form_Field_Money extends Form_Field_Line {
+ function getInput($attr=array()){
+ return parent::getInput(array_merge(array('value'=>number_format($this->value,2)),$attr));
+ }
+}
View
@@ -0,0 +1,10 @@
+<?php
+class Form_Field_Number extends Form_Field_Line {
+ function normalize(){
+ $v=$this->get();
+
+ // get rid of TODO
+
+ $this->set($v);
+ }
+}
@@ -0,0 +1,16 @@
+<?php
+class Form_Field_Password extends Form_Field {
+ function normalize(){
+ // user may have entered spaces accidentally in the password field.
+ // Clean them up.
+ $this->set(trim($this->get()));
+ parent::normalize();
+ }
+ function getInput($attr=array()){
+ return parent::getInput(array_merge(
+ array(
+ 'type'=>'password',
+ ),$attr
+ ));
+ }
+}
@@ -0,0 +1,19 @@
+<?php
+class Form_Field_Readonly extends Form_Field {
+ function init(){
+ parent::init();
+ $this->setNoSave();
+ }
+
+ function getInput($attr=array()){
+ return nl2br(isset($this->value_list) ? $this->value_list[$this->value] : $this->value);
+ }
+ function loadPOST(){
+ // do nothing, readonly field
+ }
+ function setValueList($list){
+ $this->value_list = $list;
+ return $this;
+ }
+
+}
@@ -0,0 +1,7 @@
+<?php
+class Form_Field_Search extends Form_Field {
+ // WARNING: <input type=search> is safari extention and is will not validate as valid HTML
+ function getInput($attr=array()){
+ return parent::getInput(array_merge(array('type'=>'search'),$attr));
+ }
+}
Oops, something went wrong.

0 comments on commit 075d93f

Please sign in to comment.