• Resolved wacha

    (@wacha)


    Hello,

    First, thank you for your plugin it’s perfect !
    I just have a question.
    I have a form where people can submit photo.
    I want to validate the form to be sure that at least one photo is uploaded.

    So i use the hook : acfe/form/validation/post/form=creation_ajout
    The probleme is that because it’s an input file (i think) the field is always empty.

    Here is my form :
    https://prnt.sc/9PWAv-4b2NSJ

    Here is my code :
    https://prnt.sc/1roRwOJrHizY

    Here is the content of the email i send to myself to check the content :
    a:3:{i:0;a:1:{s:6:”Photos”;b:0;}i:1;a:1:{s:6:”Photos”;b:0;}i:2;a:1:{s:6:”Photos”;b:0;}}

    And whenever the first upload file is empty or not i always trigger the error.

    Is there something i can do to check if the first file is empty or not ?

    Thank you ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The File Upload field using “browser type” is kinda special, and cannot be validated like other fields. In fact, in the ACF logic the validation is done before the form submission using ajax, and the File field (with browser type) upload files on submission.

    In order to validate a File field, ACF include builtin required/size check using Javascript. If you want to validate the field has an attachment before submission, you’ll have to use the native ACF “required” setting.

    In addition, you have a specific validation filter called acf/validate_attachment (see documentation) which is specifically designed for Image/File fields. it will give you some additional data such as size & name.

    As a side note regarding your debugging method, instead of sending an email with serialized data, I would recommend to enable the WP_DEBUG & WP_DEBUG_LOG constants in your wp-config.php file and use acf_log() within your code. Usage example:

    add_action('acfe/form/validation/form=my-form', 'my_form_validation', 10, 2);
    function my_form_validation($form, $post_id){
        
        // get field input value
        $my_field = get_field('my_field');
        
        // log
        acf_log($my_field);
        
    }

    This will log any kind of data (array, string, boolean…) in the following file: /wp-content/debug.log.

    I wrote an extended guide on How to Debug WordPress & ACF in this article explainin this process if you’re interested.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter wacha

    (@wacha)

    Hello,

    Thank you for your fast and complete answer.

    In order to validate a File field, ACF include builtin required/size check using Javascript. If you want to validate the field has an attachment before submission, you’ll have to use the native ACF “required” setting.

    I start using that option but i use a reapeter to show my photo fields.
    This repeater show from the loading 3 files field for the photos.
    When i check the required field, the 3 photos field are required and i just want at least one field to be filled.
    So this solution didn’t work for my case.

    In addition, you have a specific validation filter called?acf/validate_attachment?(see documentation) which is specifically designed for Image/File fields. it will give you some additional data such as size & name.

    When i tried this, it’s valide the attachment but i can’t use to check if the field is fill or not.
    It’s only triggered when the field is filled not when it’s empty.

    (Thank you for the debugging lesson :))

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Well, if you can’t set a Required attribute on the Repeater then I think you’ve reached the limit of the ACF logic regarding the Repeater + File sub fields. Unfortunately I’m not aware of any other solution regarding your specific case.

    The only possible way to workaround this would be to do your custom validation using the ACF JS API either on the Repeater or each File sub-fields.

    Unfortunately I cannot really provide further assistance here, as ACF Extended Form feature is just the ACF back-end logic displayed on the front-end. Your issue is more tied to ACF File itself, as you could have the same validation problem in the WP Admin for example.

    You could try to contact the ACF Support here, they might be able to provide further assistance.

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Form validation with input file upload’ is closed to new replies.