Kill and reply JSON with PHP

Asked

Viewed 241 times

0

My framework submits forms with a fixed concatenation stating the type of data, which validation it should pass, the field name relative to the table in the BD and the value, would be something like:

data[0][]company_razao_social::Empresa::company_name::text::undefined

There is a class that explodes the string and mounts an array to check:

  {
        $arr = [];
        $key = 0;
        //print_r($_POST);
        foreach ($POST['data'] as $row)
        {
            $data = $row[0];
            $EXP = explode('::', $data);
            $label = $EXP[0];
            $value = $EXP[1];
            $validation = $EXP[4];
            if ($label == 'password')
            {
                $arr[$key][$label] = sha1($value); 
            } else
            {
                $arr[$key][$label] = utf8_encode(\vidbModel\validate::get_rs($value,$validation));
            }
            $key += 1;
        }
        return $arr;
    }

This returns an array to me:

Whether it has been validated:

$data['company_razao_social'] = 'Valor do campo';

Or, if not:

$data['company_razao_social'] = 'O campo não foi validado (Aqui vem a resposta de não ter sido)';

The problem of this is dealing with the client which is the error when there is someone.

What I want is:

Kill the function, and return a json only with the error when the data does not pass the validation, instead of continuing and returning the whole form with all the errors that there were and having to handle all this client side.

//Dentro do loop para desconcatenar
{
    $arr[$key][$label] = utf8_encode(\vidbModel\validate::get($value,$validation));
    if($$arr[$key][$label] = 'Possui erro'){
        die('devolver um json aqui');
    }
}

That way, the answer doesn’t come from json, and I’m not being able to return json with die, there’s another way to kill the method and return ? .

Note: break does not work either.

1 answer

2


A little confused, but I believe it would be something like:

//Dentro do loop para desconcatenar
{
    $arr[$key][$label] = utf8_encode(\vidbModel\validate::get($value,$validation));
    if($$arr[$key][$label] = 'Possui erro'){
        header('Content-Type: application/json');
        echo json_encode(["erro" => "Mensagem de erro"]);
        die();
    }
}

Thus, the function header sends the appropriate HTTP header indicating that the response will be a JSON, the echo sets the body of the HTTP response, using the function json_encode to simplify, and finally to die would kill the process, ending the execution.

  • That’s right, it worked, I had not tested answers and kill later, I was trying to return along with the die, ai could not bring in json, no need for header, ja define the json header in the controller.

  • I think it’s even better to use exit(0) to kill the process, as it is thus killed successfully, without running the risk of the server sending a 500 response to the client. In fact, it might even be interesting to define the status of the answer in the code.

  • I simplified the code there, but has an answer stating true and false to know the status, it is that has a series of checks that were not in the context of the question, then removed from the example to be easier, I will check with.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.