Checkbox return empty array if false


My names is Yann. I am a new user from Slim.

When processing a form with checkboxes, if they are empty (false), it returns an empty array. I need to indicate NULL in my database field.

To do this, I added a condition in the process.

if($request->getMethod() === 'POST') {
    $data = $request->getParsedBody();

    // Permet de renvoyer null si la checkboxe n'est pas cochée
    if (!isset($data['is_blacklisted'])){$data['is_blacklisted'] = null;}
    if (!isset($data['open_portal'])){$data['open_portal'] = null;}

    $this->getRepository(Customer::class)->update($data, $id);
    $this->flash->addMessage('info', 'Paramètres mis à jour');
    return $response->withStatus(302)->withHeader('Location', '/admin/customers/' . $id .'/parameters');

Is this the right solution or Slim allows you to do it more “cleanly”?

Thank you.

“Sorry for my English”

1 Like

You can use ?? to coalesce to null thus:

$data['is_blacklisted'] = $data['is_blacklisted'] ?? null;
$data['open_portal'] = $data['open_portal'] ?? null;

More on this: PHP: New features - Manual


Hello :slight_smile:

Ah, nice ! it’s more clean as this.

Thank you very much for you rapidity !