tags:

views:

214

answers:

4

I have the following code to ensure that the form does not have any unfilled textboxes and selected boxes

$required_fields = array ('menu_name','visible','position');
foreach($required_fields as $fieldname)
{
    if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) )
    {
        $errors [] = $fieldname;
    }
}

Where menu_name, visible and position are variables that are received through the post method

Am having a problem when the value of 'visible' is zero (0) as it creates an entry into the error array. How can I avoid this as I presume when the value is 0 then it is considered not set by the isset function.

+8  A: 

From PHP's manual:

empty() returns FALSE if var has a non-empty and non-zero value.

Do something like this:

if ( !IsSet ( $_POST['field'] ) || Trim ( $_POST['field'] ) == '' )

this will ensure that the field is set and that it does not contain a empty string

In essence: it is the empty() that is causing your problems not IsSet()

Jan Hančič
I don't like camelcase in builtin php functions, but its a taste. :)
AlberT
It's not a taste. In theory it's wrong, since the function isn't called that.
Tor Valamo
It makes the code more readable to me like this.
Jan Hančič
Hope PHP will begin case sensitive :P
AlberT
A: 

Can't you just add another line with something like:

if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) )
{
    if ($fieldname != 'visible' || $_POST[$fieldname] != 0)
    { 
        $errors [] = $fieldname;
    }
} 
mjdth
You will get a notice if `!isset($_POST[$fieldname])` is true and `$fieldname != 'visible'` is false as you then try to access `$_POST[$fieldname]`.
Gumbo
A: 

If you want to assure an array key is present you can use arra_key_exists() instead of empty()

The check will become a concatenation of is_array() and array_key_exists(), being paranoid of course

AlberT
I thought about that too, but `isset()` also returns `false` if the key is present but contains a `null` value ( where `array_key_exists()` returns `true`). So in this use case I really think `isset()` fits better.
Felix Kling
+1  A: 

Since user data is sloppy, I use a custom function that treats empty spaces as non data. It sounds like this will do exactly what you want. This function will consider "0" to be valid (aka non-empty) data.

function isNullOrEmpty( $arg )
{
    if ( !is_array( $arg ) )
    {
        $arg = array( $arg );
    }

    foreach ( $arg as $key => $value )
    {
        $value = trim($value);
        if( $value == "" || $value == null )
        {
            return true;
        }
    }
    return false;
}

Please note it supports arrays too but requires that each value in the array contains data, which can be useful as you can just do something like this:

$required = array( $_POST['name'],  $_POST['age'], $_POST['weight'] );
if ( isNullOrEmpty($required) )
{
  // required data is missing
}

PS: keep in mind this function will fire off PHP warnings if the value isn't set and there's no easy way around that, but you should NOT have warnings enabled in production anyways.

TravisO
You could improve your code if you check wether the parameter is an array or not (I know you do this already). If it is not an array, just do `$arg = array($arg)` then you can use the foreach loop and you have to write the `trim` and comparison part only *once* which is better to maintain. Imagine you have other criteria to check, you would have to add them twice now (once for arrays and once for non-array values)(which is not DRY).
Felix Kling
@Felix, good point, it's now simplified.
TravisO
This is what I used in reference to your answerif ($_POST[$fieldname] == '' || !isset($_POST[$fieldname]))
Gatura