Custom Field and Form Validation

Support MB Frontend Submission Custom Field and Form Validation

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33027
    Nicholas CoxNicholas 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

    #33033
    Long NguyenLong 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 CoxNicholas 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 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.