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.
This commit is contained in:
Emmanuel BENOîT 2012-02-07 09:25:10 +01:00
parent 47b759d993
commit cdb2e952f5
3 changed files with 153 additions and 76 deletions

View file

@ -0,0 +1,124 @@
<?php
interface FieldView
{
public function getFieldType( );
public function render( Field $field , $prefix );
}
class FieldView_Label
implements FieldView
{
public function getFieldType( )
{
return 'label';
}
public function render( Field $field , $prefix )
{
return HTML::make( 'span' )
->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;
}
}

View file

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

View file

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