Support

Account

Forum Replies Created

  • I can’t seem to edit my original message so I’d like to add a few extra details. I get the follow error:

    Validation failed

    When I look a bit closer it seems like an ajax request is sent but gives a 500 error with the same content as above:

    I don’t know if this is default ACF behaviour but it might give an idea as to what is happening. acf_form_head() is added via the init hook so it’s not a question of not having the correct files enqueued.

  • Just to confirm that I understand correctly, on the job’s page you want a link to apply for the job?

    If that’s the case, why aren’t you passing the post ID of the job as a query variable (e.g. https://yoursite.com/apply-to-jobs/?job=YOUR_JOBS_POST_ID)?

    If you want a secondary slug for your jobs (so the URLs are prettier), it is possible to add an additional endpoint to existing URLs using add_rewrite_endpoint. If you were to go this route you could have a URL that looks like https://yoursite.com/job-a/apply/

    You would then use the template_redirect hook to load a different template that has a form (but you’d still be able to get the original job’s ID) so that you could inject the name of the job in the form.

  • I’m sorry but I never thought it was related to my validation code and more so with acf_form_head() — the former was included to show that I wasn’t doing anything silly that would be causing the issue.

    That being said, I seem to have solved it. I have a custom admin area that needs the use of ACF so I include acf_form_head() on that page. What I didn’t realize was that my code to add it was being called during AJAX calls on the frontend (because admin-ajax.php is considering part of the backend) so it would validate the submission.

    I wrapped it around a wp_doing_ajax() condition and it solved it. I’m frustrated with myself because I’m sure I grepped my plugin for acf_form_head() in my plugin and I’m sure it didn’t come up — clearly a user error there.

    I know I’m not supposed to do what I’m doing but I couldn’t figure out a way to add ACF to an options page that isn’t created with ACF but I digress and I’ve taken enough of your time!

    Thank you so much for your help and I apologize for wasting your time — just know that I’ve been working on this for three days and I posted here out of desperation.

  • Sorry, I understand the forum restricts certain things for a reason but I was unable to delete my reply or edit the previous one. The validation code does get called but my testing data was wrong which is why it wasn’t triggering that particular error.

    If I add “This is a test” at the top of my email validation function it shows it in the Ajax’s return value. What I don’t understand is why all the other validation errors show but not the custom one. I took a screenshot because I thought it would be easier to show than try and explain:

    https://i.imgur.com/NzqZtWA.png

    Notice the custom errors (as well as the default validation) appears in the response but only the default validation errors show up on the form fields themselves.

  • Apologies for the double post but I just noticed that the custom validation for the email never seems to run during the AJAX call. If the email is already in use it fails with an error when the form is submitted but the AJAX never tells me the email is in use and neither does the AJAX response (even when the response is HTML).

    Nevermind, there was a typo in the email used in the system.

  • It is an email field and the redundancy is more of an oversight on my part.

    I commented out the is_email() call and changed the priority to 11. I then tried to submit the form with an email already associated with a user and oddly enough my weak password validation is included in the response but my email one no longer appears.

    Validation failed

    • Your password must contain at least eight (8) characters; including letters, numbers, and special characters.
    • First name value is required
    • Last name value is required
    • Address value is required
    • City value is required
  • So I did some digging around if I could see other threads that had similar issues and found this old thread here (https://support.advancedcustomfields.com/forums/topic/validation-problems-front-end-ajax-acf-5-5/). There was no harm in checking to see if removing the NOT from acf_verify_ajax() and surprisingly all the frontend validation works as intended.

    Obviously editing the core isn’t a real solution but sadly this thread didn’t have a conclusion. One poster mentions that making sure both the the admin and the form’s action match when it comes to passing via https but my action is blank so it goes to the current page.

    Hilariously enough I found an old thread of mine from April (https://support.advancedcustomfields.com/forums/topic/how-to-validate-fields-on-the-same-page-when-using-acf_form/) where I was going through a similar issue and it had to do with when I called acf_form_head() but I’m already doing this in template_include.

    So I put everything back to what it should be and I also went and replaced the __() calls to just return 'bad bad bad'; and got the same HTML response from the AJAX validation but now my custom validation is replaced with “bad bad bad”.

    This is a bit of a doozy!

  • It’s the same. Basically the viewport scrolls to the top of the form where a notification bar can be found “Validation failed. X fields require attention”. However, each blank field has an error message “Please fill out this field.”. If I look at the admin-ajax.php request the response looks the same as above (except my custom validation errors are omitted).

    Is vanilla validation baked directly into the ACF’s javascript?

  • When I look at my network tab the AJAX request comes back with an 200 OK and response is as follows:

    Validation failed

    • Your email value is required

    This is generated by ACF. Wouldn’t a white space or other character also break other ACF validation? All the vanilla validation rules work, just the custom ones break.

  • What happens if you just changed the table name in your filter’s SQL query?

    $results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'NEW_TABLE_NAME ORDER BY ID ASC');

    If the ID values remain the same then it should work seamlessly.

    Let me know if I misunderstood.

  • You could create a custom capability and update capabilities for the roles you want to have access to your post type.

    For example, in ACF’s post type creation screen go to the permissions tab. Once there click on “Rename Capabilities”. Add a singular and plural value, in this example I named them “test” and “tests” respectively (I suggest you use something that makes more sense).

    As a test I went ahead and added access to a post type to only the administrator role. In your functions.php file add the following code:

    add_action( 'init', function(){
        $role_object = get_role( 'administrator' );
    
        // add $cap capability to this role object
        $role_object->add_cap( 'YOUR_CAP_VALUE' );
        $role_object->add_cap( 'edit_YOUR_CAP_VALUE' );
        $role_object->add_cap( 'read_YOUR_CAP_VALUE' );
        $role_object->add_cap( 'delete_YOUR_CAP_VALUE' );
    
        $role_object->add_cap( 'edit_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'edit_other_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'publish_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'read_private_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'delete_YOUR_PLURAL_CAP_VALUE' );
        $role_object->add_cap( 'create_YOUR_PLURAL_CAP_VALUE' );
    });

    Make sure to replace both YOUR_CAP_VALUE and YOUR_PLURAL_CAP_VALUE to whatever you pick as the capability names. So in my example they would be “test” and “tests”.

    I suggest checking out the WP documentation on creating post types and the capabilities if you would like more granular control.

  • So this took quite a lot of effort to figure out. Turns out acf_form_head() it needs to be before any output but if you do it too soon it causes all sorts of errors. I thought using WP’s init hook would work, and it did for the most part, but it would break AJAX validation (hence the middle page).

    I tried a few different hooks but it seems like template_include is the one that fixed the issue.

    If you’re getting a 500 error page when an field is blank but the rest seems to work properly, try changing where your acf_form_head() function is called.

  • Try adding acf_form_head() within your first php block of code. The white space between the block is probably breaking the redirect.

    Like this:

    <?php 
    /* Template Name: Talent Submission */ 
    
    acf_form_head();
    get_header();
    ?>
Viewing 13 posts - 1 through 13 (of 13 total)