T& AutoConenctAux::getElement<T>(const String& name)
+T& AutoConnectAux::getElement<T>(const String& name)
AutoConnectElementVT* AutoConnectAux::getElements(void)
@@ -1139,10 +1197,10 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b
The AutoConnectElement type behaves as a variant of other element types. Therefore use cast or template to convert to actual type as above. In the sketch, you access the real type of AutoConnectElement after casting it and storing into the variable.
const String auxJson = String("{\"title\":\"Page 1 title\",\"uri\":\"/page1\",\"menu\":true,\"element\":[{\"name\":\"caption\",\"type\":\"ACText\",\"value\":\"hello, world\"}]}");
-AutoConenct portal;
+AutoConnect portal;
portal.load(auxJson);
AutoConnectAux* aux = portal.aux("/page1"); // Identify the AutoConnectAux instance with uri
-AutoConenctText& text = aux->getElement<AutoConnectText>("caption"); // Cast to real type and access members
+AutoConnectText& text = aux->getElement<AutoConnectText>("caption"); // Cast to real type and access members
Serial.println(text.value);
@@ -1203,7 +1261,7 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b
AutoConnect portal;
AutoConnectAux page;
-String onPage(AutoConnectAux& aux, PageArgument& args) {
+String onPage(AutoConectAux& aux, PageArgument& args) {
AutoConnectSubmit& send = aux["send"].as<AutoConnectSubmit>();
if (WiFi.isConnected())
send.enable = (WiFi.getMode() == WIFI_STA);
@@ -1444,12 +1502,12 @@ AutoConnectElements contained in AutoConnectAux object are uniquely identified b
// Get the AutoConnectInput named "feels".
// The where() function returns an uri string of the AutoConnectAux that triggered this handler.
- AutoConnectAux* hello = portal.aux(portal.where());
- AutoConnectInput& feels = hello->getElement<AutoConnectInput>("feels");
-
- // Get the AutoConnectText named "echo".
- AutoConnectText& echo = aux.getElement<AutoConnectText>("echo");
-
+ AutoConnectAux* hello = portal.aux(portal.where());
+ AutoConnectInput& feels = hello->getElement<AutoConnectInput>("feels");
+
+ // Get the AutoConnectText named "echo".
+ AutoConnectText& echo = aux.getElement<AutoConnectText>("echo");
+
// Echo back from input-box to /feels page.
echo.value = feels.value + String(" and a bold world!");
return String("");
@@ -1589,56 +1647,6 @@ ESP8266WebServer class will parse the query string and rebuilds its arguments wh
portal.begin();
- Retrieve the values with WebServer::on handler
-ESP8266WebServer class and the WebServer (for ESP32) class assume that the implementation of the ReqestHandler class contained in the WebServer library will handle the URL requests. Usually, it is sketch code registered by ESP8266WebServer::on (or WebServer::on for ESP32) function.
-When the page transitions from the custom Web page created by AutoConnectAux to the handler registered by ESP2866WebServer::on function, a little trick is needed to retrieve the values of AutoConnectElements. (i.e. the URI of the ESP8266WebServer::on handler is specified in the uri attribute of AutoConnectSubmit) AutoConnect cannot intervene in the procedure in which the ESP8266WebServer class calls the on-page handler by the sketch. Therefore, it is necessary to retrieve preliminary the values of AutoConnectElements using the AutoConnectAux::fetchElement function for value processing with the on-page handler.
-The following sketch is an example of extracting values inputted on a custom web page with an on-page handler and then processing it.
-ESP8266WebServer server;
-AutoConnect portal(server);
-AutoConnectAux Input;
-
-const static char InputPage[] PROGMEM = R"r(
-{
- "title": "Input", "uri": "/input", "menu": true, "element": [
- { "name": "input", "type": "ACInput", "label": "INPUT" },
- {
- "name": "save",
- "type": "ACSubmit",
- "value": "SAVE",
- "uri": "/"
- }
- ]
-}
-)r";
-
-// An on-page handler for '/' access
-void onRoot() {
- String content =
- "<html>"
- "<head><meta name='viewport' content='width=device-width, initial-scale=1'></head>"
- "<body><div>INPUT: {{value}}</div></body>"
- "</html>";
-
- Input.fetchElement(); // Preliminary acquisition
-
- // For this steps to work, need to call fetchElement function beforehand.
- String value = Input["input"].value;
- content.replace("{{value}}", value);
- server.send(200, "text/html", content);
-}
-
-void setup() {
- Input.load(InputPage);
- portal.join(Input);
- server.on("/", onRoot); // Register the on-page handler
- portal.begin();
-}
-
-void loop() {
- portal.handleClient();
-}
-
-
Overwrite the AutoConnectElements
Sketches can update the attributes of AutoConnectElements with two approaches. A one is to assign directly to the attributes of a member variable of its element. The other is to overwrite them with loading the element by AutoConnectAux::loadElement.
The elements for attributes described in the JSON document for AutoConnectElements overwrites the member variables of the target AutoConnectElements. However, AutoConnectAux::loadElement keeps the member variables unchanged if there is no element in the JSON document. This overwriting behavior is the same for the AutoConnect::load function.
@@ -1674,7 +1682,7 @@ ESP8266WebServer class will parse the query string and rebuilds its arguments wh
It's shown as like:
Check data against on submission
-By giving a pattern to AutoConnectInput, you can find errors in data styles while typing in custom Web pages. The pattern is specified by regular expression. If the value during input of AutoConnectInput does not match the regular expression specified in the pattern, its background color changes to pink. The following example shows the behavior when checking the IP address in the AutoConnectInput field.
+By giving a pattern to AutoConnectInput, you can find errors in data styles while typing in custom Web pages. The pattern is specified with regular expression. If the value during input of AutoConnectInput does not match the regular expression specified in the pattern, its background color changes to pink. The following example shows the behavior when checking the IP address in the AutoConnectInput field.
{
"title" : "Page-1",
"uri" : "/page1",
@@ -1690,15 +1698,49 @@ ESP8266WebServer class will parse the query string and rebuilds its arguments wh
}
-
-
-
-
-
-If you are not familiar with regular expressions, you may feel that description very strange. And matter of fact, it is a strange description for those unfamiliar with formal languages. If your regular expression can not interpret the intended syntax and semantics, you can use an online tester. The regex101 is an exceptional online site for testing and debugging regular expressions.
-
+
It's shown as like:
+
+If you are not familiar with regular expressions, you may feel that description very strange. Matter of fact, it's a strange description for those who are unfamiliar with the formal languages. If your regular expression can not interpret the intended syntax and semantics, you can use an online tester. The regex101 is an exceptional online tool for testing and debugging regular expressions.
+
The pattern attribute of AutoConnectInput only determines the data consistency on the web browser based on the given regular expression. In order to guarantee the validity of input data, it is necessary to verify it before actually using it.
-You can validate input data from AutoConnectInput using the isValid function before actually processing it. The isValid function determines whether the value currently stored in AutoConnectInput matches the pattern.
+You can validate input data from AutoConnectInput using the isValid function before actually processing it. The isValid function determines whether the value currently stored in AutoConnectInput matches the pattern.
+You can also use the AutoConnectAux::isValid function to verify the data input to all AutoConnectInput elements on the custom Web page at once. The two sketches below show the difference between using AutoConnectInput::isValid and using AutoConnectAux::isValid. In both cases, it verifies the input data of the same AutoConnectInput, but in the case of using AutoConnectAux::isValid, the amount of sketch coding is small.
+A common declaration
+const char PAGE[] PROGMEM = R"(
+{
+ "title": "Custom page",
+ "uri": "/page",
+ "menu": true,
+ "element": [
+ {
+ "name": "input1",
+ "type": "ACInput",
+ "pattern": "^[0-9]{4}$"
+ },
+ {
+ "name": "input2",
+ "type": "ACInput",
+ "pattern": "^[a-zA-Z]{4}$"
+ }
+ ]
+}
+)";
+AutoConnectAux page;
+page.load(PAGE);
+
+
+Using AutoConnectInput::isValid
+AutoConnectInput& input1 = page["input1"].as<AutoConnectInput>();
+AutoConnectInput& input2 = page["input2"].as<AutoConnectInput>();
+if (!input1.isValid() || !input2.isValid())
+ Serial.println("Validation error");
+
+
+Using AutoConnectAux::isValid
+if (!page.isValid())
+ Serial.println("Validation error");
+
+
Convert data to actually type
The values in the AutoConnectElements field of the custom Web page are all typed as String. A sketch needs to be converted to an actual data type if the data type required for sketch processing is not a String type. For the typical data type conversion method, refer to section Tips for data conversion.
Transitions of the custom Web pages
@@ -1817,13 +1859,13 @@ ESP8266WebServer class will parse the query string and rebuilds its arguments wh
-