kamil
kamil

Reputation: 3512

getting input data with event "onkeyup"

I have small problem with getting input data while event "onkeyup" is triggered on it. I have such inputs and other datas:

// El-cheapo model for form
private static final ValueMap cheapModel = new ValueMap();
//some code...
//...

Form form = new Form( "pushNotificationForm" );
form.setOutputMarkupId( true );

final Label actualSizeLabel = new Label( ACTUAL_NOTIFICATION_SIZE_ID, new Model<String>( actualSize ) );
actualSizeLabel.setOutputMarkupId( true );

final TextField<String> titleTextField = new TextField<String>( TITLE_TEXTFIELD_ID, new PropertyModel<String>(
            cheapModel, TITLE_TEXTFIELD_ID ) );
    titleTextField.setRequired( true );
    titleTextField.add( new AjaxEventBehavior( "onkeyup" )
    {
        @Override
        protected void onEvent( AjaxRequestTarget ajaxRequestTarget )
        {
            actualSize = Integer.toString( (getTitle() ).getBytes().length );
            ajaxRequestTarget.add( actualSizeLabel );
        }
    } );
form.add( titleTextField );
form.add( actualSizeLabel );
//.. some code
//later
private String getTitle()
{
    return cheapModel.getString( TITLE_TEXTFIELD_ID );
}

The actualSize remains null, untill I submit the form, but why? Any help please

Upvotes: 2

Views: 2576

Answers (2)

biziclop
biziclop

Reputation: 49714

You need AjaxFormComponentUpdatingBehavior. However, since your behaviour is attached to the onkeyup event, you may also want to use throttling.

Upvotes: 5

magomi
magomi

Reputation: 6685

Its because form processing (and subsequently the writing of the values into the model) will be done only when you submit it, not at any ajax callback from some of your components.

I think, you can try to get the raw value from the text field:

String currentValue = titleTextField.getRawInput();

Upvotes: 1

Related Questions