Making WordPress.org

Changeset 12090


Ignore:
Timestamp:
09/26/2022 10:23:13 PM (4 years ago)
Author:
coffee2code
Message:

Photo Directory, Uploads: Rework photo submission handler to eliminate use of jQuery Validation (as provided through FU).

  • Disable jQuery Validation of any field
  • Natively report on required fields submitted without value
  • Add JS photoShowFileErrors() to aggregate what will be multiple checks on file input value
  • Pass error class name from PHP
Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js

    r12087 r12090  
    1 function photoSubmitLoaded() {
     1/**
     2 * Initializes things related to the photo upload form.
     3 */
     4function photoSubmitInit() {
    25
    36        // Remove query parameters added by Frontend Uploader.
    … …  
    2932                // Add custom validation for upload file size.
    3033                photo_upload_field.addEventListener( 'change', e => {
    31                         photoShowFileSizeError( photo_upload_field );
     34                        photoShowFileErrors( photo_upload_field );
    3235                } );
    3336        }
    3437
    35         // Customize jQuery Validator, if still in use.
     38        // Disable jQuery Validation, if still in use.
    3639        if ( window.jQuery && window.jQuery.validator ) {
    37                 // Customize error message for invalid file mimetype.
    38                 jQuery.validator.messages.accept = PhotoDir.err_invalid_mimetype;
    39         }
     40                jQuery( photo_upload_form ).validate().settings.ignore = "*";
     41        }
     42
     43        // Show/remove error when any form field value is changed.
     44        photoGetInputFields( photo_upload_form ).forEach( (input, i) => {
     45                input.addEventListener( 'change', e => {
     46                        photoRemoveFieldError( e.target );
     47                } );
     48        } );
    4049
    4150        // Check validity of form fields on submission.
    4251        photo_upload_form.addEventListener( 'submit', e => {
    43                 photoShowFileSizeError( photo_upload_field );
     52                e.preventDefault();
     53
     54                const photo_upload_form = e.target;
     55                const photo_upload_input = document.getElementById( 'ug_photo' );
     56
     57                // Clear any server notices from a previous submit.
     58                document.querySelector( '.ugc-notice' )?.remove();
     59
     60                // Disable submit button.
     61                photoSetSubmitButtonDisabled( photo_upload_form, true );
     62
     63                // Show errors for fields that aren't the file input.
     64                photoShowFieldErrors( photo_upload_form );
     65
     66                // Show errors for file input field.
     67                photoShowFileErrors( photo_upload_input );
     68
     69                // Scroll top of form into view.
     70                document.getElementById( 'wporg-photo-upload' ).scrollIntoView( true );
     71
    4472                if ( photo_upload_form.checkValidity() ) {
    45                         photo_upload_submit.disabled = true;
     73                        photo_upload_form.submit();
    4674                }
     75
     76                return false;
    4777        } );
    4878
     79}
     80
     81/**
     82 * Returns the input fields of interest for generic validation.
     83 *
     84 * Fields that have custom validation should be listed here as exclusions.
     85 *
     86 * @param {HTMLElement} form - The form.
     87 * @returns {HTMLElement[]} Array of input fields.
     88 */
     89function photoGetInputFields( form ) {
     90        return form.querySelectorAll( 'input:not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="file"])');
     91}
     92
     93/**
     94 * Sets the 'disabled' attribute for the submit button of a form.
     95 *
     96 * @param {HTMLElement} form - The form element.
     97 * @param {Boolean} state - True if the button should be disabled, else false.
     98 */
     99function photoSetSubmitButtonDisabled( form, state ) {
     100        const submitButton = form.querySelector('input[type="submit"]');
     101
     102        submitButton.disabled = !!state;
     103}
     104
     105/**
     106 * Shows errors for invalid input fields and remove previously reported errors
     107 * for valid input fields.
     108 *
     109 * Does not handle input that are:
     110 * - hidden
     111 * - buttons
     112 * - file
     113 *
     114 * @param {HTMLElement} form - A form.
     115 */
     116function photoShowFieldErrors( form ) {
     117        photoGetInputFields( form ).forEach( (input, i) => {
     118                photoShowFieldError( input );
     119        } );
     120}
     121
     122/**
     123 * Removes an error message for the given input.
     124 *
     125 * @param {HTMLElement} field - An input field.
     126 */
     127function photoRemoveFieldError( field ) {
     128        field.parentNode.querySelectorAll( '.' + PhotoDir.error_class ).forEach( errorEl => errorEl.remove() );
     129        field.parentNode.querySelectorAll( '.processing' ).forEach( errorEl => errorEl.remove() );
     130}
     131
     132/**
     133 * Shows the error if the input field is invalid, or removes a previously
     134 * reported error if the input field is valid.
     135 *
     136 * @param {HTMLElement} field - An input field.
     137 * @returns {String} Error message, if any.
     138 */
     139function photoShowFieldError( field ) {
     140        photoRemoveFieldError( field );
     141
     142        let errorMessage = field.validationMessage;
     143        const cssClass = PhotoDir.error_class;
     144
     145        if ( ! errorMessage ) {
     146                return '';
     147        }
     148
     149        // Set custom error messages.
     150        if ( field.validity.valueMissing ) {
     151                errorMessage = PhotoDir.err_field_required;
     152        }
     153
     154        errorEl = document.createElement( 'span' );
     155        errorEl.setAttribute( 'class', cssClass );
     156        errorEl.innerHTML = errorMessage;
     157        field.insertAdjacentElement( 'afterend', errorEl );
     158
     159        return '';
     160}
     161
     162/**
     163 * Validates a file upload against multiple criteria.
     164 *
     165 * @param {HTMLElement} field - The HTML file input field element.
     166 * @return {Boolean} True if an error was encountered, else false.
     167 */
     168async function photoCheckFileValidations( field ) {
     169        let error = false;
     170
     171        // Check if no file chosen.
     172        if ( ! error ) {
     173                error = field.validity.valueMissing;
     174        }
     175
     176        // Check for file size error.
     177        if ( ! error ) {
     178                error = photoCheckFileSize( field );
     179        }
     180
     181        return error;
    49182}
    50183
    … …  
    57190 * existing custom validity message is cleared.
    58191 *
    59  * @param {Object} field - The HTML file input field object.
     192 * @param {HTMLElement} field - The HTML file input field element.
     193 * @return {Boolean} True if file size is invalid, else false.
    60194 */
    61195function photoCheckFileSize( field ) {
    … …  
    68202                const file_size = files[0].size;
    69203
    70                 // Note: If changing the error message for either case, ensure the "// Don't show error message..."
    71                 // regex in `photoShowFileSizeError()` still matches them both.
    72204                if ( file_size >= MAX_SIZE ) {
    73205                        field.setCustomValidity( PhotoDir.err_file_too_large );
    74                         return;
     206                        return true;
    75207                } else if ( file_size <= MIN_SIZE ) {
    76208                        field.setCustomValidity( PhotoDir.err_file_too_small );
    77                         return;
     209                        return true;
    78210                }
    79211        }
    … …  
    81213        // No custom constraint violation.
    82214        field.setCustomValidity('');
     215        return false;
     216}
     217
     218/**
     219 * Shows error messages for the file upload input.
     220 *
     221 * @param {HTMLElement} field - The HTML file input field element.
     222 */
     223function photoShowFileErrors( field ) {
     224        // Checks custom file input validation. A custom validity error message gets
     225        // set on the field if a validation fails.
     226        photoCheckFileValidations( field );
     227        photoShowFileError( field );
    83228}
    84229
    … …  
    86231 * Handles the display of the error message for the file upload input.
    87232 *
    88  * If the file input field has a file selected, it is checked to see if it is
    89  * too large or too small. An appropriate error message is shown in either case.
    90  * If no file is selected, or the file is of sufficient size, then no error is
    91  * shown and any existing error for the field is cleared.
    92  *
    93  * This pseudo-mimics and also works around the jQuery Validation handling that
    94  * doesn't play well with custom error reporting like this.
    95  *
    96  * @param {Object} field - The HTML file input field object.
    97  */
    98 function photoShowFileSizeError( field ) {
    99         // Check field for file size validation errors.
    100         photoCheckFileSize( field );
    101 
     233 * @param {HTMLElement} field - The HTML file input field element.
     234 * @return {boolean} True if no error was shown, false if error was shown.
     235 */
     236function photoShowFileError( field ) {
    102237        const errorMessage = field.validationMessage;
    103         const errorId = `${field.id}-error`;
    104         let errorEl = document.getElementById( errorId );
    105 
    106         // Don't show error message for any other error.
    107         if ( ! errorMessage || ! field.validity.customError || ! / MB\.$/.test( errorMessage ) ) {
    108                 errorEl?.remove();
    109                 return;
    110         }
    111 
    112         if ( ! errorEl ) {
    113                 errorEl = document.createElement( 'label' );
    114                 field.after( errorEl );
    115         }
    116 
    117         errorEl.setAttribute( 'id', errorId );
    118         errorEl.setAttribute( 'class', 'custom-error' );
    119         errorEl.setAttribute( 'for', field.id );
    120         errorEl.removeAttribute( 'style' );
    121         errorEl.innerHTML = errorMessage;
     238
     239        // Remove any existing error message.
     240        photoRemoveFieldError( field );
     241
     242        // Return if no legitimate custom error to report.
     243        if ( ! errorMessage ) {
     244                return;
     245        }
     246
     247        photoShowFieldError( field );
    122248}
    123249
    124250document.addEventListener( 'DOMContentLoaded', () => {
    125         photoSubmitLoaded();
     251        photoSubmitInit();
    126252} );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php

    r12089 r12090  
    251251                                'PhotoDir',
    252252                                [
     253                                        'error_class'           => 'error',
     254
     255                                        // Field required.
     256                                        'err_field_required'    => __( 'This field is required.', 'wporg-photos' ),
     257
    253258                                        // File extension.
    254259                                        'err_invalid_mimetype' => __( 'Please select a JPEG image.', 'wporg-photos' ),
Note: See TracChangeset for help on using the changeset viewer.