0 votes

How to validate when the parameter is empty?

I have a rest api in which it is possible that the parameter "isproperty", can come empty or in his absence 1 or 0.

public function searchproperty($isproperty)
    {
        if($isproperty == 0){
            return parent::search();
        }else if($isproperty == 1){
            $sql = "SELECT * FROM $this->modelClass WHERE is_property = 1";
            $data = $this->modelsManager->createQuery($sql)->execute();

            $this->response->setStatusCode(200);
            $this->response->setJsonContent($data);
            return $this->response->send();
        }

    }

not sure how to validate this part as if the parameter does not have anything it marks me an error

0voto

Agustin Points 1

You could use empty, it returns true if a variable is empty. I hope you serve:

public function searchproperty($isproperty)
    {
        if(!empty($isproperty)){   // TE AGREGUE ESTA LINEA, si isproperty NO esta vacia
            if($isproperty == 0){
                return parent::search();
            }else if($isproperty == 1){
                $sql = "SELECT * FROM $this->modelClass WHERE is_property = 1";
                $data = $this->modelsManager->createQuery($sql)->execute();

                $this->response->setStatusCode(200);
                $this->response->setJsonContent($data);
                return $this->response->send();
            }

         }else{ // Y TAMBIEN AGREGUE ESTAS TRES LINEAS
                  echo 'esto se imprimirá si la variable está vacia';
                 }


    }

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X