Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
despecial opened this issue Oct 23, 2018 · 9 comments
Closed

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

despecial opened this issue Oct 23, 2018 · 9 comments

Comments

@despecial
Copy link
Contributor

despecial commented Oct 23, 2018

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
Copy link
Member

k1sul1 commented Oct 23, 2018

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
Copy link
Contributor Author

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
Copy link
Member

k1sul1 commented Oct 23, 2018

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

@despecial
Copy link
Contributor Author

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
Copy link
Member

k1sul1 commented Oct 23, 2018

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
Copy link
Contributor Author

despecial commented Oct 23, 2018

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
Copy link
Member

k1sul1 commented Oct 23, 2018

<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
Copy link
Contributor Author

Thanks for clearing up! This is very helpful.

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

@k1sul1
Copy link
Member

k1sul1 commented Oct 23, 2018

We could probably add a few examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants