Is Input Validation Possible?


(Paul) #1

Is there a way to do validation of the inputs when the user presses Done?


Smartapp preference validation
(Eric R) #2

There is a “required” argument for inputs. Here’s an example:

input "dayMode", "mode", title: "Day Mode", required: true

(Paul) #3

@baldeagle072 I am familiar with the required; together with ability to specify a capability, it gives you a rudimentary level of validation.

I have two inputs to specify multiple devices. I would like to confirm that the number of devices specified in the first input is equal to the number of devices specified in the second input.

There could be many more uses.


(Eric R) #4

The first thing that comes to mind is using dynamic pages. It won’t be the most elegant validation, but it could work. You could try something like this:

preferences {
     page name:"setup"
     page name:"validate"
}

def setup() {
     def pageProperties = [
          name: "setup",
          title: "Setup",
          nextPage: "validate",
          uninstall: false
     ]

     return dynamicPage(pageProperties) {
          input "devices1", "capability.switch", title: "Devices 1", required: true 
          input "devices2", "capability.switch", title: "Devices 2", required: true 
     }
}

def validate() {
     if (devices1.size() == devices2.size()) {
          def pageProperties = [
                name: "validate",
                title: "Validated",
                install: true,
                uninstall: true
           ]

           return dynamicPage(pageProperties) {
                text "Hit 'Done' to install"  
           }
     } else {
          def pageProperties = [
                name: "validate",
                title: "Not Validated",
                install: false,
                uninstall: true
           ]

           return dynamicPage(pageProperties) {
                text "The number of devices don't match"
                href "setup", title:"Go Back", description:"Tap to go back"
           }
     }
}

Let me know if this works!


(Paul) #5

@baldeagle072 This is close to perfect. I did make some changes:

  • I added multiple: true to the input statements
  • I got an error when using ‘text’; I use ‘paragraph’ instead
  • rather than using ‘href’ to go back to setup, I use ‘nextPage: setup’. Interestingly, the ‘Done’ button, rather than the ‘Next’ button is displayed for the last page, even if ‘nextPage’ is used.

This is my code:

def validatePage() {
	if (virtual.size() == physical.size()) {
		def pageProperties = [
    		name: "validatePage",
		title: "Validation",
        	install: true,
		uninstall: true
    		]
    
    		return dynamicPage(pageProperties) {
    			section() {
    				paragraph "Press 'Done' to complete Setup"
        		}
		}
	} else {
		def pageProperties = [
    		name: "validatePage",
		title: "Validation",
            	nextPage: "setupPage",
        	install: false,
		uninstall: true
    		]
    
    		return dynamicPage(pageProperties) {
    			section() {
    				paragraph "The number of devices don't match. Press 'Done' to correct the selected devices."
        		}
		}
	}
}