Navigation überspringen

[Gelöst] Regex with Toolset Form

This support ticket is created vor 6 Jahren. 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

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

Zuletzt aktualisiert von Luo Yang vor 6 Jahren.

Assistiert von: Luo Yang.

Author
Artikel
#1169843

Hi Nigel or colleagues,
in the form of an event (for example versteckter Link) you have the choice "Ich habe einen Gutschein". If you click on "Ja", the textfield "Gutscheinnummer" appears. Here you have to enter a coupon number in the following pattern: 4 digits/3 digits (for example 1234/567). I wrote the following filter and inserted it into code-snippets plugin and activated it:

add_filter( 'cred_form_validate', 'coupon_form_check',10,2);
function coupon_form_check($error_fields, $form_data)
  {
    $regex_pattern = '/[0-9]{4}\/[0-9]{3}/gm';
    $validated_field = $fields['wpcf-gutscheinnummer']['value'];

    if (preg_match($regex_pattern, $validated_field) === 0) {
      $errors['wpcf-gutscheinnummer'] = 'Bitte geben Sie eine Gutscheinnummer nach folgendem Muster ein: 4Zahlen/3 Zahlen (1234/567)';
    }
    return array($fields,$errors);
  }

But this doesn't work. Do you know what is wrong with the code? Where is the error message being displayed if the pattern is not entered correctly?

Thanks
Thorsten

#1170293

Dear Thorsten,

According to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

Since you are using PHP function preg_match(), I suggest you follow PHP document to setup your custom PHP codes:
versteckter Link

As a workaround, since the value is using "\" as delimiter, you can explode the value into an array by delimiter "\", then you can setup the preg_match function easily:
versteckter Link

#1170436

Hi Lou,

hmm, I think that I don't need the explod-funtion. If you take a look at this site: versteckter Link
they use it the way I should use it. My question was in the kind of your documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate (the Usage example). Since I only need to check one field, do I have to use an array as it is in the example?

list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==12)
    {
        //check my_field value
        if ($fields['wpcf-my_field']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['wpcf-my_field']='Wrong Value';
        }
        //check if featured image exists
        if (empty($fields['_featured_image']['value']))
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
    //return result
    return array($fields,$errors);

Or can I check my field "gutscheinnummer" in another way that it checks the entered pattern: 4 digits/3 digits (for example 1234/567)? And how can I display the error message, when the user didn't entered the correct pattern?

Thanks
Thorsten

#1171096

Thanks for the details, I have checked again your custom PHP codes:
https://toolset.com/forums/topic/regex-with-toolset-form/#post-1169843

There is one line missing:

list($fields,$errors)=$error_fields;

See the document and example codes you mentioned above:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

And you are right, you can use an array as it is in the example