Skip Navigation

[Resolved] Forms validation issues

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to use "cred_form_validate" to validate custom field value when submit Toolset post form.

Solution:

Here is an example:

https://toolset.com/forums/topic/forms-validation-issues/#post-1921395

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 3 years, 10 months ago. 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)

Tagged: 

This topic contains 15 replies, has 3 voices.

Last updated by richardB-19 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1912755

Hi Luo,

Thanks, this is now resolved. Please can you split the ticket for the below issue:

While validating the check boxes fields 'At least one check box to be selected', although the images in the same form are submitted with the correct format (Image file) it is still showing the below issues.

1. You can upload only an image file
2. Correct image files are uploaded

Page : hidden link

Please find attached the image for the same.

Thanks.

#1912945

Hello,

I can see the problem in your website, and the problem is abnormal, please check these:
1) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2021, deactivate all custom PHP snippets, and test again

2) If the problem is fixed, then activate other plugins/theme/snippets one by one, try to locate the problem plugin/theme

I suggest you backup your website first.

#1915201

Hi Luo,

I have changed to default theme "Twenty Twenty-One" and also created a new form with only image fields "hidden link".

This form contains the code snippet "post_form_image_validation" to validate the image size and dimensions.

While the images are validated through the code snippet and the image size is less and tried to submit the form, then the same issues are showing.

(i) Property Image One You can only upload an image file
(ii) Property Image One Field is required

As per my understanding, the above are showing whenever the fields either image or any other fields are validated through code snippets, and tried to submit with errors.

The issue here is although we are submitting the correct format, still it is showing but allowing to submit.

Not sure whether this is something an issue in the core functionality which will cause confusion to customers.

Please suggest me.

Thanks.

#1917221

I have tried these in your website:
1) Dashboard-> Toolset-> Settings-> Custom code
find and deactivate item "post_form_image_validation"

2) Test the URL you mentioned above:
hidden link

Select some images, and submit the form, it works fine, see my screenshot: image-validation.JPG

So there should be some problem in your custom PHP codes "post_form_image_validation"

Can you confirm it? thanks

#1917269

Hi Luo,

I think you are right, this issue is arising only when the code snippet "post_form_image_validation" is activated.

This code is provided by your team, could you please have a look at it and let me know whether there are any changes required to that code.

Thanks.

#1918527

Since you have already created another thread for the "post_form_image_validation" issue:
https://toolset.com/forums/topic/split-validation-of-image-field-in-the-user-form-in-toolset/
And Shane is already on it, please follow above thread, if you still need assistance for it.

#1919365

Hi Luo.

I have deactivated the code "post_form_image_validation" and ran the issue in page : hidden link

While validating whether at least one check is selected (code snippet : "post_form_checkbox_one" & "post_form_checkbox_two") the same issue is repeated.

This must be an issue in the core functionality of the image field. Please check and suggest me.

Thanks.

#1920071

I have tried the URL you mentioned above, with below steps:
1) choose below values in those fields:
I am-> An owner
Occupants-> Yes
AMENITIES-> Air Conditioner
FLATMATE PREFERENCE-> Age group-> 18 to 24 years
FLATMATE PREFERENCE-> Languages-> Assamese
EXISTING FLATMATES-> Age group-> 18 to 24 years
EXISTING FLATMATES-> Languages-> Assamese
Property Image One-> Choose an image
Property Image Two-> Choose an image
Property Image Three-> Choose an image

Submit the form, it works fine, I can submit the form without any problem

If I:
EXISTING FLATMATES-> Age group-> None
EXISTING FLATMATES-> Languages-> None

Submit the form, I can see the correct meesage:
Age Group: At least one checkbox is required - one
Languages: At least one checkbox is required - one

In order to avoid any misunderstanding, please provide detail steps to reproduce the same problem:

While validating whether at least one check is selected (code snippet : "post_form_checkbox_one" & "post_form_checkbox_two") the same issue is repeated.

#1920913

Hi Luo,

Please follow the below steps:

1. I am * = "A flatmate"
2. Select image in Property Image One* field.
3. Select image in Property Image Two* field.
4. Select image in Property Image Three* field.
5. None of the check boxes in the checkbox fields are selected.

While trying to submit with the above steps, please check the error message in each image field as "Property Image One You can only upload an image file".

Submit twice again and you see the error messages as "Property Image Three You can only upload an image file" and "Property Image Three Field is required".

Thanks.

#1921395

You are using lots of custom codes to validation that post form, and it make the result mess.

I have done below modifications in your website:
Dashboard-> Toolset-> Settings-> Custom codes:
1) Deactivate two items:
- post_form_checkbox_one
- post_form_checkbox_two

2) Create a new one: cred_form_validate_occupants, with below codes:

add_filter( 'cred_form_validate', 'cred_form_validate_occupants', 10, 2 );
function cred_form_validate_occupants( $field_data, $form_data ) {

  //var_dump($_REQUEST['wpcf-existing-flatmates-age-group']); die();
    $target_forms = array( 1137 );
    if ( ! in_array( $form_data['id'], $target_forms ) ) {
        return $field_data;
    }
	   
    // $field_data contains fields values and errors
    list( $fields, $errors ) = $field_data;
  	
    if ( isset( $_POST['occupants'][0] ) &&  $_POST['occupants'][0] == 10){ // Occupants = yes
        if( !isset( $_REQUEST['wpcf-existing-flatmates-age-group'][0]) ){
            $errors[ 'wpcf-existing-flatmates-age-group' ] = 'At least one checkbox is required - one';
        }
        if( !isset( $_REQUEST['wpcf-existing-flatmates-languages'][0]) ){
            $errors[ 'wpcf-existing-flatmates-languages' ] = 'At least one checkbox is required - one';
        }
    }
  
    return array( $fields, $errors );
}

Test it in front-end:
hidden link
I can submit the post form according to the steps you described above:
https://toolset.com/forums/topic/forms-validation-issues/#post-1920913

And if I choose Occupants= yes
I see correct error message:

Age Group: At least one checkbox is required - one
Languages: At least one checkbox is required - one

So this item works on the field "Occupants", Can you confirm it?

And I have checked the codes in that two items:
1) item "post_form_checkbox_one",
this one will always validate "amenities", "flatmate-preference-age-group" and "flatmate-preference-languages" fields, do you need those fields validation or not? if you don't need it, you can deactivate this one
2) item "post_form_checkbox_two"
this one will always validate "existing-flatmates-age-group" and "existing-flatmates-languages" fields, same as above, if you don't need it, you can deactivate this one

#1921617

Hi Luo,

The reason I have "post_form_checkbox_one" and "post_form_checkbox_two" code snippets is to validate the check boxes field as below:

1. Validate at least one checkbox in the below check boxes field is selected. {post_form_checkbox_one}

(i) amenities
(ii) flatmate-preference-age-group
(iii) flatmate-preference-languages

2. Validate at least one checkbox in the below check boxes fields is selected only when Occupants field is 'Yes' (ID "10") [OR] I am field is 'A flatmate' (ID "1"). {post_form_checkbox_two}

(i) existing-flatmates-age-group
(ii) existing-flatmates-languages

Please suggest me the code with one single code snippet for both the above conditions in stead of two code snippets ("post_form_checkbox_one" and "post_form_checkbox_two").

Thanks.

#1922601

Q1) post_form_checkbox_one
I have reviewed this item, it should work as you described above.
And it will validate those three fields always.

Q2) I have modified the item "cred_form_validate_occupants" as below:

add_filter( 'cred_form_validate', 'cred_form_validate_occupants', 10, 2 );
function cred_form_validate_occupants( $field_data, $form_data ) {

  //var_dump($_REQUEST['wpcf-existing-flatmates-age-group']); die();
    $target_forms = array( 1137 );
    if ( ! in_array( $form_data['id'], $target_forms ) ) {
        return $field_data;
    }
	   
    // $field_data contains fields values and errors
    list( $fields, $errors ) = $field_data;
  
    if ( isset( $_POST['occupants'][0] ) &&  ($_POST['occupants'][0] == 10 || $_POST['occupants'][0] == 1) ){ // Occupants = "yes OR "A flatmate"
        if( !isset( $_REQUEST['wpcf-existing-flatmates-age-group'][0]) ){
            $errors[ 'wpcf-existing-flatmates-age-group' ] = 'At least one checkbox is required - one';
        }
        if( !isset( $_REQUEST['wpcf-existing-flatmates-languages'][0]) ){
            $errors[ 'wpcf-existing-flatmates-languages' ] = 'At least one checkbox is required - one';
        }
    }
  
    return array( $fields, $errors );
}

Please test again, check if it is what you want.

#1923963

Hi Luo,

I do not have any issues with the code snippets, but concern is while validating the checkbox seeing the error messages in the image file although there is no issue. Copy pasting the initial conversation of mine in the same ticket.

While validating the check boxes fields 'At least one check box to be selected', although the images in the same form are submitted with the correct format (Image file) it is still showing the below issues.

1. You can upload only an image file
2. Correct image files are uploaded

Page : hidden link

Thanks.

#1925065

My issue is resolved now. Thank you!

#2012637

@pramodk-2 What did you do to fix the issue. I have a form that is used for the author to edit their post and when they edit and don't enter a required field, I am getting the post back with the [Image] Field is required and then above the image field, You can only upload an image file.

We have a custom theme with just a header and footer and custom styles. Nothing else really in the theme. The only plugins, outside of Toolset plugins, are some "The Events Calendar" plugins and a "User Switching" plugin.