Saltar navegación

[Resuelto] Need to validate CRED Content form field that it's not null/empty

This support ticket is created hace 1 año, 3 meses. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Este tema contiene 8 respuestas, tiene 2 mensajes.

Última actualización por Minesh hace 1 año, 3 meses.

Asistido por: Minesh.

Autor
Mensajes
#2642135

I already have a script to limit field to 500 words max, but was wondering if that same script can look for a value to make sure there is at least 1 word?

add_filter('cred_form_validate','chr_validation',10,2);

function chr_validation($field_data, $form_data)

{

//field data are field values and errors

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

//check if post_content field is more than 500 words

if ( str_word_count($fields['post_content']['value']) > 500 )

{

//set error message for post_content field

$errors['post_content']='500 word count exceeded. Please, use less words for your description.';

}

//return result

return array($fields,$errors);

}

#2642159

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes, to validate the form you can use the Toolset form's hook: cred_form_validate

But to ensure the validation should be fired for your desired form you should add if condition to check your form ID like its added with the following reply:
=> https://toolset.com/forums/topic/what-is-the-slug-for-a-featured-image/#post-2635967

If you do not want to add another hook you can use the same hook and add multiple conditions of if/else to check for multiple fields.

For example:

add_filter('cred_form_validate','func_check_validation_form_form_356',10,2);
function func_check_validation_form_form_356($error_fields, $form_data) {
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form 
    if ( ($form_data['id']==356) ) {

        if(!empty($fields['_featured_image']['file_data'])) {
            list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['file_data']['tmp_name']);   
            if ($width != 500 && $height != 500) {
                //set error message for featured image
                $errors['_featured_image'] = 'Image size must be 500 * 500, your image dimensions are '.$width.' * '.$height;
            }
        }

if ( str_word_count($fields['post_content']['value']) > 500 ) {

//set error message for post_content field
$errors['post_content']='500 word count exceeded. Please, use less words for your description.';
}
  
    }
    //return result
    return array($fields,$errors);
}

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

#2642215

Sorry, one more clarification. A user can leave the featured image field blank, but if they upload an image, it needs to be in the 500x500px dimensions. How would we change the code?

if(!empty($fields['_featured_image']['file_data'])) {
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['file_data']['tmp_name']);
if ($width != 500 && $height != 500) {
//set error message for featured image
$errors['_featured_image'] = 'Image size must be 500 * 500, your image dimensions are '.$width.' * '.$height;
}
}

#2642671

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I see with the code you shared you already added the if condition to check the featured image is empty or not:

if(!empty($fields['_featured_image']['file_data'])) {

It is not working?

#2642695

I must have an overlap with another script that sets a default image. Here's what I am trying to achieve.

If user comes to CRED form and doesn't upload any image, then we set a default image for the post based on a script you all created for me.

If the user does choose to upload their own image, they should receive an validation error if that image is not 500px X 500px where they have to provide a different image in those dimensions or remove their image from the CRED form and submit the form without an image which will then attach the default image.

I believe right now the script stops them dead if they don't upload any image.

#2642705

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

You want to allow exact 500 x 500 dimension image if uploaded or less than 500 x 500?

Can you please share problem URL where I can see the form with image field upload as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2642725

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Can you please tell me where you added the code that sets the default image if not uploaded.

#2642743

In this Snippet. enlace oculto

#2642793

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I checked that you are using the following code snippet to validate the form:
- enlace oculto

I tried to upload the non 500 x 500 dimension image and it shows the correct error when image uploaded dimension is not 500 x 500. Please check the following screenshot:
- enlace oculto

When no image is uploaded, it redirect on another form with ID 208 and set the default image you set using the "cred_save_data" hook.

#2642797

Thank you!!