CakePHP 4 – Formatting Validation Errors for use with Flash Messages

by | May 17, 2022 | IT Tips | 0 comments

I've posted about this before but I think this approach is entirely less complicated

Validation errors in CakePHP come in an array similar to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$errors = [
  'asn_number' => [
    '_empty' => 'ASN cannot be blank',
  ],
  'their_po' => [
    '_empty' => 'Their PO cannot be empty',
  ],
  'spi_number' => [
    '_empty' => 'SPI Number is required',
  ],
  'their_po_date' => [
    '_empty' => 'Their PO Date cannot be empty',
  ],
  'from_address' => [
    '_empty' => 'Missing EDI from Address',
  ],
  'address' => [
    '_empty' => 'Missing EDI destination Address',
  ],
]

But what do you do when you want to nicely display them as the output of $this->Flash->success() or $this->Flash->error() ?

This is what I do to get the errors ready for display. Simply flatten the array and grab the leaves and then format them using CakePHP 4's Text::toList()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
namespace AppLibUtility;
 
use \Cake\Utility\Hash;
use \Cake\Utility\Text;
 
trait ErrorFormatterTrait
{
    /**
     * @param array $validationErrors The Validation Errors from an entity
     * @return string
     */
    public function formatErrors(array $validationErrors = []): string
    {
        // get Validation errors and append them into a string
        $flattened = array_values(Hash::flatten($validationErrors));
 
        return Text::toList($flattened);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
use App\Lib\Utility\ErrorFormatterTrait;
 
class ItemsController extends Controller
{
    use ErrorFormatterTrait;
 
    public function edit($id = null)
    {
        $item = $this->Items->get($id, [
            'contain' => [],
        ]);
 
        if ($this->request->is(['patch', 'post', 'put'])) {
            $item = $this->Items->patchEntity($item, $this->request->getData());
            if ($this->Items->save($item)) {
                $this->Flash->success(__('The item has been saved.'));
 
                return $this->redirect(['action' => 'index']);
            }
            $errors = $item->getErrors();
 
            $formattedErrors = $this->formatErrors($errors);
 
            $this->Flash->error(__('<strong>Errors: </strong> {0}', $formattedErrors));
        }
    }
}

0 Comments

Submit a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.