php框架 swoop_PHP Form Validation

问题

This question will undoubtedly be difficult to answer and ask in a way that makes sense but I'll try my best:

I have a form which uses PHP to display certain sections of the form such as:

and

<?php if ($_SESSION['Num_Enrs'] > 6) { display only form information for 7 total members enrollment } ?>

In each form piece, unique information is collected about each enrollee but the basic criteria for each enrollee is the same, i.e. All enrollee's must use have a value in the FirstName field. Each field is named according to the enrollee number, i.e. Num1FirstName; Num2FirstName.

I have a PHP validation script which is absolutely fantastic and am not looking to change it but the issue I am running into is duplication of the script in order to validate ALL fields in one swoop.

On submission, all POSTED items are run through my validation script and based on the rules set return an error if they do not equal true.

Sample code:

if (isset($_POST['submit']))

{

// import the validation library

require("validation.php");

$rules = array(); // stores the validation rules

//All Enrollee Rules

$rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

The script above does the following, $rules[] ="REQUIREMENT,fieldname,error message"

where requirement gives criteria (in this case, simply that a value is passed), fieldname is the name of the field being validated, and error message returns the error used.

My Goal is to use the same formula above and have $rules[] run through ALL firstnames and return the error posted ONLY if they exist (i.e. dont check for member #7's first name if it doesnt exist on the screen).

If I simply put a comma between the 'fieldnames' this only checks for the first, then second, and so on so this wont work.

Any ideas?

回答1:

If I understand what you're trying to do correctly:

1. Define types

First, you need to define what type of field requires what kinds of rules, e.g.:

$type['FirstName']['requirement'] = 'required';

$type['FirstName']['error_message'] = 'The First Name field is required.';

// just random example:

$type['MobilePhone']['requirement'] = 'optional';

$type['MobilePhone']['error_message'] = 'Only enter digits.';

2. Go through each posted value

Second, check for each posted value what type of field it is. Now a lot of stuff might be included in the $_POST array, and you only need to check certain fields. It might make it a lot easier to use names like checkthis;1234;FirstName for your input fields.

foreach ($_POST as $key => $value) {

// split the key, so you know what's what:

$data = explode(';',$key);

// now $data[0] tells you what kind of field it is

// $data[1] is the ID of the enrollee (your Num1, Num2)

// $data[2] is the type of field

// only do checks for posted fields that start with "checkthis"

if ($data[0]=='checkthis') {

// now you can fill the $rule array:

$rule[] = $type[$data[2]]['requirement'] . ',' . $key . ',' . $type[$data[2]]['error_message'];

}

}

This way it doesn't matter what you include in your form. As long as you've defined the type of field, a rule will be added to the rule array if it's included in the $_POST values, and your validation script will do the rest.

Edit

If you structure your input fields like this:

etc..

..and you submit the form, the $_POST array will e.g. look like this:

//print_r($_POST) gives:

Array

(

[F1FirstName] => John

[F2FirstName] => Jane

)

..the easiest thing to do is to loop through each of those with foreach:

foreach ($_POST as $key => $value) {

// first $key will be "F1FirstName"

// first $value will be "John"

// second $key will be "F2FirstName"

// second $value will be "Jane"

// etc

}

Now, in that $_POST will also be other types of fields, like e.g. F1MobilePhone or whatever, that require different validations and different messages. So for each of those fields, you need to find out what type it is, to determine what kind of message to enter into your validation function. One option would be (this goes within the foreach statement):

if (strstr($key,'FirstName')) {

// add a validation rule for the FirstName type

}

if (strstr($key,'Somethingelse')) {

// etc

}

I don't know how your validation script works, but I'm guessing you're doing something like this:

// import the validation library

require("validation.php");

$rules = array(); // stores the validation rules

// All Enrollee Rules

$rules[] = "required,Num1FirstName,The First Name field is required.";

// Call on the validation function

$result = validate($rules);

Then you'll probably receive back whether, in this case, "Num1FirstName" was valid or not. Since you use an array $rules, the validator can probably handle multiple lines at once. Simply add more values to the array, but don't add another variable. If you do this:

$rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

..the validator will think that the "Num2FirstName" is the error message, since that's the 3rd value you enter, and it won't know what to do with the actual message which is now 4th value. Try this instead:

$rules[] = "required,Num1FirstName,The First Name field is required.";

$rules[] = "required,Num2FirstName,The First Name field is required.";

So bringing that all together gives you this:

foreach ($_POST as $key => $value) {

// first $key will be "F1FirstName"

// first $value will be "John"

// second $key will be "F2FirstName"

// second $value will be "Jane"

if (strstr($key,'FirstName')) {

$rules[] = "required,$key,The First Name field is required.";

}

if (strstr($key,'Somethingelse')) {

$rules[] = "required,$key,Some other message for this type of field.";

}

}

// call on your validate function and feed it the $rules array you've created

Adding JavaScript to (pre-)validate is a whole different story. In the end you still need to validate it in PHP because, like you said, the JS check can easily be bypassed. So I guess fixing this first might be best.

来源:https://stackoverflow.com/questions/2821316/php-form-validation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值