ZF2 Zend Form Validation: Email addresses

Zend_Validator can be performed with a very sophisticated and secure e-mail validation, which can be provided with appropriate error messages to the user when needed:

    <?php

    use Zend\Form\Element;
    use Zend\InputFilter\InputProviderInterface;
    use Zend\Validator;

    class EmailElement extends Element implements InputProviderInterface
    {
        protected $attributes = array(
            'type' => 'email',
        );
        private  $wrongEmailMessage = 'Bitte geben Sie eine gültige E-Mail-Adresse an.';

        public function getInputSpecification()
        {
            return array(

                'name' => $this->getName(),
                'filters' => array(
                    array('name' => 'Zend\Filter\StringTrim'),
                ),
                'attributes' => array(
                    'type' => 'email'
                ),

                'validators' => array(
                    array(
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => $this->wrongEmailMessage
                            ),
                        ),
                    ),

                    array(
                        'name' => 'EmailAddress',
                        'options' => array(
                            'hostnameValidator' => null,
                            'messages' => array(
                                \Zend\Validator\EmailAddress::INVALID => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_FORMAT => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_HOSTNAME => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::CANNOT_DECODE_PUNYCODE  => $this->wrongEmailMessag,
                                \Zend\Validator\Hostname::INVALID  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_DASH  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_HOSTNAME  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_HOSTNAME_SCHEMA  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_LOCAL_NAME  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_URI  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::IP_ADDRESS_NOT_ALLOWED  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::UNDECIPHERABLE_TLD  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::UNKNOWN_TLD  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::LOCAL_NAME_NOT_ALLOWED  => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_MX_RECORD => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_SEGMENT => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::DOT_ATOM => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::QUOTED_STRING => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $this->wrongEmailMessage,
                            )
                        )
                    )
                )
            );
        }
    }

more…

Internationalization / multilingual PHP projects. Po and. Mo

For a multilingual PHP project should outsource all the words from the beginning or. mark, to be translated into another language. These features are included in the PHP function gettext an:

<?php echo gettext("Einen schönen Tag noch"); ?>

or Alias (which is not recommended, because you can not then properly)

<?php echo _("Einen schönen Tag noch"); ?>

So you can mark text for translation and translate later and can then exchange can be dynamically , when surfing on the English websites.

If you have programmed his project with gettext, You can easily end up with tabular files generate from source Poedit. There, man very clearly the translation of strings to enter plural forms of words.

more…

Zend APIs: individual components using

The Zend Framework contains many practical interfaces to common APIs:

  1. Google Base (Zend_Gdata)
  2. Open ID (Zend_OpenId)
  3. Zend_Service with:
  • Zend_Service_Akismet
  • Zend_Service_Amazon
  • Zend_Service_Amazon_EC2
  • Zend_Service_Amazon_S3
  • Zend_Service_Audioscrobbler
  • Zend_Service_Delicious
  • Zend_Service_DeveloperGarden
  • Zend_Service_Ebay
  • Zend_Service_Ebay_Finding
  • Zend_Service_Flickr
  • Zend_Service_LiveDocx
  • Zend_Service_Nirvanix
  • Zend_Service_ReCaptcha
  • Zend_Service_ShortUrl
  • Zend_Service_Simpy
  • Zend_Service_SlideShare
  • Zend_Service_StrikeIron
  • Zend_Service_Technorati
  • Zend_Service_Twitter
  • Zend_Service_WindowsAzure
  • Zend_Service_Yahoo

A complete list is here.

These APIs can be integrated following dimensions without the need to program as a whole website to Zend project (at Ex. by Gbase: Google Base):

more…

PHP database backend generate Scaffolding

To fast an easy way for customers, to access the database, can be a practical online tool phpScaffold use.

Thus can be generated quickly on changing surfaces and customizen, Insertion and deletion of records from a table.

For larger projects, however, you should use an appropriate framework, Scaffolding which supports:

Bsp result:

Part 1: Clean Code – Rules for good, clean code

To write clean code and easy to understand the ultimate goal is a good IT Projet. Much depends on:

  • Maintainability
  • Training time for other programmers, means fast, what do individual functions
  • Robustness to changes
  • Testability, everything falls together, with small changes, can quickly stable updates are
  • Popularity with other programmers e.g.: bei Open Source Projekten, as a negative example is called XT-Commerce

The highly recommended standard work on the subject is “Clean Code – Refactoring, Patterns, Testing and techniques for clean code” of Robert C. Martin. In this article, chapter 1 to 3 treated.

Meaningful names

The name of a variable, Function or class should declare immediately, why you exist, what it does and how it is used. If a variable requires a Comment, It expresses not its purpose.

Bsp:

int d //Anzahl vergangener Tage
besser ist:
int daysSinceCreation;

Pronounceable name use

No constructs with unclear abbreviations as: int daSiCre instead of daysSinceCreation.

Searchable name use

Modern IDEs make searching easy, but it's no, when you have to look for the letters e and a control variable is swamped by results.

Variable names with a letter must only be used as local variables in short methods.

more…

Zend test with PHPUnit

PHPUnit for testing was used as a standard testing tool for PHP and Netbeans Intergrated.

This was a bootstrap file is created, in the application variables are loaded (IndexControllerTest.php):

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }
}

A test may look like dan (Test Files/application/controllers/IndexControllerTest.php):

require_once '/../../TestConfiguration.php';
require_once '/../../../application/models/Players.php';

class PlayersTest extends PhpUnit_Framework_TestCase{
    public function setUp()
    {
        TestConfiguration::setupDatabase();
    }
    public function testFetchAll(){
        $TippspielObjekt = new Players();
        $TippspielAnzahl = $TippspielObjekt->fetchAll();
        $this->assertGreaterThan(10, $TippspielAnzahl->count());        
    }
    public function testLatest(){
        $TippspielObjekt = new Players();
        $TippspielAnzahl = $TippspielObjekt->fetchLatest(1);
        $this->assertSame(1,$TippspielAnzahl->count());
    }
}