Hi,
I would like to do validity check on submission on user input value before write to the file. If validity fails I would like alter the user and not to write to the file.
I am wondering where do I do that? Is it some where here?
webserver.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest * request)
{
String inputMessage;
if (request->hasParam(PARAM_SSID))
{
inputMessage = request->getParam(PARAM_SSID)->value();
writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_PSK))
……………………..
Thanks
Johan
Hi.
The user input is saved on the inputMessage variable:
inputMessage = request->getParam(PARAM_SSID)->value();
This is the line that writes that value into SPIFFS:
writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());
So, you need to add your conditions before calling the previous line. Only if the input is valid, you’ll call the writeFile() function.
To send a message to the client, for example:
request->send(200, "text/plain", "invalid input");
Let me know if you need further help.
Regards,
Sara
Hi Sara,
I got it working but only I could send the message if the entry is invalid.
where do I write this statement?
request->send(200, "text/plain", "invalid input");
Thanks
Johan
Hi again.
You need to add a condition that if the input is not valid, it will call that line.
As you did to write in SPIFFS, but in case the input is invalid.
If you need further help, you can share the snippet of code that handles that.
Regards,
Sara
Hi Sara,
Here is the code;
webserver.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest * request)
{
String inputMessage;
if (request->hasParam(PARAM_SSID))
{
inputMessage = request->getParam(PARAM_SSID)->value();
writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_PSK))
{
inputMessage = request->getParam(PARAM_PSK)->value();
writeFile(SPIFFS, “/input_psk.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_IP))
{
inputMessage = request->getParam(PARAM_IP)->value();
int n = inputMessage.length();
char inMsg[n+1];
strcpy(inMsg, inputMessage.c_str());
if(validate_ip(inMsg) == 1)
{
writeFile(SPIFFS, “/input_ip.txt”, inputMessage.c_str());
}
else{
request->send(200, “text/plain”, “invalid input”);
}
}
else if (request->hasParam(PARAM_MASK))
…………………………………………………………………..
…………………………………………………………………
}
else
{
inputMessage = “No message sent”;
}
request->send(200, “text/text”, inputMessage);
});
Thank you.
Johan
Hi.
It looks good to me. That part seems to do exactly what you want.
if(validate_ip(inMsg) == 1) { writeFile(SPIFFS, “/input_ip.txt”, inputMessage.c_str()); } else{ request->send(200, “text/plain”, “invalid input”); }
Isn’t it working? Or is there something missing?
Regards,
Sara