5 Replies Latest reply on Sep 14, 2017 1:25 AM by martinl90743871

    Modale validation

    martinl90743871 Level 1

      Hi All,

       

      Is there a way to have a custom function when a user click on the ok button of a modal?
      I'd like to validate that an edit-field is filled before exiting the modal and if the validation is not correct, show a new message in red inside the modal to show where is the problem (as the form on Internet)

       

      Currently, I have an option by saving the elements and show a new modal if the validation doesn't pass, but I would prefer to do it by binding or something similar...

       


      Thank you
      Martin

        • 1. Re: Modale validation
          johnrellis Most Valuable Participant

          You can enable or disable the action button of the dialog by setting the "actionBinding" parameter to a property binding:

           

              local result = LrDialogs.presentModalDialog {title = "My Dialog",

                  contents = controls,

                  actionBinding = {enabled = {bind_to_object = prop,

                      key = "actionEnabled"}}}

           

          You can use more complicated bindings created by LrBinding, e.g. to call a function that evaluates a more complicated condition.

          • 2. Re: Modale validation
            martinl90743871 Level 1

            Sounds a good solution to me :-) Thank

            A bit less interactive than what I'd like to do but it's still good.

             

            Thank you John.

            • 3. Re: Modale validation
              martinl90743871 Level 1

              I just made my tests.
              It works with checkbox or so. But when I have a radio button, it does a strange stuff.
              If I select an Item, it remove the result as soon as I click on it and I have to click again to get it.

               

              local f = LrView.osFactory()

              local p = LrBinding.makePropertyTable( context )

              local res = LrDialogs.presentModalDialog {

                  title = title,

                  contents = f:row {

                    fill_horizontal = 1,

                    spacing = f:control_spacing(),

                    bind_to_object = p,

                    f:popup_menu {

                      fill_horizontal = 1,

                      items = items,

                      value = bind 'selection',

                    },

                  },

                  actionBinding = {enabled = {bind_to_object = p,key = "selection"}}

                }

               

              -> The button ok is then always active... Did I a mistake?

              • 4. Re: Modale validation
                johnrellis Most Valuable Participant

                Here's a script that works as expected:

                 

                local LrBinding = import "LrBinding"
                local LrDialogs = import "LrDialogs"
                local LrFunctionContext = import "LrFunctionContext"
                local LrView = import "LrView"
                
                local bind = LrView.bind
                
                LrFunctionContext.callWithContext ("test", function (context)
                    local f = LrView.osFactory ()
                    local p = LrBinding.makePropertyTable (context)
                    local res = LrDialogs.presentModalDialog {title = "Test",
                        contents = f:row {bind_to_object = p,
                        f:popup_menu {value = bind ("selection"),
                            items = {{title = "A", value = "A"}, {title = "B", value = "B"}}}},
                        actionBinding = {enabled = LrBinding.keyIsNotNil ("selection", p)}}
                    end)
                

                 

                The problem with your test was that actionBinding.enabled was expecting a binding that yielded the boolean values true or false.  While in most places in Lua and the LR SDK, you can pass nil and non-nil values where booleans are expected, there are a few places that doesn't work, and this appears to be one of them.

                1 person found this helpful
                • 5. Re: Modale validation
                  martinl90743871 Level 1

                  Thank you John!
                  Thhat's working perfectly!!

                   

                  All the best