Pragmatism 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: $settings = []; $container = new Slim\Container($settings); $app = new Slim\App($container); Slim 3 ships with Pimple… 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: <validators> <notEmpty> <messages> <isEmpty>Please provide your telephone number</isEmpty> </messages> </notEmpty> </validators> 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… 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: class SomeForm extends \Zend\Form\Form implements \Zend\InputFilter\InputFilterProviderInterface { public function init() { $this->add([ 'name' => 'name', 'options' => ['label' => 'Name'], ]); $this->add([ 'name' => 'email', 'options' => ['label' => 'Email'], ]); $this->add([ 'name' => 'submit', 'type' => 'button', 'options' => [ 'label' => 'Filter', ], ]); $this->setValidationGroup(['name', 'email']); } public function getInputFilterSpecification() {… 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: use Seld\JsonLint\JsonParser; use Seld\JsonLint\ParsingException; $parser = new JsonParser(); $result = $parser->lint($json); if ($result instanceof ParsingException) { // $json is invalid JSON } We… 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: <form method="POST" role="form"> <div class="form-group"> {{ formRow(form.get('email')) }} </div> {{ formElement(form.get('submit')) }} </form> The ZF2 view helpers, formRow and formElement now look like Twig functions, however we don't want… 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. use Zend\Mail\Message; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; use Zend\Mime\Mime; use Zend\Mail\Transport\Sendmail; function sendEmail($to, $from, $subject, $html, $text, $attachments = null) { $message = new Message(); $message->addTo($to); $message->addFrom($from); $message->setSubject($subject); // HTML part… 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: require 'vendor/autoload.php'; $app = new \Slim\Slim(); $app->map('/', function () use ($app) { $app->render('home.php', array( 'form' => $form )); })->via('GET', 'POST'); $app->run(); templates/home.php:… 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.