Support Forum
Support › MB Frontend Submission › image_upload validation errorResolved
hi i am using image_upload on front end form when i add image in image_upload field and then submit it still shows field is required while i added image .
waiting for reply..
Thanks for letting me know. Let me check it.
I've just checked the field and I don't see the problem. Can you give me more details? What's the code for the meta box?
Hi please check the following code
array(
'id' => 'gallery_upload',
'name' => esc_html__( 'Image Upload', 'your-prefix' ),
'type' => 'image_upload',
'admin_columns' => 'before title',
'required' => true,
// Delete image from Media Library when remove it from post meta?
// Note: it might affect other posts if you use same image for multiple posts
'force_delete' => true,
// Maximum image uploads
'max_file_uploads' => 5,
// Display the "Uploaded 1/2 files" status
'max_status' => true,
),
metabox code
$meta_boxes[] = array(
'id' => 'submit_listing',
'title' => esc_html__( 'Submit Listing' ),
'post_types' => array('listing' ),
'context' => 'advanced',
'priority' => 'high',
'autosave' => 'true',
'validation' => array(
'rules' => array(
'post_title'=> array(
'required' => true,
),
'gallery_upload' => array(
'required' => true,
),
// Rules for other fields
),
/////////////////////////
// Optional override of default error messages
'messages' => array(
'gallery_upload' => array(
'required' => 'Please Upload Images',
),
)
I got it. Thanks for your code.
This problem is expected behavior because the validation module doesn't support image_upload
. The fields image_upload, image_advanced works differently from other fields since they use the Media Library and thus, the validation can't inject into the process.
Hi Anh Tran
Ok how to solve this validation for image upload is very important. Can you provide any quick solution thanks
I'm very sorry, but I don't have a quick solution for that at the moment.
Ok so when updating the module to add this validation in future release..?
Hi Anh Tran Can we validate after form submit using php backend validation if the image field is empty
in-case as the front validation not works
thanks
Can we validate after form submit using php backend validation if the image field is empty in-case as the front validation not works
Yes, you can do that. Here is a simple code that check for the field value before processing the form, and if it fails, redirect users to an error page:
add_filter( 'rwmb_frontend_validate', function( $is_valid, $config ) {
if ( 'meta-box-id' !== $config['id'] ) {
return $is_valid;
}
if ( empty( $_POST['image_upload_field'] ) ) {
$is_valid = false;
}
return $is_valid;
}, 10, 2 );
Hi I have tried your code and tried to redirect but it goes to post submitted successfully page how to redirect to error page
thanks
Hi,
Sorry for the confusion. I've just updated the plugin to version 2.0.0 with a better validation. Please try again with the same code.
If you want to set custom error message, please see the documentation.
Hi Thanks this validation is working also what about the old validation setting is that fixed ..?
What do you mean the old validation? It's still the same one. I just improved it.