I've been wanting to do this myself, but had given it a lower priority.
Here is a way (hack) that I can think of. Note that this is only an outline.
1. In your error messages that you pass to the rules, prepend the field name followed by a colon. For example, if you have a field named 'email', your rule would be 'required, email, email: An email field is mandatory'.
2. Collect your error messages into $raw_errors (say). This will contain the field names. E.g.,
3. Process the raw errors to an error_hash. This will have a lookup of field names (which you can retrieve from the error messages) to the corresponding messages that are stripped off the field names. E.g.,
4. Near your fields, you can dump the errors through a generic function. E.g.,
This should work. You can also choose to skip the process_raw_errors(.) and instead iterate through the raw errors directly in your display_errors function.
Just looked at the code. Here is a possibility, but maybe Ben can confirm: if you're willing to edit validation.php, look at the lines that look like the following in validate_fields(.):
and replace them with
where add_error will add the error to the errors hash such that it is a lookup between field names and their error arrays.
Regards,
Arun
Here is a way (hack) that I can think of. Note that this is only an outline.
1. In your error messages that you pass to the rules, prepend the field name followed by a colon. For example, if you have a field named 'email', your rule would be 'required, email, email: An email field is mandatory'.
2. Collect your error messages into $raw_errors (say). This will contain the field names. E.g.,
Code:
$raw_errors = validate_fields($_POST, $rules);
3. Process the raw errors to an error_hash. This will have a lookup of field names (which you can retrieve from the error messages) to the corresponding messages that are stripped off the field names. E.g.,
Code:
$error_hash = process_raw_errors($raw_errors);
4. Near your fields, you can dump the errors through a generic function. E.g.,
Code:
<?php
display_errors($error_hash, "email");
?>
<input type="text" name="email" />
This should work. You can also choose to skip the process_raw_errors(.) and instead iterate through the raw errors directly in your display_errors function.
Just looked at the code. Here is a possibility, but maybe Ben can confirm: if you're willing to edit validation.php, look at the lines that look like the following in validate_fields(.):
Code:
if (!isset($fields[$field_name]) || $fields[$field_name] == "")
$errors[] = $error_message;
and replace them with
Code:
if (!isset($fields[$field_name]) || $fields[$field_name] == "")
add_error($errors, $field_name, $error_message);
where add_error will add the error to the errors hash such that it is a lookup between field names and their error arrays.
Regards,
Arun