Developing software in the Real World

Displaying errors in Expressive with Twig

If you're not using the Whoops error handler with Expressive and are using the Twig renderer, then you are given no information about the problem that occurred, even in debug mode. To fix this, I changed error.html.twig to this:

The new part is the within the {% if error %} block. In debug mode, error is set to the exception that was thown and is blank otherwise, so you won't leak sensitive information (as… continue reading.

Simple way to add a filter to Zend-InputFilter

Using Zend-InputFilter is remarkably easy to use:

How do you add your filter to it though? This is the world's most simple filter that does absolutely nothing: We'll call it MyFilter and store it in App\Filter\MyFilter.php:

Now you have a couple of choices: Extend Zend\InputFilter\Factory I needed to add my own filter in the least invasive way that I could and so I created App\InputFilter\Factory which extends Zend\InputFilter\Factory:

This class extends the… continue reading.

Zend\Input fallback value

Recently an issue was reported against Zend\InputFilter where the reporter has discovered a regression where the fallback value wasn't being populated correctly. Matthew investigated, fixed it and asked me to review it. I was fascinated as I didn't realise (or had completely forgotten!) that Zend\Input and Zend\InputFilter supported fallback values so I looked into it and it turns out that it's simple and works exactly as its name implies. For the basic case of using… continue reading.

Zend\Input and empty values

I'm forever getting confused about how the combination of Zend\Input's required, allow_empty & continue_if_empty interact with an empty value, so I've decided to write it down. These settings define what happens when you try to validate an empty value for a given input. For Zend\Input, empty means exactly equal to null, an empty string or an empty array. Firstly, let's start with the three settings: Setting Default What it does required true When true, the… continue reading.

Replacing Pimple in a Slim 3 application

One feature of Slim 3 is that the DI container is loosely coupled to the core framework. This is done in two ways: The App composes the container instance rather than extending from it. Internally, App depends on the container implementing the container-interop interface. You can see this decoupling in the way that you instantiate a Slim 3 application:

Slim 3 ships with Pimple by default, but my preference is for Zend\ServiceManager, so I… continue reading.

ZF2 validator message keys

In Zend Framework 2, if you define your input filter via configuration, then you can override validation messages using a format along the lines of:

Setting the message is easy enough, once you have the correct key name. This is a list of all the keys for the standard validators: Validator Key name Default message Alnum alnumInvalid Invalid type given. String, integer or float expected Alnum notAlnum The input contains characters which are non… continue reading.

Exclude elements from Zend\Form's getData()

If you need to exclude some elements from a Zend\Form's getData(), the easiest way is to use a validation group. For example:

The call to setValidationGroup() contains an array of all the elements you want to be validated. In this case we list all elements except the submit button. The side-effect is that when you called $form->getData(), only the values for those elements are returned which can sometimes be useful. Generalise Rather than having… continue reading.

Validating JSON with ZF2's Zend\Validator

Let's say that you have an admin form where the user can enter JSON and you'd like to validate that the JSON parses before allowing the user to submit. To do this, you can use the rather excellent jsonlint project by Jordi Boggiano. Obviously, add it via Compser :) Usage is simple:

We can wrap this up into a Zend Framework 2 validator quite easily:

Simply add RKA\Validator\Json to the input filter of… continue reading.

Using ZF2 Forms with Twig

Following on from looking at how to integrate Zend Framework 2 forms into Slim Framework, let's look at the changes required if you also happen to want to use Twig. When it comes to rendering the form, we would want our template to look like this:

The ZF2 view helpers, formRow and formElement now look like Twig functions, however we don't want to have to rewrite all our ZF2 view helpers into Twig. Fortunately,… continue reading.

Sending attachments in multipart emails with Zend\Mail

I've written before about how to send an HTML email with a text alternative in Zend\Mail, but recently needed to send an attachment with my multipart email. With help from various sources on the Internet, this is how to do it.

Let's look at some key parts. The text of the email The HTML and plain text parts of the email are treated the same. We create a Zend\Mime\Part and set the encoding and… continue reading.