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/