Skip to main content
martinl90743871
Known Participant
September 12, 2017
Answered

Modale validation

  • September 12, 2017
  • 1 reply
  • 604 views

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

This topic has been closed for replies.
Correct answer johnrellis

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.

1 reply

johnrellis
johnrellisCorrect answer
Brainiac
September 12, 2017

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.

martinl90743871
Known Participant
September 13, 2017

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?

johnrellis
Brainiac
September 13, 2017

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.