WordPress.org

Make WordPress Core

Changeset 6303


Ignore:
Timestamp:
11/01/2007 06:23:16 AM (13 years ago)
Author:
ryan
Message:

JS/AJAX form validation from mdawaffe. fixes #5299

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r6299 r6303  
    148148    foreach ( $names as $cat_name ) {
    149149        $cat_name = trim($cat_name);
    150         if ( !$category_nicename = sanitize_title($cat_name) )
    151             die('0');
    152         if ( !$cat_id = category_exists( $cat_name ) )
    153             $cat_id = wp_create_category( $cat_name );
     150        $category_nicename = sanitize_title($cat_name);
     151        if ( '' === $category_nicename )
     152            continue;
     153        $cat_id = wp_create_category( $cat_name );
    154154        $cat_name = wp_specialchars(stripslashes($cat_name));
    155155        $x->add( array(
     
    170170    foreach ( $names as $cat_name ) {
    171171        $cat_name = trim($cat_name);
    172         if ( !$slug = sanitize_title($cat_name) )
    173             die('0');
     172        $slug = sanitize_title($cat_name);
     173        if ( '' === $slug )
     174            continue;
    174175        if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
    175176            $cat_id = wp_insert_term( $cat_name, 'link_category' );
     
    190191    if ( !current_user_can( 'manage_categories' ) )
    191192        die('-1');
     193
     194    if ( '' === trim($_POST['cat_name']) ) {
     195        $x = new WP_Ajax_Response( array(
     196            'what' => 'cat',
     197            'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
     198        ) );
     199        $x->send();
     200    }
     201
    192202    if ( !$cat = wp_insert_category( $_POST ) )
    193203        die('0');
     
    216226    if ( !current_user_can( 'manage_categories' ) )
    217227        die('-1');
     228
     229    if ( '' === trim($_POST['name']) ) {
     230        $x = new WP_Ajax_Response( array(
     231            'what' => 'link-cat',
     232            'id' => new WP_Error( 'name', __('You did not enter a category name.') )
     233        ) );
     234        $x->send();
     235    }
    218236
    219237    $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
     
    271289        if ( !current_user_can( 'edit_post', $pid ) )
    272290            die('-1');
     291        if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
     292            die('1');
    273293        if ( $pid < 0 ) {
    274294            $now = current_time('timestamp', 1);
     
    335355        die('0');
    336356    elseif ( is_wp_error( $user_id ) ) {
    337         foreach( $user_id->get_error_messages() as $message )
    338             echo "<p>$message<p>";
    339         exit;
     357        $x = new WP_Ajax_Response( array(
     358            'what' => 'user',
     359            'id' => $user_id
     360        ) );
     361        $x->send();
    340362    }
    341363    $user_object = new WP_User( $user_id );
  • trunk/wp-admin/edit-category-form.php

    r6289 r6303  
    2525<?php wp_nonce_field($nonce_action); ?>
    2626    <table class="editform" width="100%" cellspacing="2" cellpadding="5">
    27         <tr>
     27        <tr class="form-field form-required">
    2828            <th width="33%" scope="row" valign="top"><label for="cat_name"><?php _e('Category name:') ?></label></th>
    2929            <td width="67%"><input name="cat_name" id="cat_name" type="text" value="<?php echo attribute_escape($category->name); ?>" size="40" /></td>
    3030        </tr>
    31         <tr>
     31        <tr class="form-field">
    3232            <th scope="row" valign="top"><label for="category_nicename"><?php _e('Category slug:') ?></label></th>
    3333            <td><input name="category_nicename" id="category_nicename" type="text" value="<?php echo attribute_escape($category->slug); ?>" size="40" /></td>
    3434        </tr>
    35         <tr>
     35        <tr class="form-field">
    3636            <th scope="row" valign="top"><label for="category_parent"><?php _e('Category parent:') ?></label></th>
    3737            <td>
     
    3939            </td>
    4040        </tr>
    41         <tr>
     41        <tr class="form-field">
    4242            <th scope="row" valign="top"><label for="category_description"><?php _e('Description: (optional)') ?></label></th>
    4343            <td><textarea name="category_description" id="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->description); ?></textarea></td>
  • trunk/wp-admin/edit-link-category-form.php

    r6299 r6303  
    2525<?php wp_nonce_field($nonce_action); ?>
    2626    <table class="editform" width="100%" cellspacing="2" cellpadding="5">
    27         <tr>
     27        <tr class="form-field form-required">
    2828            <th width="33%" scope="row" valign="top"><label for="name"><?php _e('Category name:') ?></label></th>
    2929            <td width="67%"><input name="name" id="name" type="text" value="<?php echo $category->name; ?>" size="40" /></td>
    3030        </tr>
    31         <tr>
     31        <tr class="form-field">
    3232            <th scope="row" valign="top"><label for="slug"><?php _e('Category slug:') ?></label></th>
    3333            <td><input name="slug" id="slug" type="text" value="<?php echo $category->slug; ?>" size="40" /></td>
    3434        </tr>
    35         <tr>
     35        <tr class="form-field">
    3636            <th scope="row" valign="top"><label for="description"><?php _e('Description: (optional)') ?></label></th>
    3737            <td><textarea name="description" id="description" rows="5" cols="50" style="width: 97%;"><?php echo $category->description; ?></textarea></td>
  • trunk/wp-admin/includes/user.php

    r6188 r6303  
    8383    do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
    8484
    85     if (!$update ) {
    86         if ( $pass1 == '' || $pass2 == '' )
    87             $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ));
    88     } else {
    89         if ((empty ( $pass1 ) && !empty ( $pass2 ) ) || (empty ( $pass2 ) && !empty ( $pass1 ) ) )
    90             $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ));
     85    if ( $update ) {
     86        if ( empty($pass1) && !empty($pass2) )
     87            $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
     88        elseif ( !empty($pass1) && empty($pass2) )
     89            $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
     90    } else {
     91        if ( empty($pass1) )
     92            $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
     93        elseif ( empty($pass2) )
     94            $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
    9195    }
    9296
    9397    /* Check for "\" in password */
    9498    if( strpos( " ".$pass1, "\\" ) )
    95         $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ));
     99        $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
    96100
    97101    /* checking the password has been typed twice the same */
    98102    if ( $pass1 != $pass2 )
    99         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ));
     103        $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
    100104
    101105    if (!empty ( $pass1 ))
     
    110114    /* checking e-mail address */
    111115    if ( empty ( $user->user_email ) ) {
    112         $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ));
     116        $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
    113117    } else
    114118        if (!is_email( $user->user_email ) ) {
    115             $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ));
     119            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
    116120        }
    117121
  • trunk/wp-admin/js/cat.js

    r6213 r6303  
    11jQuery( function($) {
     2    var myConfirm = function() { return '' !== $('#newcat').val(); };
    23    $('#jaxcat').prepend('<span id="ajaxcat"><input type="text" name="newcat" id="newcat" size="16" autocomplete="off"/><input type="button" name="Button" class="add:categorychecklist:jaxcat" id="catadd" value="' + catL10n.add + '"/><input type="hidden"/><input type="hidden"/><span id="howto">' + catL10n.how + '</span></span><span id="cat-ajax-response"></span>')
    3     var a = $('#categorychecklist').wpList( { alt: '', response: 'cat-ajax-response' } );
     4    $('#categorychecklist').wpList( { alt: '', response: 'cat-ajax-response', confirm: myConfirm } );
    45} );
  • trunk/wp-admin/users.php

    r6213 r6303  
    455455        echo '<p>' . sprintf(__('Users can <a href="%1$s">register themselves</a> or you can manually create users here.'), get_option('siteurl').'/wp-register.php') . '</p>';
    456456    else
    457         echo '<p>' . sprintf(__('Users cannot currently <a href="%1$s">register themselves</a>, but you can manually create users here.'), get_option('siteurl').'/wp-admin/options-general.php#users_can_register') . '</p>';
     457            echo '<p>' . sprintf(__('Users cannot currently <a href="%1$s">register themselves</a>, but you can manually create users here.'), get_option('siteurl').'/wp-admin/options-general.php#users_can_register') . '</p>';
    458458?>
    459459<form action="#add-new-user" method="post" name="adduser" id="adduser" class="add:user-list:">
    460460<?php wp_nonce_field('add-user') ?>
    461461<table class="editform" width="100%" cellspacing="2" cellpadding="5">
    462     <tr>
     462    <tr class="form-field form-required">
    463463        <th scope="row" width="33%"><?php _e('Username (required)') ?><input name="action" type="hidden" id="action" value="adduser" /></th>
    464464        <td width="66%"><input name="user_login" type="text" id="user_login" value="<?php echo $new_user_login; ?>" /></td>
    465465    </tr>
    466     <tr>
     466    <tr class="form-field">
    467467        <th scope="row"><?php _e('First Name') ?> </th>
    468468        <td><input name="first_name" type="text" id="first_name" value="<?php echo $new_user_firstname; ?>" /></td>
    469469    </tr>
    470     <tr>
     470    <tr class="form-field">
    471471        <th scope="row"><?php _e('Last Name') ?> </th>
    472472        <td><input name="last_name" type="text" id="last_name" value="<?php echo $new_user_lastname; ?>" /></td>
    473473    </tr>
    474     <tr>
     474    <tr class="form-field form-required">
    475475        <th scope="row"><?php _e('E-mail (required)') ?></th>
    476476        <td><input name="email" type="text" id="email" value="<?php echo $new_user_email; ?>" /></td>
    477477    </tr>
    478     <tr>
     478    <tr class="form-field">
    479479        <th scope="row"><?php _e('Website') ?></th>
    480480        <td><input name="url" type="text" id="url" value="<?php echo $new_user_uri; ?>" /></td>
     
    482482
    483483<?php if ( apply_filters('show_password_fields', true) ) : ?>
    484     <tr>
     484    <tr class="form-field form-required">
    485485        <th scope="row"><?php _e('Password (twice)') ?> </th>
    486486        <td><input name="pass1" type="password" id="pass1" />
     
    490490<?php endif; ?>
    491491
    492     <tr>
     492    <tr class="form-field">
    493493        <th scope="row"><?php _e('Role'); ?></th>
    494494        <td><select name="role" id="role">
  • trunk/wp-admin/wp-admin.css

    r6213 r6303  
    183183    background: #fff;
    184184    border: 1px solid #686868;
     185}
     186
     187.form-invalid {
     188    background-color: #FF9999 !important;
    185189}
    186190
  • trunk/wp-includes/classes.php

    r6213 r6303  
    711711
    712712        $response = '';
    713         if ( is_wp_error($data) )
    714             foreach ( $data->get_error_codes() as $code )
     713        if ( is_wp_error($data) ) {
     714            foreach ( $data->get_error_codes() as $code ) {
    715715                $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
    716         else
     716                if ( !$error_data = $data->get_error_data($code) )
     717                    continue;
     718                $class = '';
     719                if ( is_object($error_data) ) {
     720                    $class = ' class="' . get_class($error_data) . '"';
     721                    $error_data = get_object_vars($error_data);
     722                }
     723
     724                $response .= "<wp_error_data code='$code'$class>";
     725
     726                if ( is_scalar($error_data) ) {
     727                    $response .= "<![CDATA[$v]]>";
     728                } elseif ( is_array($error_data) ) {
     729                    foreach ( $error_data as $k => $v )
     730                        $response .= "<$k><![CDATA[$v]]></$k>";
     731                }
     732
     733                $response .= "</wp_error_data>";
     734            }
     735        } else {
    717736            $response = "<response_data><![CDATA[$data]]></response_data>";
     737        }
    718738
    719739        $s = '';
  • trunk/wp-includes/js/wp-lists.js

    r6287 r6303  
    1414        return r;
    1515    },
    16     parseAjaxResponse: function( x, r ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
     16    parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
    1717        var re = $('#' + r).html('');
    1818        if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) {
    19             if ( $('wp_error', x).each( function() { re.append('<p>' + this.firstChild.nodeValue + '</p>'); } ).size() ) {
    20                 return !re.wrap( '<div class="error"></div>' );
     19            var errs = $('wp_error', x);
     20            if ( errs.size() ) {
     21                var err = '';
     22                errs.each( function() {
     23                    var code = $(this).attr('code');
     24                    if ( formField = $('wp_error_data[@code="' + code + '"] form-field', x).text() )
     25                        code = formField;
     26                    wpAjax.invalidateForm( $('#' + e + ' :input[@name="' + code + '"]' ).parents('.form-field:first') );
     27                    err += '<p>' + this.firstChild.nodeValue + '</p>';
     28                } );
     29                return !re.html( '<div class="error">' + err + '</div>' );
    2130            }
    2231            return true;
     
    2736        else if ( 0 === x ) { return !re.html('<div class="error"><p>AJAX is teh b0rked.</p></div>'); }
    2837        return true;
     38    },
     39    invalidateForm: function( jQ ) {
     40        jQ.addClass( 'form-invalid' ).change( function() { $(this).removeClass( 'form-invalid' ); } );
    2941    }
    3042};
     
    92104
    93105        var es = $('#' + s.element + ' :input').not('[@name=_ajax_nonce], [@name=_wpnonce], [@name=action]');
     106        var required = $('#' + s.element + ' .form-required:has(:input[@value=""]), #' + s.element + ' .form-required:input[@value=""]');
     107        if ( required.size() ) {
     108            wpAjax.invalidateForm( required );
     109            return false;
     110        }
     111
    94112        s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( cls[4] || '' ) ) );
    95113        var formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
     
    103121
    104122        s.success = function(r) {
    105             if ( !wpAjax.parseAjaxResponse(r, s.response) ) { return false; }
     123            if ( !wpAjax.parseAjaxResponse(r, s.response, s.element) ) { return false; }
    106124
    107125            $(s.what + ' response_data', r).each( function() {
     
    165183
    166184        s.success = function(r) {
    167             if ( !wpAjax.parseAjaxResponse(r, s.response) ) {
     185            if ( !wpAjax.parseAjaxResponse(r, s.response, s.element) ) {
    168186                clearTimeout(hideTO);
    169187                func = function() { $('#' + s.element).css( 'background-color', '#FF3333' ).show(); list.wpList.recolor(); };
     
    219237
    220238        s.success = function(r) {
    221             if ( !wpAjax.parseAjaxResponse(r, s.response) ) {
     239            if ( !wpAjax.parseAjaxResponse(r, s.response, s.element) ) {
    222240                clearTimeout(dimTO);
    223241                func = function() { $('#' + s.element).css( 'background-color', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass); };
  • trunk/wp-includes/script-loader.php

    r6299 r6303  
    6262        ) );
    6363
    64         $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('jquery'), '20071023' );
     64        $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('jquery'), '20071101' );
    6565        $this->localize( 'wp-lists', 'wpListL10n', array(
    6666            'url' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php'
     
    111111                ) );
    112112            }
    113             $this->add( 'ajaxcat', '/wp-admin/js/cat.js', array( 'wp-lists' ), '20070823' );
     113            $this->add( 'ajaxcat', '/wp-admin/js/cat.js', array( 'wp-lists' ), '20071101' );
    114114            $this->localize( 'ajaxcat', 'catL10n', array(
    115115                'add' => attribute_escape(__('Add')),
Note: See TracChangeset for help on using the changeset viewer.