Announcement Announcement Module
Collapse
No announcement yet.
Multiple CustomEditors for the same Class? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple CustomEditors for the same Class?

    My object has two fields of type Foo. I want both Foos to be used by my SimpleFormController. I have a custom editor (extends PropertyEditorSupport) for Foo, and it works fine to translate one of the inputs into a Foo object. It doesn't work on the second, however.

    The docs say: "Note: Only one single registered custom editor per property path is supported. In case of a Collection/array, do not register an editor for both the Collection/array and each element on the same property." (http://www.springframework.org/docs/...ataBinder.html)

    Is there a workaround for this issue? Or are we just out of luck if we want two fields to use the same custom editor?

  • #2
    Can you post more details? Using the same PropertyEditor to convert two properties on the command should work out of the box.

    Comment


    • #3
      Originally posted by rwerner
      The docs say: "Note: Only one single registered custom editor per property path is supported. In case of a Collection/array, do not register an editor for both the Collection/array and each element on the same property." (http://www.springframework.org/docs/...ataBinder.html)
      That is stating that you cannot register two property editors on a single property (which makes sense, how would spring know how to co-ordinate both of them?) and is different to what you want to do.

      Originally posted by rwerner
      Is there a workaround for this issue? Or are we just out of luck if we want two fields to use the same custom editor?
      AFAIK it should work out of the box.

      You do know PropertyEditors are stateful and therefore a new *instance* should be registered for each property...
      Code:
        public ... initBinder(...) {
          binder.registerCustomEditor(myClass, new MyPropertyEditor());
        }
      would be bad as it would result in the same instance being used for each property, resulting in the second invocation overwritting the first...


      try:

      Code:
        public ... initBinder(...) {
          binder.registerCustomEditor(myClass, "fieldA", new MyPropertyEditor());
          binder.registerCustomEditor(myClass, "fieldB", new MyPropertyEditor());
        }

      Comment

      Working...
      X