Posts Tagged ‘validation’

Tools for validating your website

Wednesday, May 27th, 2015 | Programming, Tech

There are some great online tools for validating that your website is looking and working well. Of course there are loads of these and many of them we’ve been using for years. Below though, I’ve listed a few I’ve been using the most in recent times or that are often overlooked.

W3C validator

An oldie but a goodie. The W3C validator ensures that your mark-up is valid. This is good for two reasons. One, it will find any problems, missing closing, tags, etc, that may be causing weird problems. Two, it will also point out stuff that you could be doing better like semantic tags and relevant meta tags.

http://validator.w3.org/

w3c-validator

Mobile friendly checker

Google are starting to crack down on websites that do not consider mobile users. Of course we all know we should be building mobile friendly websites, indeed, it should be mobile first these days! But it is hard to debug sometimes, especially given the fragmentation of devices.

Luckily Google now provides a tool that will give you a pass and fail, as well as showing you a preview of the site on an Android device. It’s not perfect, sometimes it fails to load assets and you have to come back later, but it is still an awesome tool.

https://www.google.co.uk/webmasters/tools/mobile-friendly/

Google Developers testing tool

Over on the Leeds Restaurant Guide we expose our reviews using the hreview schema. This means that sites like Google can see what ratings we give restaurants and put them directly in the search results. To check it is working correctly, you can use Google’s testing tool.

https://developers.google.com/structured-data/testing-tool/

Facebook Debugger

Recently I wrote about the Open Graph protocol which allows you to tell social networks (mostly Facebook) what titles, images and descriptions you want it to use when sharing a web page.

Facebook have a debugging tool to test your tags are working.

https://developers.facebook.com/tools/debug/

Using the Symfony2 Validator component outside the framework

Wednesday, June 20th, 2012 | Limited, Programming

Symfony2 has a very nice Validator component for data validation. As with most of the components, it can be used outside of the framework, but unfortunately, the documentation on how to do this is rather lacking. Indeed, even using it within Symfony but outside the controller can be difficult and this is made especially difficult if you’re using Propel because you can’t use annotations.

However, it can be done, and this tutorial will show you how.

When using the validation component somewhere other than in a Symfony2 controller (be that in an entirely different project, or just in a custom class inside the framework), the problem is that you don’t have access to the service container. So we need to create the object for ourselves.

Lets start by importing the namespace into our class.

use Symfony\Component\Validator as Validator;

This will give us quick and easy access to the Validator classes. Now we can use the ValidatorFactory to generate a validation object.

$factory = Validator\ValidatorFactory::buildDefault();
$validator = $factory->getValidator();

This is good. It means we now have a validation object that we can run against annotated classes. But what if we are using Propel and need to specify our validation rules in a YAML file?

$yamlFile = "../src/Acme/DemoBundle/Resources/config/validation.yml";
$factory = Validator\ValidatorFactory::buildDefault(array($yamlFile), false);
$validator = $factory->getValidator();

Now we can use the rules we specified in the YAML file to validate our Propel classes.

$violations = $validator->validate($model);

Splendid, we’re done. One further trick I’ll throw in with this post – what happens when you need to validate multiple objects and give a combined list of errors back? Because the list we get back is a custom object, we can’t just array_merge the two $violations list. But luckily, there is a function in the object to do this.

$violations = new Validator\ConstraintViolationList;
$firstViolations = $validator->validate($firstModel);
$secondViolations = $validator->validate($secondModel);

if ( count($firstViolations) > 0 ) {
	$violations->addAll($frstViolations);
}

if ( count($secondViolations) > 0 ) {
	$violations->addAll($secondViolations);
}

That will return you a single ConstraintViolationList that you can iterate through, containing errors from both models.

Symfony2 custom validation constraints

Friday, June 15th, 2012 | Limited, Programming

The Symfony2 framework comes with a really nice validation library and on top of the built in constraints that you can use you also have the ability to add your own custom constraints to perform any other validation checks you need to do.

The Cookbook has an article on how to create such a custom constraint, but unfortunately, as has happened a few times with the Symfony2 documentation, it misses out some fundamental and sometimes rather obscure step in order to get it working.

The problem is, it doesn’t tell you where to put the files. That leaves two options – either you have somewhere they need to be in order for Symfony to find them, or you need to fell Symfony where they are.

After hours of digging around, we eventually found the answer – in your validation.yml file you need to register the namespace and then reference that name when you call the validation rules.

Start by registering the namespace at the top of your validation.yml file.

namespaces:
  myValidator: Acme\ExampleBundle\Validator\

Lets say you have added a custom validation constraint called Postcode. When you want to invoke that from your validation rules, you need to reference the namespace you have just created.

postcode:
    - myValidator:Postcode: ~

You can then use this namespace with your custom validation rules, place them in the appropriate directory (in this case src/ExampleBundle/Validator, but you can store them somewhere else if you change the namespace – remember that Symfony2 will extract the path from the namespace).