Skip Navigation

[Resolved] Translate generic field validation message

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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 1 year, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2609701
Screenshot 2023-05-24 at 10.33.20 PM.png

Dear Sir/Madam,

Please refer to screenshot, then two select fields are generic fields, I want to change the validation message "This field is required", I also want to have different message to different field, how can I customize the validation message? I have 3 languages to my site, please advise the solution, please advise the steps of translation.

Below please find the code from my post form

				<div class="col-sm-4 bod-field">
					[cred_generic_field type='select' field='gf-bod-month']
					{
					"required":1,
					"default":[],
					"options":[{"value":"","label":"BOD MM"},{"value":"1","label":"Jan"},{"value":"2","label":"Feb"},{"value":"3","label":"Mar"},{"value":"4","label":"Apr"},{"value":"5","label":"May"},{"value":"6","label":"Jun"},{"value":"7","label":"Jul"},{"value":"8","label":"Aug"},{"value":"9","label":"Sep"},{"value":"10","label":"Oct"},{"value":"11","label":"Nov"},{"value":"12","label":"Dec"}]
					}
					[/cred_generic_field]
				</div>
				<div class="col-sm-4 bod-field">
					[cred_generic_field type='select' field='gf-bod-day']
					{
					"required":1,
					"options":[ [get_day_range] ]
					}
					[/cred_generic_field]
				</div>
#2609733
Screenshot 2023-05-24 at 11.09.13 PM.png

Refer to the screenshot, I only name the field name in Traditional Chinese (my default language to my site), I didn't change the default validation message, from the String Translation, I find all the validation messages are assigned as my default language, does it mean I have to put the translated message to the form instead translating the message from WPML String Translation?

When I want to change the default language to the message, it prompts "You have selected strings belonging to a package. Please select all strings from the affected package or unselect these strings.", what is package?

#2610025

Hi,

Thank you for contacting us and I'd be happy to assist.

Your recent questions can be divided into two parts:

Part 1:

> When I want to change the default language to the message, it prompts "You have selected strings belonging to a package. Please select all strings from the affected package or unselect these strings.", what is package?

- The WPML's packaged translation feature allows plugin and theme authors to combine the translation strings from their plugins and themes into a single package, as explained in this guide:
https://wpml.org/documentation/support/string-package-translation/

So in this case, if you'll need to translate a single string coming from the Toolset Forms package, you'll have to translate them all.

Part 2:

The Toolset Forms interface allows you to change the default 'This field is required' message for all the fields in a form. But, you can not set separate messages for separate fields.

To achieve something like this, you'll need a custom function attached to the filter 'cred_filter_field_before_add_to_form':
https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form

For example, suppose your form with ID '1234' has two generic fields with slugs 'generic-field-slug-1' and 'generic-field-slug-2'.

The function to set different empty field validation messages would look like this:


add_filter('cred_filter_field_before_add_to_form', 'custom_required_fields_func', 10, 1);
function custom_required_fields_func($field){

	// check if it is a generic field
	if(isset($field['cred_generic'])) {

		// check if it is a generic field with slug 'generic-field-slug-1'
		if ($field['slug'] == 'generic-field-slug-1' ) {
			// change required validation message
			$field['data']['validate']['required']['message'] = esc_html__( 'This is a custom message 1', 'toolset-forms-1234' );
		}

		// check if it is a generic field with slug 'generic-field-slug-2'
		if ($field['slug'] == 'generic-field-slug-2' ) {
			// change required validation message
			$field['data']['validate']['required']['message'] = esc_html__( 'This is a custom message 2', 'toolset-forms-1234' );
		}

	}
	
	return $field;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Feel free to adjust the form ID, field slugs, and message text as needed. You'll be able to translate these custom messages, in the WPML's string translation section too.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2610163

Dear Waqar,

Many thanks for your reply. May I know how I can translate all messages at once if I have several forms? I expect I don't need to translate every form as the messages are same.

Best regards,

Kelvin.

#2611069

Glad I could help.

Your understanding is correct. Each conditional block inside that code example, checks only for the generic field's slug, irrespective of the form.

So as long as you're using the same generic field slug in your forms, the code should work to customize and translate the validation message in all those forms.