Device preference inputs not respecting "required:true"

Hi,

I am writing a device driver with several text preference inputs using the required : true option. An asterisk appears next to the preference in the UI, however it is possible for the respective preferences to be saved with null values. No warning appears as is the case with enum input types??

Code snippet:

preferences {
def refInt = [:]
	    refInt << ["1 min" : "Refresh every minute"]
	    refInt << ["5 min" : "Refresh every 5 minutes"]
	    refInt << ["15 min" : "Refresh every 15 minutes"]
    refInt << ["Disabled" : "Disabled"]
    
    input name: "dimmerIPAddress", type: "string", title: "Dimmer IP Address:", required: true
    input name: "refInt", type: "enum", title: "Automatic Refresh Interval", options: refInt, required: true 

}

image

Cheers

It's not possible to save but I'm guessing you have null values because you had the device created already and then added the preferences to the driver after the device was already created. Required prevents the save from going through until you've given the preferences values.

Perhaps I've misunderstood you, or I've done a bad job of explaining the question.

In the example above, I'm allowed to save preferences for Dimmer IP Address when it is empty, despite it being a mandatory field i.e. required: true. I have found this to be the case for all string type inputs

I just showed the Automatic Refresh Interval field to highlight that it works for enum type inputs.

I think what @codahq was saying is this.
If you already had an instance of the driver created before you finished coding the preferences section, it might explain your issue.
Try creating a new instance of the driver to see if the required:true is respected.

@stephack I just gave this a go, and am getting the same result. I recreated a new driver using the simplified code below, and then tested with a new virtual device. Still allows me to save preferences with null value in input field.

If you have time, I'd be curious if you get the same result.

Cheers

/**
 *  Debug Input Driver
 *
 *  Github: Mark Collins (Technomorph303)
 *  Date: 2020-01-15
 *
 *  Copyright 2020 Mark Collins
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 *  Change Log:
 *
 *  1.0.0 - Initial release
 *
 */

metadata {

    definition (name: "Debug Input Driver", namespace: "Technomorph303", author: "Mark Collins", importUrl: "") {
        capability "Actuator"
        capability "Sensor"
        capability "Switch"
        capability "SwitchLevel"    
       
    }
    
    preferences {
    
       
        input name: "IPAddress", type: "string", title: "IP Address:", required: true

    }
    
}

def installed() {
    log.info "Device installed."
}

def uninstalled() {

    log.info "Device uninstalled."    
}

def updated() {
     log.info "Device updated." 
}  


def on() {
    
}

def off() {
    
}   

def setLevel (level) {
    
}  

def setLevel (level, duration) {

}

I'm not at home but can test when I get there.
In the meantime, try wrapping the input in a section() to see if that changes anything.

I would also use type of "text" as well.

input name: "IPAddress", type: "text", title: "IP Address:", required: true

String works as a type on HE, but at least on SmartThings which HE emulates the API of, it's not supported. Perhaps that why it's not being enforced?

3 Likes

I think you are right sir.

Just checked one of my drivers. I can delete the value in it (this field is required and of type "number") and then click on "save preferences" and it then executes my updated method even though the value was null.

The * should prevent the "save preferences" from working if no value is present.

On the commands though (boxes at the top) required fields are respected and I get a prompt.

2 Likes

I did get the same result.
I also went back into my previously built drivers (that use the "text" syntax that @srwhite suggested) only to realize that they also do not respect the required option. I'm not sure if this has always been the case or if something changed in a recent build because I dont think I ever tested this in any of my drivers. It has always worked in my apps so I assumed it worked in my drivers.

@chuck.schwer is this expected behavior....required:true seems to only be respected in apps and not drivers.

2 Likes

@srwhite, good spot on the type. Have changed mine to text.

@stephack, @gavincampbell thanks for testing and confirming.

3 Likes

I'm going to go out on a limb and say it's not expected behavior because I think I remember when this worked.

nope, appears to be a bug, thanks for bringing it to my attention.

4 Likes