Skip Navigation

[Résolu] How to debug the cred_form_validate

This support ticket is created Il y a 7 années et 9 mois. 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)

Marqué : 

This topic contains 12 réponses, has 2 voix.

Last updated by kelvinL-2 Il y a 7 années et 9 mois.

Assigned support staff: Luo Yang.

Auteur
Publications
#285001

I added below code in functions.php

function cred_filetype_size_validation($field_data, $form_data) {


    // Field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if ((isset($_FILES['br']['type'])) && (isset($_FILES['br']['size']))) {
   
        //Retrieve file type
        $file_type_uploaded=$_FILES['br']['type'];
          
        //Retrieve file size
        $file_size_uploaded=$_FILES['br']['size'];
          
        //Validate files uploaded, make sure its JPEG file type AND not more than 2MB
        if (!(  ('application/jpeg' == $file_type_uploaded) && ($file_size_uploaded < 2048000) )) {
              
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['the-file-field']='????起??閣下上載之檔案格????是 JPEG 或大??超出 2MB ??制。';  
                  
        }
   
    }
    //return result
    return array($fields,$errors);
}

add_filter('cred_form_validate_2855','cred_filetype_size_validation',10,2);

and CRED form with id 2855

      <div class="cred-field cred-field-br">
			<label class="cred-label">商業登記證</label>
        	[cred_field field="br" post="dealer" value="" urlparam=""]
      </div>

I tried to upload a .gif or .pdf file, the form didn't stop the submission and prompt the error, it continue the submission. What wrong to the code?

Best regards,

Kelvin.

#285145

Sorry there is a typo in the code to the $errors. it should be as below.

function cred_filetype_size_validation($field_data, $form_data)
{
    // Field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if ((isset($_FILES['br']['type'])) && (isset($_FILES['br']['size']))) {
   
        //Retrieve file type
        $file_type_uploaded=$_FILES['br']['type'];
          
        //Retrieve file size
        $file_size_uploaded=$_FILES['br']['size'];
          
        //Validate files uploaded, make sure its JPEG file type AND not more than 2MB
        if (!(  ('application/jpeg' == $file_type_uploaded) && ($file_size_uploaded < 2048000) )) {
              
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['br']='????起??閣下上載之檔案格????是 JPEG 或大??超出 2MB ??制。';  
                  
        }
   
    }
    //return result
    return array($fields,$errors);
}

add_filter('cred_form_validate_2855','cred_filetype_size_validation',10,2);
#285194

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I suggest you try debug your PHP codes line by line, check each line works correctly.

For example, you can check if the file is uploaded to your webserver:

...
// Field data are field values and errors
    list($fields,$errors)=$field_data;
print_r($_FILES);
die(); 
// add above two line
...

Goto your front-ed, and upload the file with your CRED form, it should be able output the variable “$_FILES?? value you need check.

#285287

Dear Luoy,

Thanks for your reply. Is there alternative way to do the debug without stopping the site?

Best regards,

Kelvin.

#285458

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It is the simplest way, you can remove the debug lines after you correct your codes.

#285516
Screen Shot 2015-02-25 at 5.20.02 PM.png

Dear Luoy,

I added the print_r function() but nothing print out. What's wrong to my code?

function cred_filetype_size_validation($field_data, $form_data)
{
    print_r($_FILES['br']);
    die();
    
    // Field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if ((isset($_FILES['br']['type'])) && (isset($_FILES['br']['size']))) {
   
        //Retrieve file type
        $file_type_uploaded=$_FILES['br']['type'];
          
        //Retrieve file size
        $file_size_uploaded=$_FILES['br']['size'];
          
        //Validate files uploaded, make sure its JPEG file type AND not more than 2MB
        if (!(  ('application/jpeg' == $file_type_uploaded) && ($file_size_uploaded < 2048000) )) {
              
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['br']='????起??閣下上載之檔案格????是 JPEG 或大??超出 2MB ??制。';  
                  
        }
   
    }

    return array($fields,$errors);
}
add_filter('cred_form_validate_2855','cred_filetype_size_validation',10,2);

and code in CRED

[credform class="cred-form cred-keep-original"]

	[cred_field field="form_messages" value=""]

<div class="input_details">
	<div class="cred-field cred-field-post_title">
		<label class="cred-label">
車行?稱
</label>
		[cred_field field="post_title" post="dealer" value="" urlparam=""]
	</div>

	<div class="cred-field cred-field-post_content">
		<label class="cred-label">
簡介
</label>
		[cred_field field="post_content" post="dealer" value="" urlparam=""]
	</div>

	<div class="cred-field cred-field-_featured_image">
		<label class="cred-label">
商標圖片
</label>
		[cred_field field="_featured_image" value="" urlparam=""]
	</div>

	<div class="cred-group cred-group-車行詳細資料">
		<div class="cred-header"><h3>車行詳細資料</h3></div>
      
      
            <div class="cred-field cred-field-br">
			<label class="cred-label">
商業登記證
</label>
        	[cred_field field="br" post="dealer" value="" urlparam=""]
      </div>

      
      
      
      
      
      
		<div class="cred-field cred-field-tel">
			<label class="cred-label">
??絡電話
</label>
			[cred_field field="tel" post="dealer" value="" urlparam=""]
		</div>
      
      
		<div class="cred-field cred-field-address">
			<label class="cred-label">
地??
</label>
			[cred_field field="address" post="dealer" value="" urlparam=""]
		</div>
		<div class="cred-field cred-field-office-hour">
			<label class="cred-label">
開放時間
</label>
			[cred_field field="office-hour" post="dealer" value="" urlparam=""]
		</div>
		<div class="cred-field cred-field-url">
			<label class="cred-label">
網??
</label>
			[cred_field field="url" post="dealer" value="" urlparam=""]
		</div>
      

		<div class="cred-field cred-field-services">
			<label class="cred-label">
??務範疇
</label>
            [cred_field field="service" display="checkbox"]

          <!--
          [cred_field field="services" post="dealer" value="" urlparam=""]
-->
		</div>
		<div class="cred-field cred-field-email">
			<label class="cred-label">
電郵地??
</label>
			[cred_field field="email" post="dealer" value="" urlparam=""]
		</div>
	</div>

</div>
	[cred_field field="form_submit" value="Submit" urlparam=""]

[/credform]

#285828

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I assume the custom image field "br" is created with Types, Types pre-pend wpcf- before the field slug, so the meta_key in database is "wpcf-br"
If you are using the debug codes as I mentioned above:

...
// Field data are field values and errors
    list($fields,$errors)=$field_data;
print_r($_FILES);
die(); 
// add above two line
...

You should be able see some results as below:
Array ( [wpcf-br] => Array ( [name] => 3.jpg [type] => image/jpeg [tmp_name] => E:\wamp\tmp\phpBEB6.tmp [error] => 0 [size] => 145206 ) )

as you can see, array key is wpcf-br, Please try replace your codes from:

$_FILES['br']

To:

$_FILES['wpcf-br']
#285982

Dear Luoy.

Yes, you are right. the br is created by Types. I change the code as below

function cred_filetype_size_validation($field_data, $form_data)
{
    print_r($_FILES['wpcf-br']);
    die();
    
    // Field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if ((isset($_FILES['wpcf-br']['type'])) && (isset($_FILES['wpcf-br']['size']))) {
   
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-br']['type'];
          
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-br']['size'];
          
        //Validate files uploaded, make sure its JPEG file type AND not more than 2MB
        if (!(  ('application/jpeg' == $file_type_uploaded) && ($file_size_uploaded < 2048000) )) {
              
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['wpcf-br']='????起??閣下上載之檔案格????是 JPEG 或大??超出 2MB ??制。';  
                  
        }
   
    }
    //return result
    return array($fields,$errors);
}
add_filter('cred_form_validate_2855','cred_filetype_size_validation',10,2);

and got below result.

Array ( [name] => Untitled-1.pdf [type] => application/pdf [tmp_name] => /tmp/phpFBJbqS [error] => 0 [size] => 500957 ) 

After remark the debug script and try to upload a pdf for the br field, the validation bypassed and complete the save process, what's wrong to my code. I expect the file type should be .jpg and size is smaller than 2MB.

Best regards,

Kelvin

#286191

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Please try remove the two debug lines, and test again, If the problem still exists, please duplicate same problem in a test site and fill below private detail box with login details, also point out the problem page URL and CRED form URL, and where I can edit your PHP codes, I need debug in a live website.

#286780

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, I have modified the PHP codes as below:

//Validate files uploaded, make sure its JPEG file type AND not more than 2MB
	if (!($file_type_uploaded == 'image/jpeg') || ($file_size_uploaded > 2048000)) {
		//echo 'No, this is not a valid jpeg file <br/>';
		$errors['br']='????起??閣下上載之檔案格????是 JPEG 或大??超出 2MB ??制。';  
        }

1) in your case it should be "or" logic, so it is "||"
2) the in CRED error array, it should be the slug of Types settings, so it is $errors['br']
Please test again, if the problem is fixed or not.

#286810

dear Luoy,

Thanks for your solution, it works. By the way, how do I know when I should use ['wpcf-br'] or ['br']?

Best regards,

Kelvin

#286817

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It is using same slug as the shortcode in CRED form:
[cred_field field="br" post="dealer" value="" urlparam=""]

#287227

Thanks a lot.