ZF2 Zend Form Validation: User text-input


In order to safely text filters you can with the Zend Framwork 2 Validator and an HTML filter as HTML Purifier A validation of the contents of conduct:

use Zend\Form\Element;
use Zend\InputFilter\InputProviderInterface;
use Zend\Validator;
use Zend\I18n\Validator\Alpha;
use Foo\Filter\HtmlPurify;

class TextElement extends Element implements InputProviderInterface
{
    protected $attributes = array(
        'type' => 'text',
    );

    public function getInputSpecification()
    {
        return array(

            'name' => $this->getName(),
            'filters' => array(
                array('name' => 'Foo\Filter\HtmlPurify'),
                array('name' => 'Zend\Filter\StringTrim'),
                array('name' => 'Zend\Filter\StripTags'),
                array('name' => 'Zend\Filter\HtmlEntities'),
            ),
            'attributes' => array(
                'type' => 'textarea'
            ),
            'validators' => array(
                array(
                    'name' => 'NotEmpty',
                    'options' => array(
                        'messages' => array(
                            \Zend\Validator\NotEmpty::IS_EMPTY => 'Bitte tragen Sie etwas ein.'
                        ),
                    ),
                ),
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'max' => 5000,
                        'messages' => array(
                            \Zend\Validator\StringLength::TOO_LONG => 'Bitte nicht zu viel Text.'
                        )
                    ),
                )
            ),
        );
    }
}

 

further information on the use of filters can be found here: Email validation with ZF2.