From cdb2e952f5eb1314a925afd2dac8c5c9f4ea9823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 7 Feb 2012 09:25:10 +0100 Subject: [PATCH] Form fields clean-up Added a FieldView interface which must be implemented by field renderers. The form's main view will look for FieldView implementations to determine how to render specific field types. The only exception are hidden fields, which are handled separately. --- includes/form/field_views.inc.php | 124 ++++++++++++++++++++++++++++++ includes/form/package.inc.php | 10 +++ includes/form/view.inc.php | 95 +++++------------------ 3 files changed, 153 insertions(+), 76 deletions(-) create mode 100644 includes/form/field_views.inc.php diff --git a/includes/form/field_views.inc.php b/includes/form/field_views.inc.php new file mode 100644 index 0000000..c661175 --- /dev/null +++ b/includes/form/field_views.inc.php @@ -0,0 +1,124 @@ +appendText( $field->value( ) ) + ->setAttribute( 'id' , $prefix . 'field' ); + } +} + + +class FieldView_Text + implements FieldView +{ + + public function getFieldType( ) + { + return 'text'; + } + + public function render( Field $field , $prefix ) + { + return HTML::make( 'input' ) + ->setAttribute( 'type' , 'text' ) + ->setAttribute( 'name' , $field->name( ) ) + ->setAttribute( 'id' , $prefix . 'field' ) + ->setAttribute( 'class' , 'form-text-field' ) + ->setAttribute( 'value' , HTML::from( $field->value( ) ) ); + } +} + + +class FieldView_Password + implements FieldView +{ + + public function getFieldType( ) + { + return 'password'; + } + + public function render( Field $field , $prefix ) + { + return HTML::make( 'input' ) + ->setAttribute( 'type' , 'password' ) + ->setAttribute( 'name' , $field->name( ) ) + ->setAttribute( 'id' , $prefix . 'field' ) + ->setAttribute( 'class' , 'form-text-field' ); + } + +} + + +class FieldView_TextArea + implements FieldView +{ + + public function getFieldType( ) + { + return 'textarea'; + } + + public function render( Field $field , $prefix ) + { + return HTML::make( 'textarea' ) + ->setAttribute( 'name' , $field->name( ) ) + ->setAttribute( 'id' , $prefix . 'field' ) + ->setAttribute( 'class' , 'form-text-field' ) + ->appendText( (string) $field->value( ) ); + } + +} + + +class FieldView_Select + implements FieldView +{ + + public function getFieldType( ) + { + return 'select'; + } + + public function render( Field $field , $prefix ) + { + $select = HTML::make( 'select' ) + ->setAttribute( 'name' , $field->name( ) ) + ->setAttribute( 'id' , $prefix . 'field' ) + ->setAttribute( 'class' , 'form-select' ); + + $selected = $field->value( ); + foreach ( $field->options( ) as $value => $obj ) { + $option = HTML::make( 'option' ) + ->setAttribute( 'value' , $value ) + ->setAttribute( 'disabled' , $obj->disabled ? 'disabled' : null ) + ->appendText( $obj->text ); + if ( "$value" === "$selected" ) { + $option->setAttribute( 'selected' , 'selected' ); + } + $select->appendElement( $option ); + } + return $select; + } + +} diff --git a/includes/form/package.inc.php b/includes/form/package.inc.php index 470d331..f8e3a18 100644 --- a/includes/form/package.inc.php +++ b/includes/form/package.inc.php @@ -3,6 +3,7 @@ $package[ 'requires' ][] = 'box'; $package[ 'files' ][] = 'field'; +$package[ 'files' ][] = 'field_views'; $package[ 'files' ][] = 'form'; $package[ 'files' ][] = 'view'; $package[ 'files' ][] = 'ctrl'; @@ -15,11 +16,20 @@ $package[ 'ctrls' ][] = 'form'; $package[ 'extras' ][] = 'Field'; $package[ 'extras' ][] = 'FieldModifier'; $package[ 'extras' ][] = 'FieldValidator'; +$package[ 'extras' ][] = 'FieldView'; $package[ 'extras' ][] = 'Form'; $package[ 'extras' ][] = 'FormAware'; +$package[ 'extras' ][] = 'FieldView_Label'; +$package[ 'extras' ][] = 'FieldView_Text'; +$package[ 'extras' ][] = 'FieldView_Password'; +$package[ 'extras' ][] = 'FieldView_TextArea'; +$package[ 'extras' ][] = 'FieldView_Select'; + $package[ 'extras' ][] = 'Modifier_TrimString'; + $package[ 'extras' ][] = 'Validator_StringLength'; $package[ 'extras' ][] = 'Validator_InArray'; $package[ 'extras' ][] = 'Validator_IntValue'; $package[ 'extras' ][] = 'Validator_Email'; + diff --git a/includes/form/view.inc.php b/includes/form/view.inc.php index aa6c91e..775a619 100644 --- a/includes/form/view.inc.php +++ b/includes/form/view.inc.php @@ -4,6 +4,7 @@ class View_Form extends BaseURLAwareView { protected $form; + protected $fieldTypes; public function __construct( Form $form ) { @@ -28,90 +29,32 @@ class View_Form return $result; } - - protected function renderPasswordField( $field , $prefix ) - { - return HTML::make( 'input' ) - ->setAttribute( 'type' , 'password' ) - ->setAttribute( 'name' , $field->name( ) ) - ->setAttribute( 'id' , $prefix . 'field' ) - ->setAttribute( 'class' , 'form-text-field' ); - } - - - protected function renderTextField( $field , $prefix ) - { - return HTML::make( 'input' ) - ->setAttribute( 'type' , 'text' ) - ->setAttribute( 'name' , $field->name( ) ) - ->setAttribute( 'id' , $prefix . 'field' ) - ->setAttribute( 'class' , 'form-text-field' ) - ->setAttribute( 'value' , HTML::from( $field->value( ) ) ); - } - - - protected function renderTextArea( $field , $prefix ) - { - return HTML::make( 'textarea' ) - ->setAttribute( 'name' , $field->name( ) ) - ->setAttribute( 'id' , $prefix . 'field' ) - ->setAttribute( 'class' , 'form-text-field' ) - ->appendText( (string) $field->value( ) ); - } - - - protected function renderSelectField( $field , $prefix ) - { - $select = HTML::make( 'select' ) - ->setAttribute( 'name' , $field->name( ) ) - ->setAttribute( 'id' , $prefix . 'field' ) - ->setAttribute( 'class' , 'form-select' ); - - $selected = $field->value( ); - foreach ( $field->options( ) as $value => $obj ) { - $option = HTML::make( 'option' ) - ->setAttribute( 'value' , $value ) - ->setAttribute( 'disabled' , $obj->disabled ? 'disabled' : null ) - ->appendText( $obj->text ); - if ( "$value" === "$selected" ) { - $option->setAttribute( 'selected' , 'selected' ); - } - $select->appendElement( $option ); - } - return $select; - } - - protected function renderField( $field , $prefix ) { - switch ( $field->type( ) ) { - - case 'password': - $result = $this->renderPasswordField( $field , $prefix ); - break; - - case 'text': - $result = $this->renderTextField( $field , $prefix ); - break; - - case 'textarea': - $result = $this->renderTextArea( $field , $prefix ); - break; - - case 'select': - $result = $this->renderSelectField( $field , $prefix ); - break; - - default: - throw new Exception( "field " . $field->name() . " has unknown type " . $field->type() ); - + $type = $field->type( ); + if ( array_key_exists( $type , $this->fieldTypes ) ) { + return $this->fieldTypes[ $type ]->render( $field , $prefix ); } - return $result; + throw new Exception( "field " . $field->name() . " has unknown type " . $field->type() ); + } + + private function loadFieldTypes( ) + { + $types = Loader::Find( 'FieldView' ); + $loaded = array( ); + + foreach ( $types as $type ) { + $instance = Loader::Create( $type ); + $loaded[ $instance->getFieldType( ) ] = $instance; + } + + $this->fieldTypes = $loaded; } protected function renderVisibleFields( $target , $prefix ) { + $this->loadFieldTypes( ); foreach ( $this->form->fields( ) as $field ) { if ( $field === null ) { $target->appendElement( HTML::make( 'hr' ) );