ComboBoxItem and unknown values

Dear,

I’m currently using a ComboBoxItem, linked with a RestDataSource, and don’t want the user to be able to type unknowned values, meaning that If he leaves the field without choosing a right value, then characteres typed should be removed from the textfield.
I may use a SelectItem, but I won’t be able to the use autocomplete feature…

Is there any way to achieve ?

Currently on SVN Trunk (r270)

Alexandre

Making the field type enum will mean the entered value will be flagged as an error, will that work?

Mac version of GWT

Hum… The RestDataSource is used for the list, the ComboBoxItem is on a DynamicForm but without a DataSource.

Then where I can specify the ENUM ?

Alexandre

Part of the code used…

magistratResponsable = new ComboBoxItem( "magistratResponsable" );
magistratResponsable.setWidth( "*" );
magistratResponsable.setCompleteOnTab( true );
magistratResponsable.setColSpan( 3 );
magistratResponsableDS = new RemoteDataSource( "magistratResponsable", ( CriteriaPolicy ) null );
magistratResponsable.setOptionDataSource( magistratResponsableDS );
magistratResponsable.setDisplayField( "displayValue" );
magistratResponsable.setValueField( "id" );

and the RemoteDataSource :

public class RemoteDataSource extends RestDataSource {
	HashMap<String, String>	defaultParamsMap	= null;

	public RemoteDataSource(final String name) {
		this( name, CriteriaPolicy.DROPONCHANGE, null );
	}

	public RemoteDataSource(final String name, final CriteriaPolicy criteriaPolicy, final String[]... defaultParams) {
		// setID( name );
		final DataSourceTextField id = new DataSourceTextField( "id", "ID" );
		id.setPrimaryKey( true );
		final DataSourceTextField display = new DataSourceTextField( "displayValue", "Display Value" );

		setFields( id, display );

		addHandleErrorHandler( new HandleErrorHandler() {
			public void onHandleError(final ErrorEvent event) {
				SC.say( "Data error : " + event.toDebugString() );
			}
		} );

		String urlParams = null;
		if ( criteriaPolicy != null ) {
			setCriteriaPolicy( criteriaPolicy );
			setClientOnly( false );

			if ( ( defaultParams != null ) && ( defaultParams.length > 0 ) ) {
				defaultParamsMap = new HashMap<String, String>();
				for ( final String[] param : defaultParams ) {
					defaultParamsMap.put( param[ 0 ], param[ 1 ] );
				}

				setDefaultParams( defaultParamsMap );
			}
		} else {
			setClientOnly( true );
			if ( ( defaultParams != null ) && ( defaultParams.length > 0 ) ) {
				final StringBuilder params = new StringBuilder();
				for ( final String[] s : defaultParams ) {
					params.append( s[ 0 ] ).append( "=" ).append( s[ 1 ] ).append( "&" );
				}
				urlParams = params.substring( 0, params.length() - 1 );
			}
		}

		setDataFormat( DSDataFormat.XML );
		setDataProtocol( DSProtocol.POSTXML );

		setDataURL( "../services/" + name + ".rservlet" + ( ( urlParams != null ) ? "?" + urlParams : "" ) );

	}
}

ComboBoxItem suggestion…

Dear,

After hours trying to find a good solution, finally I found a hack to achieve my requirement…

If someone get a better/simpler solution, I will appreciate.

First the requirement:
Get a data bound combo box, where the user CANNOT put an unknown value (autocomplete feature but only for allowed values, from the list).
First was to use a SelectItem, but the user cannot type for values… only select.
Second was to use a ComboBoxItem, but if the user do not select/type any value from the list, then the resulting value of the item will be the one he put… (getValue())

How to achieve:

  1. First, add a new function to the ComboBoxItem class
class MyComboBoxItem extends ComboBoxItem {
	public MyComboBoxItem() {
		setType( "ComboBoxItem" );
	}

	public MyComboBoxItem(final JavaScriptObject jsObj) {
		super( jsObj );
	}

	public MyComboBoxItem(final String name) {
		setName( name );
		setType( "ComboBoxItem" );
	}

	public native Boolean isRecordSelected()
	/*-{ 
	var self = this.@com.smartgwt.client.core.DataClass::getJsObj()();
	var isRecordSelected =  ( self.getSelectedRecord() ) ? true : false;
	return @com.smartgwt.client.util.JSOHelper::toBoolean(Z)(isRecordSelected);		
	}-*/;
}

On the previous code, I would be great to add the ‘getSelectedRecord()’ function available from Java…

  1. Second declare a blurRegistred (explained later) :
boolean isSectionBlurRegistered = false;
  1. Third add the ComboBoxItem to the form :
MyComboBoxItem section = new MyComboBoxItem( "section" );
section.setWidth( 250 );
section.setCompleteOnTab( true );
section.addChangedHandler( new ChangedHandler() {
	public void onChanged(final ChangedEvent event) {
		if ( !isSectionBlurRegistered ) {
			section.addBlurHandler( new BlurHandler() {
				public void onBlur(final BlurEvent event) {
					if ( section.isRecordSelected() == false ) {
						section.setValue( "" );
					}
				}
			} );
			isSectionBlurRegistered = true;
		}
	}
} );
  1. Last, an extract from the DataSource of the DynamicForm the field belong to:
final DataSourceTextField section = new DataSourceTextField( "section", "Section" );
section.setForeignKey( "section_DS.section" );
section.setRequired( true );

That’s it, if the use type a value without any matching records, then the combobox will be emptied when leaving the field.

On this code, there is another hack, the ‘isSectionBlurRegistered’ boolean… in fact, the onBlur handler is invoked when the user press a key for the first type in the combo box, not any more after, I don’t know why, may be someone can explain…

Hope that may help someone else.

In any case, it would be great if the smartgwt/client team could introduce this feature, with an attribute like ‘allowUnknownValues (boolean)’

Best Regards

Alexandre