Navigation überspringen

[Gelöst] Regex with Toolset form / Show the form depending on how much the user selected

This support ticket is created vor 5 Jahren, 11 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 10 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von ThorstenS967 vor 5 Jahren, 11 Monaten.

Assistiert von: Nigel.

Author
Artikel
#1193088
conditional_how-many.gif

Hi Nigel,
I opened this thread a few weeks ago:
https://toolset.com/forums/topic/regex-with-toolset-form/

The answer from Luo was not helpful for me, because the function doesn't work, nothing happens. I know it is custom code, but I think the problem is not he custom code, it is the function itself. I try to achieve for the form field "Gutscheinnummer" (that only appears if you click on Yes for the question if you have a voucher/Gutschein) to check if it has the following pattern:
1234/567
I created the following regex: '/[0-9]{4}\/[0-9]{3}/gm'

I also wrote in the Facebook group "Toolset User Group" and your colleague Beda answered there with the following documentation:
https://toolset.com/documentation/user-guides/cred-conditional-display-engine/?fbclid=IwAR3QJUB-2zyF-_pBeoq9TIJF4Bla1gAKhEcflr0Ebno7boOiPkRGwkBeXZQ
But as far as I have seen, you don't have the option to output an error message when the regex doesn't match.
Do you have some advice for me?

Also another question:
Is it possible to output the whole form depending on the number, which a user seleted from a drowpdoen menu? For example if I want the user to select the number of people who want to register forn event and the user selects 3, the whole form appears 3 times for each person? Please see the attached gif, this is en example from formidable forms plugin.

Thanks
Thorsten

#1193227

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Screenshot 2019-01-30 at 15.33.03.png

The regex problem I think is just a problem with your regex pattern. I'm not an expert on regular expressions, but with PHP preg_match 'g' is not a recognised option.

I tried the following code on my site with a modified regex pattern and it worked, as you can see in the screenshot.

/**
 * Custom form validation
 */
function tssupp_form_validation($error_fields, $form_data) {

	if (in_array($form_data['id'], array(142))) {

		//split error fields into separate arrays
		list($fields, $errors) = $error_fields;

		// tests
		$regex_pattern = '/[0-9]{4}\/[0-9]{3}/';

		if (!preg_match($regex_pattern, $fields['wpcf-high-number']['value'])) {

			$errors['wpcf-high-number'] = "Not valid pattern";
		}

		$error_fields = array($fields, $errors);
	}

	return $error_fields;
}
add_filter('cred_form_validate', 'tssupp_form_validation', 10, 2);

Try that. You may want to experiment with the pattern to make sure it fully meets your requirements.

As a general rule when trying out any such code you should have your debug log enabled: it would have shown the error with the regular expression.

Regarding the feature available with Formidable, I'm afraid there is no such feature available with Toolset Forms. You must use the Add New button to add extra instances of repeating fields.

#1193320

Thanks for the explanation.
Regarding the Formidable Form Feature -> in toolset repeating fields: You mean this?: https://toolset.com/documentation/getting-started-with-toolset/creating-and-displaying-repeatable-field-groups/front-end-forms-for-repeatable-field-groups/
What do you mean with "Add new" button? Can form fields created with this when the user selected a number of registrations from a dropdown-field?

#1193588

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

In the screen recording from Formidable forms it looks like you are adding multiple instances of repeating fields, not repeatable field groups (the documentation you then link to).

So, what I meant was that when you have a repeating field (i.e. a single field that can have multiple instances), if you include that in a form there is a button to add another instance, and you can keep adding instances of that field.

I understood the screen recording to mean that with Formidable you can specify how many instances to add, but with Toolset Forms you add them one at a time.

#1193638

Ah ok, I understood. I took a look at the repating field documentation of Toolset (https://toolset.com/documentation/user-guides/repeating-fields/), but the problem is, that for example radio buttons can not be used as repeatable fields. If you take a look for example for this event: versteckter Link and you scroll to the bottom you see the registration form made with toolset. So my intetion was, that the user can choose at the beginning of the form, how many of his family members he wants to register for this event and according to his choice the whole form (except the submit button, the recaptcha and the GDPR compliance select box at the bottom) will have
corresponding instances of the same form so that the user don't have to go back to the menu above again, goes to "Veranstaltungen" -> "Übersicht Veranstaltungen" -> choose an event again for every single user he have to register for the event. Is there a way to achieve this with toolset?

Also in your code above (regex) you use the in_array function. But how did you know thw array number 142? How can I find out the array in which php has to search?

#1193687

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Last question first: 142 is the ID of the form, we only want to run that code for a particular form, on my sample site it was 142. Sorry if that was not clear. We use in_array because you can then specify several forms where the code should run, e.g. in_array( 142, 199, 255 )

Regarding adding duplicate sections to a form in the way you describe, Toolset Forms don't offer anything like this.

I had another client some time ago developing a complex site with forms to publish posts that would include multiple repeatable field groups and child posts, which with Toolset Forms each need to be a separate form, and the solution they went with in the end was to use Formidable forms because they have a nice front-end UI for such a use case, but then they had to write custom code to handle the entire form submission handling, to process the content included in a single form submission and then use a combination of standard WordPress functions to publish the various posts, and the Toolset API to establish connections between related posts etc.

If you were comfortable writing PHP that might be a nice option for your site, but if not you'll have to work with the limitations of Toolset Forms.

#1193771

Ok thanks. But where can I find the form_id of the toolset form? Is it the following, which I found out via the google develoepr tools: cred_form_1297_1 (so the number 1297_1)?

Ok, I have experience with PHP but it would be too much time consuming with this solution. Is there a way that after the user submitted the form (now a message is diplayed after submitting the form) to clear all entries and show a new form again, so that the user doesn't need to go back to the menu again as I desribed in an earlier post? (so the form or page is loads new without leaving the page)

#1193874

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Go to Toolset > Post Forms and the list of forms includes the ID of each.

Refreshing the page would be okay?

Unfortunately it is not one of the options for what happens after the page submits.

You can achieve it though using the cred_success_redirect hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect).

Like so:

/**
 * After form submitted reload the same page
 */
add_filter('cred_success_redirect', 'tssupp_reload_page', 10, 3);
function tssupp_reload_page($url, $post_id, $form_data) {

	if (in_array($form_data['id'], array(142))) {  // Edit the form ID

		if (isset($_REQUEST['_cred_cred_prefix_cred_container_id'])) {
			$url = get_permalink($_REQUEST['_cred_cred_prefix_cred_container_id']);
		}
	}

	return $url;
}
#1193936

Unforunately this does not work. The reason is the Ajax-Call after clicking the Submit button. In your documentation it says:
"Please note that for the redirection hooks to work, the form must be set to redirect to a specific page or a post after the submission. If for example, the form options are set to “Keep displaying this form” or “Display a message instead of the form” after submission, the redirection hooks will not work."

But this solution worked, where we already wrote about this topic: (https://toolset.com/forums/topic/help-using-cred_save_data-hook-option-after-sending-form/#post-1146600) .
I think thats the best solution for the moment.

#1194118

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Yes, sorry, for that code to work you have to set the page to redirect—anywhere, it doesn't matter because the code overrides the target destination, but the hook is only triggered if some kind of redirection is selected—and then it will reload the same page where the form is hosted.

(I don't remember the details of your site without reading through our previous threads, but from your previous update it sounds like reloading the page was an option.)

#1194292

Thanks Nigel for your help. The other code for the regex also worked fin for me so I resolve this thread. Thanks again for your help!