tags:

views:

29

answers:

3

Hello

In my project my project manager says me to make 'state' the field required if the country is united states and make the city field optional if the country is not US.

How to accomplish it? Any help?

A: 

I don't know anything about how you're validating form data so i can only provide you with basic logic. Youll have to adapt this....

if ( $_POST['country'] == 'US' && !$_POST['city'] ) {
    $error = "city is required";
}

But seeing as how simple this is it makes me feel like maybe your question is missing something?

Galen
A: 

In the PHP script that is called when the form is submitted, you can check to see if the country is 'United States' and if it is, check to see if the state is selected. If it's not, send the user back to the page with the form with an error message of the problem.

Catfish
A: 

So it is basically a rule based validation. The rule is simple in your case. Such rules can get even more complicated. There are two options.

First, just do a hard coded validation. Means you do check for the value of the 'state' field and then check for the field 'state' in the run time. This means, in case the rule is changed, you change the code.

Second, you put these rules outside the code (config files, database etc) and dynamically load the rules and validate accordingly. This makes the initial design complex, but the maintenance is easy.

Kangkan