BuddyDev

Search

[Resolved] Form Validation in Simple Front End Post

  • Participant
    Level: Initiated
    Posts: 19
    Ada on #10854

    Hi Brajesh,

    sorry to bother you with this query. Is there a way to make sure that the user has selected at least one option from the list of Custom Taxonomies? The reason I ask is that failure to select any check box results in ALL options to be included in the post after the post has been submitted – this doesn’t make good sense. Can you please guide me in the right direction or is there perhaps a filter to have one taxonomy to be selected as default? Am using SFEP with Blog Categories for Groups.

    As always appreciate your help.

  • Participant
    Level: Initiated
    Posts: 19
    Ada on #13726

    Hi Brajesh, I have tried but could not resolve this issue on my own. Can you please lend a helping hand?

    Many thanks.

  • Keymaster
    (BuddyDev Team)
    Posts: 22512
    Brajesh Singh on #13756

    Hi Ada,
    Thank you for posting.

    I will assist you on the weekend. need to re setup my BuddyBlog install and will assist then.

    Thank you
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2541
    Ravi on #13779

    Hello Adda,

    Please try the following code in your bp-custom.php file. This will make sure atleast one category to be selected by author and let me know if issue resolve or still persists

    
    function buddydev_validate_form( $error, $post ) {
    
    	if ( ! function_exists( 'bcg_get_option' ) ) {
    		return $error;
    	}
    
    	if ( bcg_get_option( 'enable_taxonomy' ) && empty( $post['tax_input'] ) ) {
    		$error['error'] = true;
    		$error['message'] = __( 'Please select atleast one category' );
    	}
    
    	return $error;
    }
    add_filter( 'bsfep_validate_post', 'buddydev_validate_form', 10 , 2 );
    

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 19
    Ada on #13930

    Thank you so very much Mr. Ravi and Mr. Brajesh for getting back with this code. I had to reinstall Windows after a crash thus some delay in getting back to you.

    The code you provided indeed validate the form in that it won’t publish the form if no taxonomy is selected. And it does that by resetting the form i.e. all entries (content, images are then lost). Since this happens when the “Submit” button is pressed the author would lose all the hard work and is presented with a “resetted” form. I also can’t see any error message displayed in case there is no taxonomy is selected. Can this code be slightly tweaked to display the error code in the form instead of resetting the form?

    Many thanks all along for your help.

    Ada

    PS: I am using Simple Frontend post only with Blog Categories with Groups – Buddyblog is not used here.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2541
    Ravi on #13935

    Hello Ada,

    Okey, In that case we have to implement client side validation for the form. Please remove the old code and use the following code and let me know if it works or not.

    
    function buddydev_add_bsfep_custom_js() {
    
    	wp_add_inline_script( 'bsfep-js', "jQuery('div.bp-simple-post-form #submit').click(function() {
    
        var terms = jQuery('ul.simple-post-tax-check-list input[type=checkbox]:checked')
    
        if ( terms.length == 0 ) {
            alert( 'Please select atleast one category' );
            return false;
        }
    
        })" );
    }
    add_action( 'wp_enqueue_scripts', 'buddydev_add_bsfep_custom_js' );
    
    

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 19
    Ada on #13952

    Absolutely ingenious! Thank you very much Mr. Ravi.
    With a bit of adjusting the priority, got it finally to work.

    With gratitude to you and Mr. Brajesh,

    Ada

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2541
    Ravi on #13953

    Thank you for the acknowledgement. I am glad that I could help.

The topic ‘ [Resolved] Form Validation in Simple Front End Post’ is closed to new replies.

This topic is: resolved