groovy
  1. groovy
  2. GROOVY-3939

@Bindable with validator allows incorrect behaviour on binding creation

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7-rc-2
    • Fix Version/s: 1.8.1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Actually saw this in Griffon 0.3 snapshot, but is a @Bindable issue
    • Number of attachments :
      0

      Description

      Currently a binding's validator only reacts to a change and is not given a chance to react to any initial data.

      Given:

      Model:

      @Bindable String data = 'x'
      

      View:

      textField(id: 'port', columns: 5, text: bind(source: model, 'port',
      mutual: true, validator: { isInteger(port) }))
      

      As created, the textField will contain 'x', even though an isInteger validator method (assuming one exists and is correct) would reject it. If 'y' is subsequently typed into the field, the validator will run and will (presumably)reject the contents.

      More discussion and workaround here: http://archive.codehaus.org/lists/org.codehaus.griffon.user/msg/001701ca7885$a4925f40$edb71dc0$@com.au

        Activity

        Andres Almiray made changes -
        Field Original Value New Value
        Assignee Andres Almiray [ aalmiray ]
        Hide
        Andres Almiray added a comment -

        Tested with 1.8.1

        import groovy.swing.SwingBuilder
        import groovy.beans.Bindable
        
        class Model {
            @Bindable String data = 'x'
        }
        
        new SwingBuilder().edt {
            def isInteger = { v ->
                try {
                    Integer.parseInt(v)
                    true
                } catch (NumberFormatException nfe) {
                    false
                }
            }
            
            bean(new Model(), id: 'model')
            textField(id: 'tf', text: bind(source: model, 'data',
                mutual: true, validator: {isInteger(it)}))
            assert tf.text == ''
            model.data = 'y'
            assert tf.text == ''
            model.data = '1'
            assert tf.text == '1'
            model.data = 'z'
            assert tf.text == '1'
        }
        

        Apparently it works now

        Show
        Andres Almiray added a comment - Tested with 1.8.1 import groovy.swing.SwingBuilder import groovy.beans.Bindable class Model { @Bindable String data = 'x' } new SwingBuilder().edt { def isInteger = { v -> try { Integer .parseInt(v) true } catch (NumberFormatException nfe) { false } } bean( new Model(), id: 'model') textField(id: 'tf', text: bind(source: model, 'data', mutual: true , validator: {isInteger(it)})) assert tf.text == '' model.data = 'y' assert tf.text == '' model.data = '1' assert tf.text == '1' model.data = 'z' assert tf.text == '1' } Apparently it works now
        Andres Almiray made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.8.1 [ 17223 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Hide
        Paul King added a comment -

        Also just noting that there is a built-in DGM#isInteger() method. So you can just use it.isInteger() in the above example with no need to define the isInteger Closure.

        Show
        Paul King added a comment - Also just noting that there is a built-in DGM#isInteger() method. So you can just use it.isInteger() in the above example with no need to define the isInteger Closure.
        Hide
        Paul King added a comment -

        Or instead of this:

        validator: { it.isInteger() }
        

        you could do this:

        import org.codehaus.groovy.runtime.DefaultGroovyMethods as DGM
        ...
        validator: DGM.&isInteger
        
        Show
        Paul King added a comment - Or instead of this: validator: { it.isInteger() } you could do this: import org.codehaus.groovy.runtime.DefaultGroovyMethods as DGM ... validator: DGM.&isInteger

          People

          • Assignee:
            Andres Almiray
            Reporter:
            alpheratz
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: