Custom Fields
Re-Envisioned
Support MB Frontend Submission Custom Field and Form Validation

  • Creator
    Topic
  • #33027
    Nicholas Cox
    Participant

    I have created the following custom field

    
    return sprintf(
    $html = '<input id="' . $targetName .'-' . $fieldType . '" type="text" name="%s" title="' . $field['field_name'] . '" value=" ' . $meta . '">
            <script>
            document.addEventListener("DOMContentLoaded", function(event) {
                const fp = flatpickr("#' . $targetName .'-' . $fieldType . '", 
                {
                    dateFormat: "Y-m-d H:i"
                });
            })
            </script>
            ',
    $field['field_name'],
    $meta
    

    );

    I want to add validation to this and after reading the docs in confused as to how I can make this work with the above hidden input field? basically Flatpickr hides the input field and then uses a html overlay to choose a date. I assume I need to validate e.g. YYYY-MM-DD? or can i just check there value is not null?
    https://docs.metabox.io/validation/

    
    [
        'type'      => 'dc_date_picker_calendar',
        'name'      => 'End Date', 
        'id'        => 'end_date',
        'class'     => 'dc-start-date',
        'required'  =>  true,
    ],
    

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #33033
    Long Nguyen
    Moderator

    Hi,

    The hidden field is ignored to validate, it is updated from this commit https://github.com/wpmetabox/meta-box/pull/1361

    You can create your own JS code to check the empty field value, refer to this topic https://stackoverflow.com/questions/3937513/javascript-validation-for-empty-input-field

    #33039
    Nicholas Cox
    Participant

    Hi Long,

    I am note sure how to go about this using the topic link provided to work with Metabox.

    Just thinking about the 'required' field setting in Metabox. Surely if we specify a hidden field to be 'required' then the jQuery validation could honour it?

    
    array(
        'id'   => 'field_id',
        'type' => 'hidden',
        'required' => true,
    )
    

    Sorry if I'm missing the point but to ignore all hidden fields surely it will not be great for development purposes?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.