forms + validation
am 24.09.2007 16:23:37 von Sergei Riaguzov
Hi. I haven't written in PHP too much so I think I should ask how my
problem can be solved "the right way".
I'm having some forms (actually this is not my code, and actually I
haven't got it yet but I see the result). This forms should be filled by
user and when filled and when user clickes on the Next button the next
page or the same page with filled results and some yellow/red/whatever
signs that something was not filled is shown.
Now it doesn't do anything of that.
The first thing (showing previously typed values) is simple: I should
just put every field a value from $_POST, like
["field"]>.
Now the second one would also be simple, smth like:
function showError(field) {
if (field == NULL)
print "some html element with yellow/red/whatever signs";
}
And now just add this showError() to every field.
But that is not enough because this is not a validation. And if I am to
add validation for every field I have to write as many showError()
functions as there are fields or write a huge switch statement inside
showError().
And if there are some checkboxes, radiobuttons and other select lists
then why should I write all this validation by hand? Maybe there is a way
to specify this validation in more simple way? Like saying that if $_POST
contains information about selection in some list then it should be
selected, and elesewhere it should validate automatically about selection
indices in list and so on.
Is it possible to do such things? Or maybe I should write my showError()
in Javascript instead? Or maybe I am wrong about the solution. What is
the "right way" of doing such things?
Re: forms + validation
am 24.09.2007 17:00:57 von panda31
On 24 sep, 16:23, Sergei Riaguzov wrote:
> Hi. I haven't written in PHP too much so I think I should ask how my
> problem can be solved "the right way".
>
> I'm having some forms (actually this is not my code, and actually I
> haven't got it yet but I see the result). This forms should be filled by
> user and when filled and when user clickes on the Next button the next
> page or the same page with filled results and some yellow/red/whatever
> signs that something was not filled is shown.
>
> Now it doesn't do anything of that.
>
> The first thing (showing previously typed values) is simple: I should
> just put every field a value from $_POST, like
> ["field"]>.
>
> Now the second one would also be simple, smth like:
>
> function showError(field) {
> if (field == NULL)
> print "some html element with yellow/red/whatever signs";
>
> }
>
> And now just add this showError() to every field.
>
> But that is not enough because this is not a validation. And if I am to
> add validation for every field I have to write as many showError()
> functions as there are fields or write a huge switch statement inside
> showError().
>
> And if there are some checkboxes, radiobuttons and other select lists
> then why should I write all this validation by hand? Maybe there is a way
> to specify this validation in more simple way? Like saying that if $_POST
> contains information about selection in some list then it should be
> selected, and elesewhere it should validate automatically about selection
> indices in list and so on.
>
> Is it possible to do such things? Or maybe I should write my showError()
> in Javascript instead? Or maybe I am wrong about the solution. What is
> the "right way" of doing such things?
Hi,
You can also implement an array with critical fields id.
For each critical field, you do your check. In your array, in fact an
associative one with couples key => value, you put key = id and value
= {true or false} according your validation criteria.
Then, when this array is completely filled, for eack id with a false
value, you write "yellow/red/whatever signs".
$critical_items