-1

please pardon my way of asking question, but this is a follow up on my last question How to check if value is empty in PHP [JSON], I had 4 answers on it but I have not been able to solve it, please help me out, been trying for three days nothing,

I have Objects and value in my JSON below, what I am trying to achieve is simple, lets say "payment_type" object is empty, I want to tell the user its empty with response
payment_type parameter is required and if it's value is empty which is "1" I respond with payment_type value is required, I want to tell the user, my issue is that

// api.php

public function create_insert_new_delivery()
{
    $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
    );

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



{
    "name":"create_insert_new_delivery",
    "param":{
       "payment_type":"1",
       "date_of_delivery":"B",
       "your_generated_waybill_number":"39ei40909444567avaaaaba",
    }
}

WHAT I TRIED BUT IT DOESN'T WORK WELL ANSWER FROM THE PREVIOUS QUESTION, it only shows this error "$this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");"

// rest.php

/**
 * @param string $fieldName e.g payment_type
 * @param mixed $value e.g 1
 * @param bool $required is equal to true for strings
 */
public function validateParameter($fieldName, $value, $required = true){
    if ($required AND empty($fieldName)) {
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " parameter is missing");
    } elseif ($required AND empty($value)) {
        $this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");
    } elseif (!empty($fieldName) AND empty($value) AND $required){
        $this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");
    }
}

In the screenshot of the postman call Image its responds with the  value is empty instead of field name is empty.

Christian
  • 27,509
  • 17
  • 111
  • 155
Shasha
  • 439
  • 8
  • 26
  • 1
    I tried to format your question as nicely as possible. Please, in the future do this yourself. :) – Christian Jun 25 '18 at 23:27
  • @christian thank you very much for the format, but also please could you be of assistance if you may. – Shasha Jun 25 '18 at 23:28
  • Sure. First of, PHP cannot does not parse json payloads by itself, so I'm guessing you're using some framework that reads the HTTP request body and `json_decode`s it.This detail is important because the framework might do something fancy with the data. – Christian Jun 25 '18 at 23:29
  • @christian yes JWT, I am building an API, I just want the errors to be pointed out perfectly. – Shasha Jun 25 '18 at 23:30
  • @christian could you break down a little further "First of, PHP cannot does not parse json payloads by itself," and "This detail is important because the framework might do something fancy with the data", just a summary – Shasha Jun 25 '18 at 23:31
  • Sure, give this a quick look: https://stackoverflow.com/questions/18866571/receive-json-post-with-php – Christian Jun 25 '18 at 23:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173776/discussion-between-christian-and-shasha). – Christian Jun 25 '18 at 23:33

1 Answers1

2

If I understood your problem, you need to be careful when reading entries in an array that do not exist. PHP will trigger a warning/notice and pass a null value, which might not be what you expect. Here's the code that I'd use:

public function create_insert_new_delivery()
{
    $payment_type = $this->validateParameter(
        $this->param,
        'payment_type',
        STRING,
        true
    );

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

    $waybill_number = $this->validateParameter(
        $this->param,
        'your_generated_waybill_number',
        STRING,
        true
    );
}

public function validateParameter($params, $fieldName, $required = true)
{
    if ($required) {
        if (!array_key_exists($fieldName, $params)) {
            $this->throwError(API_PARAM_REQUIRED, "$fieldName parameter is missing");
        } elseif (empty($params[$fieldName])) {
            $this->throwError(EMPTY_PARAMETER, "$fieldName value is required");
        }
    }

    return $params[$fieldName];
}

What I did differently:

  • instead of passing the value, I passed the entire parameters array and did the check inside
  • I used array_key_exists, because empty() returns true when the value is a null (and you specifically want to check when the key is missing)
  • I simplified the conditions
  • I've returned the value (this was implied by the usage of the function, but missing in the original code)
Christian
  • 27,509
  • 17
  • 111
  • 155