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
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
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
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