Skip to main content
Inspiring
January 20, 2023
Answered

SDK Dialog Ok button only enabled if edit_field is not empty

  • January 20, 2023
  • 1 reply
  • 407 views

What I want is to have the dialog Ok button only be enabled when a edit_field is not empty.

 

In the post https://community.adobe.com/t5/lightroom-classic-discussions/modale-validation/m-p/9351740 John Ellis gives the following example which I extended by one edit_field.

Setting the check on the popup_menu works, setting it on the popup_menu works not.

 

2 questions:

  1. What am I missing that this doesn't work for the edit_field
  2. What is the most easy way to disable the Ok button if an edit_field is empty.

If possible I want to stay away from using addObserver

 

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"}}
				},
				f:edit_field {
					alignment = 'left',
					place_horizontal = 0,
					value = bind( 'taskDescription' ),
					width_in_chars = 20,
				},			
			},

--			actionBinding = {enabled = LrBinding.keyIsNotNil ("selection", p)}}
			actionBinding = {enabled = LrBinding.keyIsNotNil "taskDescription", p)}}


	end)

.

This topic has been closed for replies.
Correct answer LightroomStatistics

Awesome John

I works, you nailed it again!

1 reply

johnrellis
Legend
January 20, 2023

I'm probably in a minority, but I actually really like Lua as a scripting language. But your issue illustrates why many hate it.  A number of issues, which took a bit to resolve:

 

- There was a syntax error, probably a copy/paste problem:

actionBinding = {enabled = LrBinding.keyIsNotNil "taskDescription", p)

should have been:

actionBinding = {enabled = LrBinding.keyIsNot ("taskDescription", "", p)

 

- The edit_field() needs the "immediate = true" property.  Otherwise, its bound value won't get updated until the keyboard focus leaves the edit field.

 

- The value of p.taskDescription defaults to nil, but as soon as the user edits it and then erases the content of the field, its value changes to the empty string.  So the solution is to initialize p.taskDescription to "" and then use:

      actionBinding = {enabled = LrBinding.keyIsNot ("taskDescription", "", p)}}

 

So a working script is:

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)
    p.taskDescription = ""
    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"}}},
            f:edit_field {alignment = 'left', place_horizontal = 0,
                width_in_chars = 20, immediate = true, 
                value = bind "taskDescription"},  
            f:static_text {title = bind "taskDescription"}},
      actionBinding = {enabled = LrBinding.keyIsNot ("taskDescription", "", p)}}
    end)
LightroomStatisticsAuthorCorrect answer
Inspiring
January 21, 2023

Awesome John

I works, you nailed it again!