PHP Classes

just use validation

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  PHP Forms Class with HTML Generator and JavaScript Validation package blog  >  How to Show Google Ma...  >  All threads  >  just use validation  >  (Un) Subscribe thread alerts  
Subject:just use validation
Summary:How do I just use your clase to validate my form?
Messages:8
Author:gloria bai
Date:2008-05-15 02:43:42
Update:2008-05-17 06:02:27
 

  1. just use validation   Reply   Report abuse  
Picture of gloria bai gloria bai - 2008-05-15 02:43:42
Hi,

I have a form already, here is the form:

recipeyeh.com/upload.php

I want to validate it using form class. Since I am new to PHP. I have no idea about how to use this form class. What file do I need to install on my website? Do I need to edit .htaccess file? How do I set the validation parameters such as a regular expression for recipe title (alpha, numeric, " ", - are allowed)?

Thanks.

  2. Re: just use validation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-05-15 05:25:44 - In reply to message 1 from gloria bai
If you have your form ready it would not be hard to integrate it to display it with the class.

If you just want to validate the submitted form, you just need to point your form submission URL to a PHP script that calls the class AddInput function to define the form inputs and validate rules. Then you call the Validate function to check if it passes the validation.

Anyway, if you can integrate your form with the class, the benefits will be greater for you. One thing is that the class can automatically generate Javascript to validate your form even before submit it to your server. This makes your form much more usuable because the user does not have to wait for the form to be validated on the server. If Javascript is disabled, the form will still be validated on the server side.

Another advantage is that you can display, validate and process your form in a single page script. So, if your form is validated on the server and is invalid, the same script can be used to show the form again.

Take a look at the test_form.php script for an example of how to do that.

  3. Re: just use validation   Reply   Report abuse  
Picture of gloria bai gloria bai - 2008-05-16 05:06:07 - In reply to message 2 from Manuel Lemos
Thank you so much. I will have a look at those examples and try to figure out how to use this class. Thanks again.

Gloria

  4. Re: just use validation   Reply   Report abuse  
Picture of gloria bai gloria bai - 2008-05-16 23:31:41 - In reply to message 2 from Manuel Lemos
hi, Manuel

I wrote a validation file named as: validate_recipe.php. Here is part of the code:

<?php

require("forms.php"); // Include form class code.
$myform = new form_class; // Create a form object.

// Define the name of the form to be used.
$myform->NAME="uploadRecipe";

// Specify a debug output function you really want to output any programming errors.
$myform->debug="trigger_error";

//Output all validation errors at once.
$myform->ShowAllErrors=1;

// CSS class to apply to all invalid inputs.
// Set to a non-empty string to specify the invalid input CSS class
$myform->InvalidCLASS='invalid';

// Text to prepend and append to the validation error messages.
$myform->ErrorMessagePrefix="- ";
$myform->ErrorMessageSuffix="";



// Define the form field properties even if they may not be displayed.
//-------------------------------------------------------------------
$myform->AddInput(array(
"TYPE"=>"text",
"NAME"=>"recipeTitle",
"ValidateAsNotEmpty"=>1,
"ValidateMinimumLehgth"=>5,
"MAXLENGTH"=>100,
"ValidateRegularExpression"=>array( "^([a-zA-Z])[\w -]+$" ),
"ValidationErrorMessage"=>"Invalid name. Please enter a valid recipe title.",
"ACCESSKEY"=>"R" ));

$myform->AddInput(array(
"TYPE"=>"select",
"NAME"=>"cuisine",
"SIZE"=>1,
"VALUE"=>"",
"ValidationErrorMessage"=>"Please select the type of cuisine.",
"ValidateAsDifferentFromText"=>"",
"ACCESSKEY"=>"C" ));


......


$myform->AddInput(array(
"TYPE"=>"submit",
"NAME"=>"SubmitForm",
"VALUE"=>"Submit",
"ACCESSKEY"=>"u"
));


$myform->AddInput(array(
"TYPE"=>"hidden",
"NAME"=>"doit",
"VALUE"=>1 ));

$myform->LoadInputValues($myform->WasSubmitted("doit"));

// Empty the array that will list the values with invalid field after validation.
$verify=array();

if($myform->WasSubmitted("doit")){
if(($error_message=$myform->Validate($verify))=="") {
$doit=1;
}else{
$doit=0;
$error_message=nl2br(HtmlSpecialChars($error_message));
}
}else{
$error_message="";
$doit=0;
}

if($doit) { $myform->ReadOnly=1; }

if(!$doit) {
if(strlen($error_message)) {
Reset($verify);
$focus=Key($verify);
}else {
$focus='recipeTitle';
}

?>


and in the upload.php file, I added:
include("/phpLibrary/validate_recipe.php");
Then after I submitted the form with some data, nothing was validated. I know I am wrong, but I cannot figure out.

Another question is: Is it a must to specify an ACCESSKEY?

Please help. Thanks.



  5. Re: just use validation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-05-17 01:37:51 - In reply to message 4 from gloria bai
I wonder what you by mean nothing is validated.

In your script you check if the form was submitted testing an input named "doit" . Do you have that input in your form?

The ACCESSKEY attribute is to tell the browser which is the key that may be used as shortcut to activate the input. So it is only useful when you are presenting the form.

  6. Re: just use validation   Reply   Report abuse  
Picture of gloria bai gloria bai - 2008-05-17 05:23:53 - In reply to message 5 from Manuel Lemos
yes, I put a hidden input field in my form in my upload.php file.
the form part is:

<!-- ***************** table 3.1 (form )********************* -->
<table bgcolor="#FFF2BF" width="759" border="0" cellspacing="0" cellpadding="1px">
<form name ="uploadRecipe" enctype="multipart/form-data"
method ="POST" action ="http://recipeyeh.com/upload.php" >
<tr><th colspan="3" height="60" align="center" valign="middle" >Post Your Recipe</th> </tr>

<tr><td rowspan="20" width="10"></td>
<td class="td1" > Recipe Title :<span class="arial22boldred"> *</span></td>
<td width="563" height="30" align="left" valign="top">
<input name="recipeTitle" type="text" maxlength ="100" class="textbox260"></td>
</tr>

......


<tr><td class="td1">Your Email:<span class="arial22boldred">*</span></td>
<td height="30" align="left" valign="top">
<input name="youremail" type="text" class="textbox260" maxlength='50' ></td>
</tr>

<tr><td></td><td><input name="doit" type="hidden" value="1" ></td> </tr>

<tr><td colspan="2" height="40" align="center">
<input type="reset" value="Reset" name =" ResettForm" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Submit" name ="SubmitForm" > </td> </tr>

<tr> <td class="td1"><span class="arial22boldred">*</span> <span class="arial12regular">Mandatory</span></td> <td align="left" valign="top">&nbsp;</td> </tr>

</form>
</table>
<!-- ************ end of table 3.1 (form )************ -->

<?php
include("/phpLibrary/validate_recipe.php");
?>


when I submit an empty form, it does not give me error message. Something wrong? I appreciate your help. Thanks again.



  7. Re: just use validation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-05-17 05:38:56 - In reply to message 6 from gloria bai
It seems you have few errors in the form input definitions. For instance the recipeTitle option "ValidateMinimumLehgth" is misspelled and the cuisine input does not have an OPTIONS array.

If the inputs fail to be added to the form they will not be validated.

In the case of your script, when an error occurs, the trigger_error function is called. If you have php.ini setting to display_errors off, you will not see the errors output on your page. It is a good idea to have that enabled while you are debugging your script.

  8. Re: just use validation   Reply   Report abuse  
Picture of gloria bai gloria bai - 2008-05-17 06:02:27 - In reply to message 7 from Manuel Lemos
Sorry for typing errors. So I have to put the options in the OPTIONS array. Let me try.

Many thanks for quick response.