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' ) );