So i've been reading about using jQuery in conjunction with PHP to give a much more interactive experience on a site. I've never really played with javascript (just bits and pieces here and there) so i'm pretty new w/that aspect.

Anyway, I have an array of "form rules" that are processed serverside given that javascript is disabled on the users browser.

<?php

#
# Setting up Form Rules
#

$form_rules = array(
    'user_login' => array(
        'ident' => "Login Name",
        'required' => true,
        'minlength' => 4
    ),
    'user_first' => array(
        'ident' => "First Name",
        'required' => true,
        'minlength' => 2
    ),
    'user_last' => array(
        'ident' => "Last Name",
        'required' => true,
        'minlength' => 2
    ),
    'user_pass' => array(
        'ident' => "Password",
        'required' => true,
        'minlength' => 6
    ),
    'user_pass_ii' => array(
        'ident' => "Verify Password",
        'required' => true,
        'minlength' => 6,
        'equalTo' => '#user_pass'
    ),
    'user_email' => array(
        'ident' => "Email",
        'required' => true,
        'email' => true
    ),
);

These same rules are being processed by jQuery client side if the user has javascript enabled;

<script>
                $(document).ready(function() {
                    $('#portal_reg').validate({
                        'rules': <?php echo json_encode($form_rules); ?>
                    });
                });
            </script>

That being said - The server side code is what i'm having minor issues with

I have a custom function to process rules and apply them; just for clarity, i'll post the rule processor

require './admin/users/registration.rules.php';

function get_errors($form_data, $rules) {
    // returns an array of errors
    $errors = array();
    foreach ($form_data as $name => $value) {
        if (!isset($rules[$name])) {
            continue;
        }
        $hname = htmlspecialchars($name); // form fields
        $rule = $rules[$name];
        if (isset($rule['required']) && $rule['required'] && !$value) {
            $errors[] = '<strong> ' . $rule['ident'] . ' </strong> is required.';
        }
        if (isset($rule['equalTo'])) {
            $ename = substr($rule['equalTo'], 1);
            if (!isset($form_data[$ename]) || $value != $form_data[$ename]) {
                $errors[] = $rule['ident'] . ' must be identical to ' . htmlspecialchars($ename);
            }
        }
        if (isset($rule['minlength']) && strlen($value) < $rule['minlength']) {
            $errors[] = $rule['ident'] . ' should be at least ' . $rule['minlength'] . ' characters in length.';
        }
        if (isset($rule['email']) && $rule['email'] && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
            $errors[] = $rule['ident'] . ' must be an email address.';
        }
        $rules[$name]['found'] = true;
    }
    foreach ($rules as $name => $values) {
        if (!isset($values['found']) && isset($values['required']) && $values['required']) {
            $errors[] = '<strong> ' . $rule['ident'] . ' is required.';
        }
    }
    return $errors;
}

$errors = get_errors($_POST, $form_rules);
if (!count($errors)) {
    // save the data, or post it, or whatever
    echo 'success';
} else {
    // errors found
    echo '<h3 class=\"portlet-title\">Corrections Necessary</h3><ul><li>';
    echo join('</li><li>', $errors);
    echo '</ul><p>Please go back and make necessary corrections.</p>';
}

Under the form_rules - user_pass_ii section I have

'equalTo' => '#user_pass'

basically saying that user_pass needs to be equal to user_pass_ii ( input type=text name=user_pass, etc)

This works really well. However in the output to the user shows
Verify Password must be identical to user_pass

Where i want it to show "Password" which is the $rules['ident'] name for user_pass

I'm unsure how to go about doing this w/o breaking the client side validation by changing 'equalTo'=>'#user_pass'

If it makes a difference i'm using the jquery plugin Form Validator from http://bassistance.de/jquery-plugins/jquery-plugin-validation/

    DeadlySin3;11041807 wrote:

    Where i want it to show "Password" which is the $rules['ident'] name for user_pass

    What have you tried? You already have 'user_pass', which is the index into the $rules array you're after, so it sounds like you simply need to use the 'ident' index of that subarray.

      Yeah it would seem like that would be an easy answer - I've tried most anything I can think of lol - Problem being is i'm doing this: $ename = substr($rule['equalTo'], 1); -- where the 1 removes the # symbol from user_pass in the rule-set. This needs to be this way due to how the rules work w/the jquery validation plugin - it must be able to match the id of user_pass to the id of user_pass_ii.

      <p><label>
      <input class="textinput" type="password" id="user_pass" tabindex="3"><span>Password</span>
      </p></label>
      <p><label>
      <input class="textinput" type="password" id="user_pass_ii" tabindex="4"><span>Verify Password</span>
      </p></label>
      <!-- The above are examples of the form set up while the below is part of the rule-set
      'user_pass_ii' => array(
              'ident' => "Verify Password",
              'required' => true,
              'minlength' => 6,
              'equalTo' => '#user_pass'
          ),
      -->
      

      The way the validation code is written - is it pulls the id = "user_pass" field from the input type and says that user_pass_ii must be identical - so - w/that tid bit - just printing $rules['ident'] here echos "erify Password" -- note the missing "v" lol. In which case the actual user would see "Verify Password must equal erify Password"

      I've put every grain of effort into this that I care to and I just can not conceive a working combination. Regardless, it's all for not anyway. the custom "ident" rule I added to the array isn't built into the validation plugin, it's just something I thought to throw in there - serverside it works wonders but client side it breaks the rule set and the form is no longer validated unless i comment out / remove the "ident" line of the form rules array and replace $rules['ident'] with $hname in the serverside validation code. I would customize the validation script but again i'm a newb when it comes to javascript - and i'd rather focus my energies on server-side in my comfort zone (for now).

      I could simply add a custom serverside rule-set but the whole idea is to square the server & client validation away with one set of rules for simplicity and to make it that much more re-usable. Not that It really matters - i'm just doin' this for myself to prove that I can. I suppose I can live with just printing the forms field name as opposed to the custom ident handler. Thanks anyway!

        Write a Reply...