Results 1 to 5 of 5

Thread: Single form, with custom field binding problem

  1. #1
    Join Date
    Jun 2006
    Location
    Oxford UK
    Posts
    15

    Question Custom field binding for a single form

    Hi,

    I have had a search for this, and seen a few posts that say override onBind() for custom binding, but wasn't quite sure how to do this, and whether it was relevant to my problem.

    I am developing a paginated listing page with filters, number of results per page drop down and previous and next buttons.

    table goes something like this:
    Code:
    <form>
        [10] per page drop down +  [next] and [previous] buttons
      <table>
       <tr>
        <th>column header</th>...repeated per column
      </tr>
      <tr>
         <td>multiple [filter input boxes] for filtering </td>...repeated per 
    column
        <td>[Apply Filter button]</td>  
    </tr>
      <tr>
         <td>records go here along with [select boxes] for each record</td>....
      </tr> 
      ......record row repeats x times .......
      </table>
      [other buttons]
    </form>
    Essentially what I want to do is three different binding actions:
    1. when the next and previous buttons are used, bind only the number of records per page drop down, and no other fields.
    2. When the Apply filter button is pressed, bind only the filters for the form, and no other fields.
    3. When other buttons on the form are pressed, bind only the select boxes next to the records.
    The reasoning for this is that I only want to apply filters explicitly when the user clicks apply filter, and not bind all fields on the form when users perform other actions, and similar for the other two actions.

    I know this cries out for a simple solution like three separate forms, but due to preciousness of screen space I want to lay things out like this, and forms cannot wrap individual table rows, but either only wrap a whole table or appear inside a table cell, and if three forms/tables were used it would ruin my layout and alignment.

    Does anyone have specific examples on how to override the onBind method so that only specific fields are bound?? or even better and easier just a method that I can include in my formAction that bind specific fields for me?

    Many thanks!
    alpower
    Last edited by alpower; Nov 2nd, 2007 at 06:50 AM. Reason: changed title for clarity

  2. #2
    Join Date
    Dec 2005
    Location
    Croatia
    Posts
    192

    Default

    I think overriding initBinder() is the way to go. For example:

    Code:
    protected void initBinder(RequestContext ctx, DataBinder binder) {
    	super.initBinder(ctx, binder);
    
    	String[] allowed = ...get allowed fields from somewhere (request scope, for example);
    		
    	// Register allowed fields to data binder
    	binder.setAllowedFields(allowed);
    }
    This method is called automaticaly every time form action creates new data binder.


    Regards,
    Igor.

  3. #3
    Join Date
    Jun 2006
    Location
    Oxford UK
    Posts
    15

    Thumbs up Thanks for the solution!

    Thanks Igor - worked like a charm! posted here in case it is of help to anyone (or they pick major holes in it!)

    added following code to my specific FormAction

    Code:
    private String[] allowedFields;
    
    public Event bindAndValidateOnlyFilters(RequestContext context) throws Exception {
            allowedFields = new String[] {"filter1","filter2"}; // fields on page
            return super.bindAndValidate(context); // as per the bindAndValidate method
        }
    
        public Event bindOnlyItemsPerPage(RequestContext context) throws Exception {
            allowedFields = new String[] {"itemsPerPage"}; // fields on page
            return super.bind(context); // just bind this time
        }
    
        @Override
        protected void initBinder(RequestContext context, DataBinder binder) {
            super.initBinder(context, binder);
            // Register allowed fields to data binder if not null
            if (allowedFields != null) binder.setAllowedFields(allowedFields);
            //reset flag
            allowedFields = null; //reset for the next time
        }
    allowing me to specify in my flow XML for example:
    Code:
    <action-state id="gotonextpage">
            <action bean="formAction" method="bindOnlyItemsPerPage"/>
            <action bean="formAction" method="goToNextPage"/>
            <transition to="recordlist"/>
        </action-state>
    Sorted! One way I just bind a drop down on one particular action, and the other I bind and validate only a few filters on another.

    Am sure there is a more elegant way of coding the allowedFields - but it works and is pretty simple!

    Many thanks again!
    alpower
    Last edited by alpower; Nov 2nd, 2007 at 08:33 AM. Reason: tweaked example

  4. #4
    Join Date
    Dec 2005
    Location
    Croatia
    Posts
    192

    Default

    In one of my past projects I used to define "allowed fields" as flow attributes or request scope attributes - they would be defined in flow definition, for every action state, so it was clearly visible what parameters will be bound for particular action state. Then, in my initBinder() method, I just checked if there are "allowed fileds" defined (since initBinder() can access request context it is possible to check flow attributes and request scope).

    The good thing about this is that no special binding methods are neccessary - you allways just call ordinary bind(). And there is no need to use additional member varible in form action, which looks ugly, imho.

  5. #5
    Join Date
    Jun 2006
    Location
    Oxford UK
    Posts
    15

    Default Thanks

    ..sounds a much more elegant solution - will try and give that a go.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •