Copy link to clipboard
Copied
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:
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)
.
Awesome John
I works, you nailed it again!
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Awesome John
I works, you nailed it again!