Adds AutoConnectInput::isValid function

pull/41/head
Hieromon Ikasamo 5 years ago
parent c99d91c8e4
commit 62ae967e2d
  1. 5
      examples/mqttRSSI/mqttRSSI.ino
  2. 1
      keywords.txt
  3. 1
      src/AutoConnectElementBasis.h
  4. 24
      src/AutoConnectElementBasisImpl.h

@ -292,8 +292,9 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {
// Echo back saved parameters to AutoConnectAux page.
AutoConnectText& echo = aux.getElement<AutoConnectText>("parameters");
echo.value = "Server: " + serverName + "<br>";
echo.value += "Channel ID: " + channelId + "<br>";
echo.value = "Server: " + serverName;
echo.value += mqttserver.isValid() ? String(" (OK)") : String(" (ERR)");
echo.value += "<br>" + "Channel ID: " + channelId + "<br>";
echo.value += "User Key: " + userKey + "<br>";
echo.value += "API Key: " + apiKey + "<br>";
echo.value += "Update period: " + String(updateInterval / 1000) + " sec.<br>";

@ -30,6 +30,7 @@ handleClient KEYWORD2
handleRequest KEYWORD2
home KEYWORD2
host KEYWORD2
isValid KEYWORD2
join KEYWORD2
load KEYWORD2
loadElement KEYWORD2

@ -105,6 +105,7 @@ class AutoConnectInputBasis : virtual public AutoConnectElementBasis {
}
virtual ~AutoConnectInputBasis() {}
const String toHTML(void) const override;
bool isValid(void) const;
String label; /**< A label for a subsequent input box */
String pattern; /**< Format pattern to aid validation of input value */

@ -11,6 +11,7 @@
#define _AUTOCONNECTELEMENTBASISIMPL_H_
#include "AutoConnectElementBasis.h"
#include <regex.h>
/**
* Generate an HTML <button> element. The onclick behavior depends on
@ -66,6 +67,29 @@ const String AutoConnectInputBasis::toHTML(void) const {
return html;
}
/**
* Evaluate the pattern as a regexp and return whether value matches.
* Always return true if the pattern is undefined.
* @return true The value matches a pattern.
* @return false The value does not match a pattern.
*/
bool AutoConnectInputBasis::isValid(void) const {
regex_t preg;
bool rc = true;
if (pattern.length()) {
if (regcomp(&preg, pattern.c_str(), REG_EXTENDED) != 0) {
AC_DEBUG("%s regex compile failed\n", pattern.c_str());
rc = false;
}
else {
regmatch_t p_match[1];
rc = regexec(&preg, value.c_str(), 1, p_match, 0) == 0 ? true : false;
regfree(&preg);
}
}
return rc;
}
/**
* Indicate an entry with the specified value in the value's collection.
* @param value The value to indicates in the collection.

Loading…
Cancel
Save