libreform / wp-libre-form

Easy native HTML5 forms for WordPress. Version 1.5 is unmaintained, but works without issue. 2.0 has been rewritten from the ground, and can be found at https://github.com/libreform/libreform
https://wordpress.org/plugins/wp-libre-form
GNU General Public License v3.0
67 stars 27 forks source link

How to add a select box with dynamic fields? #127

Closed despecial closed 6 years ago

despecial commented 6 years ago

Couldn't find an example how to add dynamic values to a select box. wplf_dynamic_values doesnt seem to support it.

Any idea how I can inject the html code without using a shortcode (a temp hack)?

Thanks in advance!

k1sul1 commented 6 years ago

Can't say I've tried to have dynamic select field values, but dynamic values should do the job. After all, they're just strings so I don't see why your couldn't do return "<option value="dyn1">Dyn1</option><option value="dyn2"></option>" by adding to the filter in https://github.com/libreform/wp-libre-form/blob/master/classes/class-wplf-dynamic-values.php#L58

Note that it differs from "traditional" filters a bit. That filter essentially contains a list of functions, that turn into values when called. I'm sick at the moment so I can't help you more right now, but please post what have you tried and we'll see what's wrong.

despecial commented 6 years ago

Hey thanks and get well soon.

The problem is: that's not really dynamic. What if I have dozens of options? I would need to add them in advance.

I made a shortcode to paste all the options to the select field. That seems to work - I havent tested if the values are stored within the database. Cant look into that until this evening.

A filter to overwrite the options or even better - to insert new fields would be awesome.

PS: is it possible to disable a form (not rendering) in case of a pre-check? Maybe via the "wplf_pre_validate_submission" action?

k1sul1 commented 6 years ago

That's not dynamic, but the function is. Nothing prevents you from generating the string inside a foreach loop.

despecial commented 6 years ago

Not sure if there is a misunderstanding here.

On the page with the libre form shortcode. I receive a dynamic parameter via $_REQUEST. This triggers a wp_query to populate the select box.

How does the form support php/foreach loops in the backend?

k1sul1 commented 6 years ago

Dynamic value placeholders are transformed "at runtime", when wplf_form is called.

https://github.com/libreform/wp-libre-form/blob/master/classes/class-wplf-dynamic-values.php#L19 https://github.com/libreform/wp-libre-form/blob/master/classes/class-cpt-wplf-form.php#L891

It simply runs the dynamic value functions and replaces the placeholders with the function return values, which should be strings.

despecial commented 6 years ago

I dont think thats what I am talking about. As far as I understand you, this filters single values from pre-entered placeholders (input value="XXXX" to input value="REPLACED_STRING")

What I want it some like that:

<select name="something">
   __PLACEHOLDER__
</select>

becomes

<select name="something">
  <option value="123">Bla</option>
  <option value="234">Bla</option>
  <option value="321">Bla</option>
  <option value="134">Bla</option>
  ...
</select>
k1sul1 commented 6 years ago

<select>%SELECT_OPTIONS%</select> should work just fine. Refer to the README on how to correctly add to the filter. You're going to want something like this in the callback:

$html = '';
foreach ($options as $value => $text) {
  $html = $html . "<option value='$value'>$text</option>\n";
}
despecial commented 6 years ago

Thanks for clearing up! This is very helpful.

Maybe the README/doc can be extended with such examples for the future.

k1sul1 commented 6 years ago

We could probably add a few examples.