Support Forum
Support › MB Relationships › Form verification - required fields stopped workingResolved
Hello,
I use MB Relationships to connect a Custom Post Type to posts. My CPT edit page has a custom metabox which contains a form, some fields are normally required to save / update the CPT.
Until a few months ago, if a required field wasn't filled, users could not save the post (and an error message appeared inside the form). But the form verification is not working anymore, the post can be saved with empty form, except if I disable MB Relationship plugin (or do not use add_action( 'mb_relationships_init', function () {...} in my code).
Any idea what could prevent the form to be verified ? I thought maybe the MB_relationship metaboxes are saved prior to the form, causing the post to update too early, but I don't know how to test or change this.
Help greatly appreciated 🙂
Best regards
Hi,
I've tested this case on my local site and do not see any issue. A required field and a relationship between a CPT and post, the CPT still does not save if the required field isn't filled.
Please deactivate all plugins except Meta Box and MB Relationships then check this issue again. You can also mark the relationship required by using this code.
'from' => 'CPT',
'to' => array(
'post_type' => 'post',
'field' => array(
'required' => true
),
)
Hi Long, thank you for your fast answer
I've tried deactivating all plugins except Metabox, MB Relationship and my own plugin defining the CTP, I also switched from my child theme to the original, with the same result.
If I add your required field lines to one of the MB relationship metaboxes, as expected, the relationship actually is required and the post cannot be saved. But no difference with my form.
The code is as follows in my plugin :
// CPT CREATION
add_action( 'init', 'ht_custom_post_lien_externe' );
function ht_custom_post_lien_externe() {
$labels = array(
'name' => _x( 'Liens Externes', 'post type general name' ),
'singular_name' => _x( 'Lien Externe', 'post type singular name' ),
);
$args = array(
'labels' => $labels,
'description' => 'Articles, references et sites webs',
'public' => true,
'exclude_from_search' => false,
'menu_position' => 5,
'supports' => array( 'title', 'author' ),
);
register_post_type( 'liens_ext', $args );
}
// TAXONOMy FOR THE CPT
add_action( 'init', 'create_cat_taxonomies' );
function create_cat_taxonomies() {
$labels = array(
'name' => _x( 'Catégorie', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Catégorie', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Chercher une catégorie', 'textdomain' ),
'all_items' => __( 'Toutes les catégories', 'textdomain' ),
'parent_item' => __( 'Catégorie parente', 'textdomain' ),
'parent_item_colon' => __( 'Catégorie parente:', 'textdomain' ),
'edit_item' => __( 'Modifier catégorie', 'textdomain' ),
'update_item' => __( 'Mettre a jour la catégorie', 'textdomain' ),
'add_new_item' => __( 'Ajouter une catégorie', 'textdomain' ),
'new_item_name' => __( 'Nouveau nom de catégorie', 'textdomain' ),
'menu_name' => __( 'Catégories', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categorie' ),
);
register_taxonomy( 'categorie', array( 'liens_ext' ), $args );
}
// ADD METABOXES MB RELATIONSHIP
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'link_to_post', // Vers les articles
'from' => [
'object_type' => 'post',
'post_type' => 'liens_ext',
'admin_column' => true,
'meta_box' => [
'title' => 'Liaison article(s)',
],
],
'to' => [
'object_type' => 'post',
'admin_column' => true,
'meta_box' => [
'title' => 'Lien(s) associé(s)',
],
],
] );
} );
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'link_to_category', // Vers les catégories
'from' => [
'object_type' => 'post',
'post_type' => 'liens_ext',
'admin_column' => true,
'meta_box' => [
'title' => 'Liaison catégorie(s)',
],
],
'to' => [
'object_type' => 'term',
'taxonomy' => 'category',
],
] );
} );
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'link_to_tags', // Vers les étiquettes
'from' => [
'object_type' => 'post',
'post_type' => 'liens_ext',
'admin_column' => true,
'meta_box' => [
'title' => 'Liaison étiquette(s)',
],
],
'to' => [
'object_type' => 'term',
'taxonomy' => 'post_tag',
],
] );
} );
/* REGISTER FORM METABOX */
function hcf_register_meta_boxes2() {
add_meta_box( 'formulaire_liens', // ID attribute of metabox
__( 'Formulaire', 'hcf' ), // Title of metabox visible to user
'formulaire_liens', // Function that prints box in wp-admin
'liens_ext', // Show box for posts, pages, custom, etc.
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'hcf_register_meta_boxes2' );
/* LOAD FORM FROM SEPARATE FILE */
function formulaire_liens( $link ) {
include plugin_dir_path( __FILE__ ) . './form_links.php';
}
/* SAVING POST DATA */
function save_meta_box_liens_ext( $post_id ) {
if ( get_post_type( get_the_ID() ) == 'liens_ext' ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Enregistrement des données du formulaire
// Stuff to save form data....
}
add_action( 'save_post', 'save_meta_box_liens_ext' );
Hi,
Please follow the Basic Usage to create meta boxes and custom fields by using the Meta Box's filter to compatible with MB Relationship.
Let me know how it goes.
Well, that's a lot of work because my form is long, has multiple javascript functions to change dynamically which fields appear and which are required, and the data is stored in a custom table.
It used to work with MB Relationship, so I don't know if the issue comes from my code or MB relationship's update, but I think there might be a simpler solution.
I will try to recreate my form with Meta Box's filter and will come back to let you know the results. If you have another idea in between, do not hesitate cause it could save me a lot of time 🙂
Have a good day !
Hi,
I had a look but changing my own form for Meta Box's would really be too long, and will not let me do all I'm doing through wordpress' function add_metabox.
But I found out more precisely the source of the problem : it was not MB Relationship, but Meta Box plugin, when it updated from version 5.2.10 to 5.3 !
I guess this is not your field of action anymore, so I'll ask in the right section.
Thanks for your help, you can mark this topic as closed 🙂