• Resolved berttervoert

    (@berttervoert)


    I’m working on a custom meta box which has a number of fields that are required. Looking through the documentation and this forum, I thought I found out how to make fields required, but when I test it, the fields get saved even if they are left empty. I also don’t see any indication in the form that fields are required. Can you help me with this?

    the piece of code I use to indicate that a field is required:

    'attributes' => array(
                       'required' => 'required',
                     ),
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you provide the entirety of your CMB2 field registration, at least for the part that seems to be problematic for this detail? That attributes parameter above is the correct one, but I’m curious if other parts of the configuration are playing a part in things not going as expected here.

    Thread Starter berttervoert

    (@berttervoert)

    Here is a part of the CMB2 registration:

      $prefix = '_wdtv_fiets_route_';
      //  Initiates the metabox
      $cmb = new_cmb2_box( array(
          'id'            =>  'fiets_routes_data',
          'title'         =>   __( 'Fiets routes informatie', 'cmb2' ),
          'object_types'  =>  array( 'fiets_routes' ),
          'context'       =>  'advanced',
          'priority'      =>  'high',
          'show_names'    =>  true, //  show field names on the left
          // 'cmb_styles' =>  false,  //  false to disable the CMB stylesheet
          // 'closed'     =>  true,  //  keep the meatabox closed by default
      ) );
    
      /**
        * Creates the fields to go with the custom metabox
        */
    
      //  small text area field for route description
      $cmb->add_field( array(
        'name'        =>  'Route beschrijving',
        'desc'        =>  'Geef hier een korte beschrijving van de route',
        'id'          =>  $prefix . 'beschrijving_textareasmall',
        'type'        =>  'textarea_small',
        'attributes'  =>  array(
                            'required' => 'required',
                          ),
      ));
    
      //  small text field for route distance
      $cmb->add_field( array(
        'name'        =>  'Lengte route',
        'after_field' =>  ' <b>km</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cmb2-metabox-description"> De (globale) lengte van de route</span>',
        'id'          =>  $prefix . 'afstand_textsmall',
        'type'        =>  'text_small',
        'attributes'  =>  array(
                            'type'      =>  'number',
                            'required'  =>  'required',
                          ),
      ));
    

    The ‘Lengte route’ part is supposed to be numbers only. It shows arrow up and down in the inputfield that produces a number, but you’re still able to type text in the field. Any thoughts on that?

    Thanks in advance,
    Bert.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are you using this inside a Gutenberg editor experience? or are you in the classic version? Asking because I noticed it worked as expected in classic, but it seems that Gutnberg is bypassing.

    Thread Starter berttervoert

    (@berttervoert)

    I have Gutenberg block editor installed on this development environment. I’m using the cmb code in a plugin, not in functions.php. I think it shouldn’t make a difference, but I’m not sure. If it would help I can post the entire code of my plugin via a dropbox link. Please let me know.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Where the code being used to create the CMB2 box isn’t a matter, agreed. It’s the environment when editing the post that is playing a part here. For example, if you change the object type to a post type that doesn’t get Gutenberg, you could try it out and see what I was seeing with something actually getting validated.

    Thread Starter berttervoert

    (@berttervoert)

    Does this mean that CMB2 cannot be used with Gutenberg or does that need some sort of workaround. Or is it possible to create these cutom post types without Gutenberg and still use Gutenberg for the rest of the website?

    I saw that I have the Gutenberg editor enabled for my custom post type. Is it enough if I remove this line:

    'show_in_rest' => true,

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    It can, but obviously there are some spots that we need to review regarding things. However, I also have to believe that this would be a problem outside of CMB2 as well. Some aspect of saving with Gutenberg is not allowing for required attributes to be validated first.

    That said, yes, setting “show_in_rest” to false for the applicable post types would get you further along here, while retaining for the others.

    Thread Starter berttervoert

    (@berttervoert)

    Ok, thanks, I’ll try that out. If I run into more problems, I’ll start a new post.

    Thank you so far.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘make fields required’ is closed to new replies.