CakePHP 2 Displaying Validation Errors

Written by James McDonald

August 18, 2019

# app/Controller/ShipmentsController.php
    public function toggleShipped($id = null)
        if (!$this->Shipment->exists($id)) {
            throw new NotFoundException(__('Invalid shipment'));

        if ($this->request->is(['post', 'put'])) {
            $shipment = $this->Shipment->find(
                'first', [
                    'conditions' => [
                        '' => $id

            $data = [
                'shipped' => !(bool) $shipment['Shipment']['shipped'],
                'id' => $id

            if ($this->Shipment->save($data)) {
                $this->Flash->success("Successfully toggled shipped state");

            } else {
                $errorText = '';
                // get Validation errors and append them into a string
                $ve = $this->Shipment->validationErrors;
                foreach (array_keys($ve) as $ak) {
                    $errorText .= join(' ', $ve[$ak]);
                $this->Flash->error('Failed to toggle shipped state. ' . $errorText);
            $this->redirect(['action' => 'index']);


This is just how I handle and display potentially multiple CakePHP 2 Validation Errors by looping through them and concatenating them together before displaying them with the Flash element


Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

Toggen builds a new website

So I have built and hosted a new website for Roger Clark Pest Management they are a family owned and operated Pest...


Just downloaded WizTree (not a sponsor) from It seems to do what it says on the tin which is...

Scam call from “Telstra”

Just had a call from a man with a sub-continental accent from caller ID 0734946596 Told me there was problem with my...