Groovy
  1. Groovy
  2. GROOVY-3939

@Bindable with validator allows incorrect behaviour on binding creation

    Details

    • Type: Bug Bug
    • Status: 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

      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

        alpheratz created issue -
        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 ]
        Resolution Fixed [ 1 ]
        Fix Version/s 1.8.1 [ 17223 ]
        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
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12732900 ] Default workflow, editable Closed status [ 12744634 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12970543 ] Default workflow, editable Closed status [ 12978350 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        613d 10h 59m 1 Andres Almiray 19/Aug/11 12:24
        Resolved Resolved Closed Closed
        9h 59m 1 Paul King 19/Aug/11 22:23

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development