Custom Fields
Re-Envisioned
Support MB Frontend Submission Validation for post field title

  • Creator
    Topic
  • #8498
    mdiessner
    Participant

    Hi,

    Using this to initiate a form

    $form = '[mb_frontend_form id="question" post_fields="title"]';
    echo do_shortcode( $form );

    How can I make post field TITLE validation?

    Thanks
    Martin

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #8510
    Anh Tran
    Keymaster

    Hi Martin,

    You can add validation rules for post title, just like normal custom fields:

    $meta_boxes[] = [
    	'title'      => 'Simple form',
    	'id'         => 'simple-form',
    	'fields'     => [], // Your fields
    	'validation' => [
    		'rules' => [
    			'post_title' => [
    				'required'   => true,
    				'minlength' => 5,
    			],
    		],
    	],
    ];

    See more validation rules.

    #8523
    mdiessner
    Participant

    Hi Anh,
    THanks - I don't really understand this. I bought MB Frontend where I can add fields, but it appears not built in WP fields, those need to be added via the short code.
    Is there no way to add WP fields via MB Frontend (the visual interface) without programming validation, titles, descriptions etc?
    Thanks
    Martin

    #8524
    mdiessner
    Participant

    Just to be a bit more clear - I bought the MB Builder and MB Frontend and did all the setup of meta boxes with its fields there.

    Here is the code snipped from the MB Builder Edit Metabox screen:

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
          'id' => 'question',
          'title' => 'Question',
          'pages' =>   array (
             'question',
          ),
          'context' => 'after_title',
          'priority' => 'high',
          'autosave' => false,
          'fields' =>   array (
             
            array (
              'id' => 'post_4',
              'type' => 'post',
              'name' => 'Post',
              'post_type' => 'post',
              'field_type' => 'select_advanced',
            ),
             
            array (
              'id' => 'textarea_bchoa_question_text',
              'type' => 'textarea',
              'name' => 'Question',
              'desc' => 'Please enter any additional content here - be as descriptive as you like.',
              'rows' => '3',
            ),
             
            array (
              'id' => 'checkbox_bchoa_notification',
              'name' => 'Notification',
              'type' => 'checkbox',
              'desc' => 'Check this box if you want to be notified of responses',
              'std' => 1,
            ),
             
            array (
              'id' => 'divider_3',
              'type' => 'divider',
              'name' => 'Divider',
            ),
          ),
        );
    
        return $meta_boxes;
    }

    After that, I am using the PHP short code to activate the form as user front end, but I struggle to make the user write into the WP post name field with form validation (and ideally a custom message):

    $meta_boxes[] = [
    		'id'         => 'question',
    		'fields'     => ['title'],
    			'validation' => [
    				'rules' => [
    					'post_title' => [
    						'required'   => true,
    						'minlength' => 5,
    					],
    				],
    			],
    		];
    		
    	$form = '[mb_frontend_form id="question" post_fields="title" submit_button="Submit Question" confirmation="Thank you for submitting your question, which will be addressed shortly!"]';
    	echo do_shortcode( $form );

    Please can you send me the correct code? In the future would be nice to have WP fields available in the form builder for MB Frontend and MB Builder.
    Thanks
    Martin

    #8527
    Anh Tran
    Keymaster

    Please add the validation rules to the meta box code exported from the Builder extension. Here is the code:

    https://pastebin.com/aad19BSR

    And the code to show form is just:

    $form = '[mb_frontend_form id="question" post_fields="title" submit_button="Submit Question" confirmation="Thank you for submitting your question, which will be addressed shortly!"]';
    echo do_shortcode( $form );
    #8533
    mdiessner
    Participant

    Hi Anh,

    I have done that - copied the php code into functions.php and changed the $form code to what you recommend - but its not working.

    What I don't understand I purchased both MB Frontend and MB Builder so why would I need to copy the code - I am under the assumption that both modules do all of that for me without any coding?

    The reason why I want to use the post_title is because we are building an FAQ module - so the title should be set by the user entering data (a question) in the front end.

    Why could MB Frontend not allow in the visual interface to select post_title and post_excerpt and other WP fields to be selected? Then all this would be just working fine without any additional code?

    Pls advise - really need to get this site up and running.

    Thanks
    Martin

    #8538
    Anh Tran
    Keymaster

    Hi Martin,

    Sorry for the confusion. The Builder extension is behind some extensions and we're updating it to catch the others. Specifically:

    - The validation module has not yet been added to the Builder. You can do that via the Custom Attributes for meta box (via the Settings tab) like this:

    View post on imgur.com

    It's not intuitive yet. But it should works (you can verify by seeing the exported Code). Here is the guide to work with custom attributes (written for field settings, but also works for meta box settings).

    - Regarding "Why could MB Frontend not allow in the visual interface to select post_title and post_excerpt", my first attempt to build the extension is to make it also work with existing meta box in the backend, where post fields already exist. Adding post fields to the form will duplicate them in the backend. So, they must be removed somehow.

    Anyway, can you send me an admin account to check why the code doesn't work via contact form?

    Thanks,
    Anh

    #8544
    mdiessner
    Participant

    HI Anh,
    Just sent you a message through contact form.
    Please let me know once you found a solution.
    Thanks
    Martin

    #8557
    Anh Tran
    Keymaster

    Let’s close this topic as we have resolved it via email. The problem was that the custom attributes should be set for meta box, not for fields.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Validation for post field title’ is closed to new replies.