Avoiding many conditionals

I have a form with various input fields but I want to focus this topic on just 3 of them.
Name,lastname and company name. http://hastebin.com/awaxidibit.xml

The user must fill either name,lastname OR company name…not both.

I am a little stuck with the conditionals for checking this.

Because there must be a check also made that the fields are indeed filled…that complicates the logic plus a check that the user has NOT filled all of them.

It might sound easy to you but I am stuck with it and I would need some extra help…

Another approach would be to have just one of the fields and a radio to switch between last name or company.

use xor (exclusive or, either/or) not or (inclusive or). cf. http://php.net/manual/en/language.operators.logical.php

Ah, I see - you mean they either fill first and last name, or fill company name, but not both - I read it first as being always first name, then either last name or company name. Many ways, including using Ajax to disable the ‘company name’ field as soon as they type anything in first name or last name, and vice versa.

1 Like

Here is a function that might get you started:

function check_names($name,$lastName,$companyName)
    // Clean things a tiny bit, should probably be done when extracting from $POST
    $name = trim($name);
    $lastName = trim($lastName);
    $companyName = trim($companyName);

    // Start with company name first
    if ($companyName) {
        if ($name || $lastName) {
            return false;
        return true;
    // Need both name and lastname
    if ($name && $lastName) {
        return true;
    return false;
$name = '';
$lastName = null;
$companyName = 'Trump Enterprise';
if (check_names($name,$lastName,$companyName)) echo "Names are okay\n";
else echo "Names NOT okay\n";

$name = 'Donald';
$lastName = 'Trump';
$companyName = 'Trump Enterprise';
if (check_names($name,$lastName,$companyName)) echo "Names are okay\n";
else echo "Names NOT okay\n";

Of course it will need to be refined to suit your particular use case.

I’m not saying “don’t have post submission validation”, but I think in this case it would be good for UX to have in-form validation for this to stop users making the error in the first place. Having such conditional fields could be confusing for some.

This is a basic example using the “checkbox/radio hack” to make it easier for the user to get it right.
You will still need the post submission validation though.

Using the above in-form method I would probably do the post submission validation differently.
First check the result of who.
Then according to that, get the data only from the relevant input and simply disregard the other one.
That will be easier to process and easier for the user.

I am assuming when you are mentioning validation you are referring to sanitization for example.
Are you referring to the function made by ahundiak?

make some clarifications please.

I’m referring to validation, as that is what the topic appears to be about, making sure the data you get is the data that you want.
Sanitisation is a different, but related subject which may well be relevant to this script too.
Either way, my html example is simply the create better UX in decreasing the likelihood of a user mistake which would force them to have to return to the form to correct it.
The thing to be mindful of is that any client-side coding is always prone to failure through user error or malicious behaviour, so should always be backed up by server-side validation.

That could be used, but with my method it could be simpler.
The radio forces the user to explicitly state whether they are giving a person’s name or a company name.
The css will show/hide the appropriate inputs according to this. This will lessen the likelihood of them filling in both.
However, it is still possible that they could do so. But, that’s not a problem because they told you which one they are giving you via the radio toggle.
So rather than flagging it as an error and sending them back to the form again, you simply disregard the unwanted data and process only what you want.
Which means it makes your validation simpler too.

if($_POST['who'] == 'name') {
    // Process the name fields and forget about whatever is in companyname
elseif($_POST['who'] == 'comp') {
    // Process the company name field and forget about the first and last name
else { // This is an error! }

all these are ideas are good,nonetheless they will cause me to do considerable refactoring.
I am trying to think what other options do I have.

Validation should not be optional.

Another way, which is not as nice, is to just decide on a priority and if they fill in too many fields, just ignore one set or the other. If you get a company name, don’t do anything with the first name or last name, even if they filled it in. So your logic would be to check if they gave a company name, if they didn’t then look for a first name and last name, if there’s still nothing, throw an error.

Of course, that’s not a nice way to do it and might lead to questions of “why ask for my first and last name if you’re not going to do anything with it” in the future.

Ι decided to retain the old code and not make any changes after all as that would require extensive refactoring.
I found a solution after all with the code as it is now.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.