Developing software in the Real World

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.

Integrating ZF2 forms into Slim

Let's say that you want to use Zend Framework 2's Form component outside of ZF2 itself. In this case, a Slim application. It turns out that Composer makes this quite easy, though there's quite a lot of code involved, so this is a long article. Start with a really simple Slim Application. index.php:

templates/home.php:

and of course, composer.json:

Given this starting point, we can now add Zend\Form. Firstly, we update composer.json:

continue reading.

Globally overriding validation messages for ZF2 forms

One thing that I always do when creating a Zend Framework 2 form is override the validation messages for a number of validators – EmailAddress in particular. I recently decided that I should probably sort this one out once and be done with it. Turns out that it's quite easy assuming that you use the FormElementManger to instantiate your forms. All that I need to do is create my own validator classes that extend the… continue reading.