tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.45k stars 1.94k forks source link

Add virtual for getValue (for override getValue) #1662

Closed Laxilef closed 5 months ago

Laxilef commented 8 months ago

Hi Currently there is no way to create a dynamic checkbox. I wrote the code for this:

class CheckboxParameter : public WiFiManagerParameter {
public:
  const char* checked = "type=\"checkbox\" checked";
  const char* noChecked = "type=\"checkbox\"";
  const char* trueVal = "T";

  CheckboxParameter(const char* id, const char* label, bool value): WiFiManagerParameter("") {
    init(id, label, value ? trueVal : "0", 1, "", WFM_LABEL_BEFORE);
  }

  const char* getValue() const override {
    return trueVal;
  }

  const char* getCustomHTML() const override {
    return strcmp(WiFiManagerParameter::getValue(), trueVal) == 0 ? checked : noChecked;
  }

  bool getCheckboxValue() {
    return strcmp(WiFiManagerParameter::getValue(), trueVal) == 0 ? true : false;
  }
};

But for it to work, getValue must be overridden.

https://github.com/tzapu/WiFiManager/issues/1654 https://github.com/tzapu/WiFiManager/issues/1639 https://github.com/tzapu/WiFiManager/issues/1111

sbquinlan commented 8 months ago

I'm lost. Can you explain the example a bit? You're setting the default to "T" and making getValue always return "T", but then only calling base getValue(). How does this create a dynamic checkbox?

Laxilef commented 8 months ago

For a checkbox the value must always be filled (1/yes/true/T). State of the checkbox is the attribute "checked". Therefore, we override the getValue method that is used to generate the form: https://github.com/tzapu/WiFiManager/blob/f328cd05cd5aa758b414d8e6961e21a1f6370cab/WiFiManager.cpp#L1777C9-L1777C81

And we get the real value using getCheckboxValue. Then you can change the value and the checkbox in the form will be checked or unchecked.

Yes, this is not the most beautiful method. But I couldn't find another one :(

tablatronix commented 8 months ago

I thought this was already addressed. Thanks will take a look

Laxilef commented 8 months ago

I thought this was already addressed. Thanks will take a look

What do you think about it? :) So far I have to use a fork in my open source projects.