FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display Validation errors next to each field
#1
I searched the forums on this one and couldn't find a good answer.

I have been testing a new form with .php and using the PHP validation rules and so far, so good. I even got all of my fields setup to refill when there is an error.

But now it's about how to best display the error messages. I'm sure there is some simple tweak to the PHP script from your tutorial to do this, but I'd like to have each field's error message be displayed next to the corresponding field. For example, rather than having one list of errors somewhere in the page, I'd like the error to display next to the field it corresponds to.

Is there a way to tweak the code to this? I imagine I'd need some kind of if-else statement for every single field in my form, but I'd be ok with that.

PS - I have somewhat limited knowledge of PHP, but I think I can follow terminology.

Thanks in advance!
Brandon
I think if I could tweak this code so that it only called one specific error, then I could paste the tweaked code next to each field and change the code to reflect THAT field, right?

Code:
<?php
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
  echo "<div class='error'>Please fix the following errors:\n<ul>";
  foreach ($errors as $error)
    echo "<li>$error</li>\n";

  echo "</ul></div>";
}
?>
Not a FT employee, just a FT user lending a hand. If I've been helpful, please rate.

[Image: giraffe_long_85.gif]
Reply
#2
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.,

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
Reply
#3
My PHP just isn't good enough to get it, I guess. I changed a rule to reflect the colon:

'required, email, email: An email field is mandatory'.

Then I added the following to the end of the list of rules:

Code:
$raw_errors = validate_fields($_POST, $rules);
$error_hash = process_raw_errors($raw_errors);

I don't, however, understand adding a generic function to find and output a particular error message. I added the bit of code from your example, but it's obviously an undefined function, so it returned an error. It makes sense, but I don't understand PHP enough to know how to create the function (or define it).
Not a FT employee, just a FT user lending a hand. If I've been helpful, please rate.

[Image: giraffe_long_85.gif]
Reply
#4
I'm afraid the only way I can see *is* to do some PHP coding along the lines I mentioned. I myself might get down to it sometime soon - if I ever do, I will share it here.
Reply
#5
We have to show or display the errors that are placed in our websites and make our system stuck and hang. I must have to use this best essays to highlight and point out the errors.
Reply
#6
 Form Tools is an open source web form management application. Form Tools was initially released in 2005 and has since been downloaded ... by epicfollowerscanada
Reply
#7
formtools – a collection of assorted utilities that are useful for specific form use cases. This code is now distributed separately from Django, for easier maintenance ... by buy instagram canada
Reply
#8
Doji Candle refers to a candlestick pattern that helps to provide vital information to traders and investors regarding a particular security.

buy followers australia
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)