Home > Archive > PHP Language > March 2006 > General error checking
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
General error checking
|
|
|
| Hi there,
This is more a logic question(s) to get some idea of how you guys/gals do
your error checking.
When creating a form within PHP, naturally I need to do verification to
ensure that the form fields contain relevant data, or are indeed filled out
at all.
Generally people have the user redirected back to the form upon an error,
and bold/coloured text is printed next to the field causing an issue, but
how do most people do it?
Do you check the $_POST variables on submission, and then if they contain an
error/are empty, set an error variable or something else?
How do you do this for specific errors? An example being, say I have a
username field that may only contain alphabetical characters, no numerics,
and be at least 5 characters in length. You would, in theory, need 2-3
checks? One to ensure it doesn't contain numeric characters. One to check
it's at least 5 characters in length, and one to check it's even set at all.
So, in your coding experience, do you set a $nick_error variable or
something, and if it's set, display the error next to the name? What, then,
if there are multiple errors? Say the user enters a 4 character length
username that's alphanumeric, how do you handle that?
Naturally you could do your error checking in a much less direct way,
something like foreach ($_POST as $key -> $value) and then an
if(empty($_POST['val'])) I assume? But this wouldn't be specific enough for
the end user to recognise their own error.
Being a fairly new PHP developer, I realise the importance of end user
stupidity and making your scripts as 'idiot proof' as possible. Any tips,
guidelines or practices you've picked up along the way would be much
appreciated.
Thanks,
Rich.
| |
| Jan Schulze 2006-03-15, 7:55 am |
| Hi,
yes you're right with your ideas.
And if you want to treat all errors at once, just give back an array with
all errors at the end of your check routine.
That array could have the form
[$nick]=>error_code
where error_code are 'defines' or just a string to display.
The check routine could be such a slope if you have just to check if the
values are empty. In other cases you could check each value of interest
individually.
To examine if the array with the check results respectively just the errors
is valid, you could use the following function:
function array_valid($aArray){
if(empty($aArray) || (count($aArray)<1))
return false;
return true;
}
With Regards,
Jan
"Rich" <rflack@gmail.com> schrieb im Newsbeitrag
news:rwLRf.36145$wl.23854@text.news.blueyonder.co.uk...
> Hi there,
>
> This is more a logic question(s) to get some idea of how you guys/gals do
> your error checking.
>
> When creating a form within PHP, naturally I need to do verification to
> ensure that the form fields contain relevant data, or are indeed filled
> out at all.
> Generally people have the user redirected back to the form upon an error,
> and bold/coloured text is printed next to the field causing an issue, but
> how do most people do it?
>
> Do you check the $_POST variables on submission, and then if they contain
> an error/are empty, set an error variable or something else?
> How do you do this for specific errors? An example being, say I have a
> username field that may only contain alphabetical characters, no numerics,
> and be at least 5 characters in length. You would, in theory, need 2-3
> checks? One to ensure it doesn't contain numeric characters. One to
> check it's at least 5 characters in length, and one to check it's even set
> at all. So, in your coding experience, do you set a $nick_error variable
> or something, and if it's set, display the error next to the name? What,
> then, if there are multiple errors? Say the user enters a 4 character
> length username that's alphanumeric, how do you handle that?
>
> Naturally you could do your error checking in a much less direct way,
> something like foreach ($_POST as $key -> $value) and then an
> if(empty($_POST['val'])) I assume? But this wouldn't be specific enough
> for the end user to recognise their own error.
>
> Being a fairly new PHP developer, I realise the importance of end user
> stupidity and making your scripts as 'idiot proof' as possible. Any tips,
> guidelines or practices you've picked up along the way would be much
> appreciated.
>
> Thanks,
> Rich.
>
| |
|
| "Rich" <rflack@gmail.com> wrote in message
news:rwLRf.36145$wl.23854@text.news.blueyonder.co.uk...
> Hi there,
>
> This is more a logic question(s) to get some idea of how you guys/gals do
> your error checking.
>
> When creating a form within PHP, naturally I need to do verification to
> ensure that the form fields contain relevant data, or are indeed filled
> out at all.
> Generally people have the user redirected back to the form upon an error,
> and bold/coloured text is printed next to the field causing an issue, but
> how do most people do it?
>
> Do you check the $_POST variables on submission, and then if they contain
> an error/are empty, set an error variable or something else?
> How do you do this for specific errors? An example being, say I have a
> username field that may only contain alphabetical characters, no numerics,
> and be at least 5 characters in length. You would, in theory, need 2-3
> checks? One to ensure it doesn't contain numeric characters. One to
> check it's at least 5 characters in length, and one to check it's even set
> at all. So, in your coding experience, do you set a $nick_error variable
> or something, and if it's set, display the error next to the name? What,
> then, if there are multiple errors? Say the user enters a 4 character
> length username that's alphanumeric, how do you handle that?
>
> Naturally you could do your error checking in a much less direct way,
> something like foreach ($_POST as $key -> $value) and then an
> if(empty($_POST['val'])) I assume? But this wouldn't be specific enough
> for the end user to recognise their own error.
>
> Being a fairly new PHP developer, I realise the importance of end user
> stupidity and making your scripts as 'idiot proof' as possible. Any tips,
> guidelines or practices you've picked up along the way would be much
> appreciated.
>
> Thanks,
> Rich.
I define an array which has fields for indexes, and each top element is an
array defining how each field should be validated. You can match on regular
expressions, compare the field to another, etc. If an error is found, the
variable in question is entered into an "errors" array in the session
variable. The user is then header("location:")'d back to the form, where
the form is displayed, with erroneous boxes highlighted (I use a template
for this, and simply have lines like this:
<input type="text" name="email" class="input%%%email_error%%%"
value="%%%email%%%">
Where the %%%email_error%%% is set to "Error" if indeed erroneous.
This means checking is ridiculously easy, and still speedy. Want to add
another field? Simply enter the HTML line, then enter it in the array if it
needs checking upon entry. simple :)
dave
| |
| Arnold Shore 2006-03-19, 6:56 pm |
| In interest of response time to the user, I use Javascript for userland form
validation, prior to any submission. In the JS, I'll check the entire set
and complain to the users re each error in a single response.
In fact, my first serious look at an application is to try to submit an
empty form, and see how that's handled.
AS
"Rich" <rflack@gmail.com> wrote in message
news:rwLRf.36145$wl.23854@text.news.blueyonder.co.uk...
> Hi there,
>
> This is more a logic question(s) to get some idea of how you guys/gals do
> your error checking.
>
> When creating a form within PHP, naturally I need to do verification to
> ensure that the form fields contain relevant data, or are indeed filled
> out at all.
> Generally people have the user redirected back to the form upon an error,
> and bold/coloured text is printed next to the field causing an issue, but
> how do most people do it?
>
> Do you check the $_POST variables on submission, and then if they contain
> an error/are empty, set an error variable or something else?
> How do you do this for specific errors? An example being, say I have a
> username field that may only contain alphabetical characters, no numerics,
> and be at least 5 characters in length. You would, in theory, need 2-3
> checks? One to ensure it doesn't contain numeric characters. One to
> check it's at least 5 characters in length, and one to check it's even set
> at all. So, in your coding experience, do you set a $nick_error variable
> or something, and if it's set, display the error next to the name? What,
> then, if there are multiple errors? Say the user enters a 4 character
> length username that's alphanumeric, how do you handle that?
>
> Naturally you could do your error checking in a much less direct way,
> something like foreach ($_POST as $key -> $value) and then an
> if(empty($_POST['val'])) I assume? But this wouldn't be specific enough
> for the end user to recognise their own error.
>
> Being a fairly new PHP developer, I realise the importance of end user
> stupidity and making your scripts as 'idiot proof' as possible. Any tips,
> guidelines or practices you've picked up along the way would be much
> appreciated.
>
> Thanks,
> Rich.
>
|
|
|
|
|