-1

I have some JSON parameters, that I want to validate if the value are empty with an IF statement. I have written an IF statement in a function to check, but the IF statement only checks for the parameter, I want to check for the values if they are empty. Please, how do I go about this. my code

   //api.php

    $payment_type = $this->validateParameter('payment_type', $this->param['payment_type'], STRING, true);

$date_of_delivery = $this->validateParameter('date_of_delivery', $this->param['date_of_delivery'], STRING, true);

 $your_generated_waybill_number = $this->validateParameter('your_generated_waybill_number', $this->param['your_generated_waybill_number'], STRING, true);


   "payment_type" - is an example of parameter ||  "1" - is an example of value



     {
  "name":"create_insert_new_delivery",
  "param":{
       "payment_type":"1",
       "date_of_delivery":"", //E.g here want to check if the value is empty
       "your_generated_waybill_number":"39ei40909444567avaab",
       }
    }





  //rest.php
   public function validateParameter($fieldName, $value, $required = true){

    if($required == true && empty($value) == true){
        $this->throwError(EMPTY_PARAMETER, $fieldName . " parameter is missing"); //HERE check if the parameter is missing and fires error, but I also want to include value check
    } else if ($required == true && empty($fieldName) == true){
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " value is required");
    } else {

    } //check when parameter is present but value is empty and leave if it is not required

  }
Shasha
  • 439
  • 8
  • 26
  • Did you see, your method is 3 args while you're calling it with 4 ? And did you see you assign their calls in variables while the method have no return ? – WizardNx Jun 22 '18 at 18:37

4 Answers4

3

With empty() function you check for the value

$str = '';
// or 
$str = null;
// or 
$str = false;
var_dump(empty($str)) // output => true

To check if a variable is defined you can use isset

http://php.net/manual/ro/function.isset.php

Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
3

testing your variable should be done by using the identity operator.

if (!$var){}

or

if  ($var === null){} //checks by type

or

if (empty($field)){}
bitsNbytes
  • 64
  • 6
2

You may make your function a little clearer since the tests you are doing will already return true or false. There is no need to compare them to true.

if ($required AND empty($value)) {
        $this->throwError(EMPTY_PARAMETER, $fieldName . " parameter is missing"); //HERE check if the parameter is missing and fires error, but I also want to include value check
    } elseif ($required AND empty($fieldName)) {
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " is required");
    } elseif (!empty($fieldName) AND empty($value)) {
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " value is required");
    }
}
Dave
  • 5,108
  • 16
  • 30
  • 40
2

You're main problem is that you're passing the value (grabbed from the array) to the method and checking that. Because at that point, if the array key is not set, a warning is triggered and the value is coerced to null.

empty(): Will return true when the variable is not set OR if it evaluates to some falsey value (e.g. false, null, [], "", etc)

isset(): Will return true ONLY if the value is not set.

So:

$a = ['one' => false];
empty($a['one']); //true
isset($a['one']); //true
isset($a['two']); //false

So you could edit you validation function like so:

public function validateParameter($fieldName, &$inputArray, $required = true){

    if($required){
        if(!isset($inputArray[$fieldName])){
            $this->throwError(EMPTY_PARAMETER, $fieldName . " parameter is missing");
        } else if (empty($inputArray[$fieldName])){
            $this->throwError(API_PARAM_REQUIRED, $fieldName . " value is required");
        }
    } else {
        if(!isset($inputArray[$fieldName])){
            return null;
        }
    }

    return $inputArray[$fieldName];
  }

Note: since you are checking a class attribute $this->param. You may avoid passing the array to the method and just reference $this->param in the function, unless it should be more reusable.

I didn't completely understand you desired result in the missing block, but made a reasonable assumption

Dan
  • 10,614
  • 5
  • 24
  • 35