• Resolved rick2rick

    (@rick2rick)


    It is possible to create two buttons with different post statuses on frontend form without ajax? I have managed did it only with Post Statuses field, but I need only buttons, without additional choices. One button is Publish, other if Draft.

    By side, I did through “Ajax call” but is not clear how to validate and redirect post after submission.

    add_action('acfe/fields/button/name=submit', 'my_acf_button_pending', 10, 2);
    

    function my_acf_button_pending ($field, $post_id){
    wp_update_post(array(
    ‘ID’ => $post_id,
    ‘post_status’ => ‘pending’
    ));
    wp_send_json_success(‘Pending Success!’);
    }

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    You can add a secondary button in your ACFE Form using different methods:

    • Using the ACFE Button field in the Field Group of the form
    • Using ACFE Button Field or custom HTML in the Override Form Render setting
    • Using custom HTML in the Submit Button setting

    If you decide to use the ACFE Button field, then you should disable the “Ajax Request” setting from the button, since the forms aren’t compatible with it (unless you want to code it yourself).

    With the “Ajax Request” disabled, the form will be submitted when the user will click on the button, just like if he clicked the main “Submit” button.

    You’ll be able to know if the user clicked that secondary button in any ACFE Form hook using get_field('my_button_name'). Usage example:

    add_action('acfe/form/submit/form=my-form', 'my_form_submit', 10, 2);
    function my_form_submit($form, $post_id){
        
        $my_button = get_field('my_button');
        
        if($my_button){
            
            // User clicked the secondary button
            
        }else{
            
            // User clicked on the main button
        
        }
        
    }
    

    If you decide to render a secondary button using a custom HTML like this one:

    <button name="my_button" type="submit" value="button">Button</button>
    

    Then you’ll be able able to know if the user clicked that secondary button in any ACFE Form hook using $_POST['my_button'].

    Please refer to the HTML form and button submit documentation on w3schools if you need some help to understand how HTML pages process custom buttons/input in the $_POST dataset.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter rick2rick

    (@rick2rick)

    Thank you for response! This method work perfectly, but how disable validation errors on Draft button?

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    You can add or disable Field Validation using the acf/validate_value hook. In that hook, you’ll be able to check the $_POST dataset as conditional.

    Note that you cannot disable required fields validation, since there is an additional validation in Javascript for required ACF fields. You can hook in the Javascript filter validation_complete to remove that JS validation tho. See documentation.

    But it’s an advanced usage since you’ll have to set a custom data in Javascript when the user click on the secondary button, and retrieve that data in Javascript later in that filter to know if you have to remove the required validation or not.

    Regards.

    Thread Starter rick2rick

    (@rick2rick)

    Can you please show example of code how to bypass required fields on frontend form? Below method won’t work as I expected.

    $('#draft').click(function(){
    acf.add_filter('validation_complete', function( json, $form ){
        if( json.errors ) {
    	json.errors = 0;
    	json.valid = 1;
        }
        return json;        
    });			
    });
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    ACF JS hooks should be hooked at root level (not inside a jQuery click function). Also note that you should use the acf/input/admin_enqueue_scripts PHP hook in order to enqueue your JS file. See documentation.

    Here is a JS file example, with the ACF JS hook:

    (function($){
    
        if(typeof acf === 'undefined')
            return;
    
        acf.addFilter('validation_complete', function(json, $form){
    
            // do something...
    
            return json;
    
        });
    
    })(jQuery);
    

    Note that as explained in my in my previous answer, what you’re trying to achieve is a very custom and advanced development task, since ACF fields aren’t designed to have their required setting disabled on-the-fly, via Javascript.

    It’s also a strange UX behavior to have required form fields, which become un-required when the user click on a button. I don’t think I ever seen something like that before.

    In theory, you could probably set a custom JS data when the user click on the secondary button, and then retrieve that custom data in the validation_complete hook, to reset errors. It’s all theoretical tho.

    I think it would be easier to simply disable all fields “required” settings in the ACF Field Group, and just add your own validation via PHP using acf/validate_value combined with $_POST conditional from above, and recreate your own “required” validation with PHP. So you don’t have to bother with JS.

    Please note that I cannot provide any further assistance here, as this forum is dedicated to the ACF Extended plugin support and the topic is slowly sliding into custom ACF development questions.

    You can try to reach the Official ACF Support forum, they have some experienced developers who may help you with that task.

    Hope it helps!

    Regards.

    Thread Starter rick2rick

    (@rick2rick)

    actually I have found solution after many tries.

    $('#draft').click(function(){
    	$("form").attr('novalidate', 'novalidate');	
    });

    with acf_reset_validation_errors();

    Thank you for all help!

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Nice find, I didn’t think about that solution!

    Have a nice day!

    Regards.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Draft and Pending buttons’ is closed to new replies.