I'm trying to create a simple application with Zend Framework 2 and now stacked at the point of adding comments form.
I have two modules "Book" and "Comment". I want to show a form for adding comments that is disposed in module "Comment" from "Books" module controller.
The problem appears when I'm trying to open forms tag.
I'm having this error : " Fatal error: Call to undefined method Comment\Form\CommentForm::openTag()".
I tried to check $this->addComment - with var_dump and everything looks great.
Can anybody help me to solve this problem ?
<div class="row">
<div class="col-xs-12 commentBlock">
<?php
if($this->addComment){
$this->addComment->prepare();
$commentFieldset = $this->addComment->get('comment');
print $this->addComment()->openTag($this->addComment);
}
?>
</div>
</div>
Here is 'Comment' module files :
Comment\Form\CommentFieldset
<?php
namespace Comment\Form;
use Comment\Entity\Comment;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class CommentFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('comment');
$this->add(
array(
'name' => 'commentTitle',
'attributes' => array(
// 'required' => 'required',
'class' => 'form-controll',
'placeholder' => 'Введите заголовок комментария'
),
'options' => array(
'label' => 'Заголовок комментария',
'label_attributes' => array(
'class' => 'text-center col-sm-3 control-label',
),
),
)
);
$this->add(
array(
'name' => 'commentText',
'type' => 'Zend\Form\Element\Textarea',
'attributes' => array(
'required' => 'required',
'class' => 'form=-controll',
'palceholder' => 'Введите текст комментария',
),
'options' => array(
'label' => 'Текст комментария',
'label_attributes' => array(
'class' => 'text-center col-sm-3 control-label',
),
),
)
);
$this->add(
array(
'name' => 'commentType',
'type' => 'Zend\Form\Element\Radio',
'attributes' => array(
'required' => 'required',
),
'options' => array(
'label' => 'Тип комментария',
'label_attributes' => array(
'class' => 'text-center col-sm-3 control-label',
),
'value_options' => array(
'positive' => 'postive',
'neutral' => 'neutral',
'negative' => 'negative',
),
),
)
);
}
public function getInputFilterSpecification()
{
return array(
'commentTitle' => array(
'required' => FALSE,
'filters' => array(
array(
'name' => 'Zend\Filter\StripTags',
),
array(
'name' => 'Zend\Filter\HtmlEntities'
),
array(
'name' => 'Zend\Filter\StringTrim',
),
),
'validators' => array(
array(
'name' => 'Zend\Validator\StringLength',
'options' => array(
'min' => 1,
'max' => 250,
),
),
),
),
'commentText' => array(
'required' => TRUE,
'filters' => array(
array(
'name' => 'Zend\Filter\StripTags',
),
array(
'name' => 'Zend\Filter\HtmlEntities'
),
array(
'name' => 'Zend\Filter\StringTrim',
),
),
'validators' => array(
array(
'name' => '\Zend\Validator\StringLength',
'options' => array(
'min' => 1,
'max' => 1000,
),
),
),
),
'commentType' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Zend\Validator\InArray',
'options' => array(
'haystack' => array(
'positive',
'neutral',
'negative',
),
'strict' =>\Zend\Validator\InArray::COMPARE_STRICT,
),
),
),
),
);
}
}
Comment\Form\CommentFieldset
<?php
namespace Comment\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class CommentForm extends Form
{
public function __construct()
{
parent::__construct('comment');
$this
->setAttribute('method', 'POST')
->setHydrator(new ClassMethodsHydrator())
->setInputFilter(new InputFilter())
;
$this->add(
array(
'type' => 'Comment\Form\CommentFieldset',
'options' => array(
'use_as_base_fieldset' => true,
),
)
);
$this->add(
array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
)
);
$this->add(
array(
'name' => 'captcha',
'type' => 'Zend\Form\Element\Captcha',
'options' => array(
'label' => 'А вы точно-приточно не робот ? ',
'captcha' => new \Zend\Captcha\Figlet(),
'attributes' => array(
'required' => true,
)
),
)
);
$this->add(
array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Добавить комментарий',
'class' => 'bigSubmit btn btn-pink',
),
)
);
$this->setValidationGroup(
array(
'csrf',
'captcha',
'comment' => array(
'commentTitle',
'commentText',
'commentType',
),
)
);
}
}
Comment\View\Helper\DisplayAddCommentForm
<?php
namespace Comment\View\Helper;
use Zend\Form\View\Helper\AbstractHelper;
class DisplayAddCommentForm extends AbstractHelper
{
public function __invoke()
{
return new \Comment\Form\CommentForm();
}
}
Book\Controller\BookController
<?php
namespace Book\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Comment\Form\CommentForm;
class BookController extends AbstractActionController
{
protected $bookModel;
protected $commentsModel;
public function showAction()
{
$isbn = $this->params()->fromRoute('num');
$book = $this->bookModel->getU(array($isbn));
$comments = $this->commentsModel->getBookComments(array($isbn));
if($_SESSION['user']){
$commentForm = new CommentForm();
} else {
$commentForm = NULL;
}
return new ViewModel(
array(
'book' => $book,
'comments' => $comments,
'addComment' => $commentForm,
)
);
}
**************
}
openTag() is method from form view helper not from your helper. In your view script change this line : print $this->addComment()->openTag($this->addComment); to this one print $this->form()->openTag($this->addComment);
Related
I have some issues adding a new tab in the backoffice menu.
I successfully created it with this function (called inside install method of module class):
public function createMenuTab() {
$tab = new Tab();
$tab->module = $this->name;
$tab->class_name = 'AdminQuote';
$tab->id_parent = 0;
$tab->active = 1;
foreach (Language::getLanguages(false) as $l)
$tab->name[$l['id_lang']] = 'Gestione Preventivi';
return (bool)$tab->add();
}
But now I don't know how to show a view.
I put the class AdminQuoteController in /controllers/admin/AdminQuote.php and it just extends ModuleAdminController.
What should I do now to show a view? I didn't find anything in the PS docs!
There is example from smartblog module.
<?php
class AdminImageTypeController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'smart_blog_imagetype';
$this->className = 'BlogImageType';
$this->module = 'smartblog';
$this->lang = false;
$this->context = Context::getContext();
$this->bootstrap = true;
$this->fields_list = array(
'id_smart_blog_imagetype' => array(
'title' => $this->l('Id'),
'width' => 100,
'type' => 'text',
),
'type_name' => array(
'title' => $this->l('Type Name'),
'width' => 350,
'type' => 'text',
),
'width' => array(
'title' => $this->l('Width'),
'width' => 60,
'type' => 'text',
),
'height' => array(
'title' => $this->l('Height'),
'width' => 60,
'type' => 'text',
),
'type' => array(
'title' => $this->l('Type'),
'width' => 220,
'type' => 'text',
),
'active' => array(
'title' => $this->l('Status'),
'width' => 60,
'align' => 'center',
'active' => 'status',
'type' => 'bool',
'orderby' => false
)
);
parent::__construct();
}
public function renderForm()
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Blog Category'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Image Type Name'),
'name' => 'type_name',
'size' => 60,
'required' => true,
'desc' => $this->l('Enter Your Image Type Name Here'),
),
array(
'type' => 'text',
'label' => $this->l('width'),
'name' => 'width',
'size' => 15,
'required' => true,
'desc' => $this->l('Image height in px')
),
array(
'type' => 'text',
'label' => $this->l('Height'),
'name' => 'height',
'size' => 15,
'required' => true,
'desc' => $this->l('Image height in px')
),
array(
'type' => 'select',
'label' => $this->l('Type'),
'name' => 'type',
'required' => true,
'options' => array(
'query' => array(
array(
'id_option' => 'post',
'name' => 'Post'
),
array(
'id_option' => 'Category',
'name' => 'category'
),
array(
'id_option' => 'Author',
'name' => 'author'
)
),
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'switch',
'label' => $this->l('Status'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'id' => 'active',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active',
'value' => 0,
'label' => $this->l('Disabled')
)
)
)
),
'submit' => array(
'title' => $this->l('Save'),
)
);
if (!($BlogImageType = $this->loadObject(true)))
return;
$this->fields_form['submit'] = array(
'title' => $this->l('Save '),
);
return parent::renderForm();
}
public function renderList()
{
$this->addRowAction('edit');
$this->addRowAction('delete');
return parent::renderList();
}
public function initToolbar()
{
parent::initToolbar();
}
}
class BlogImageType extends ObjectModel
{
public $id_smart_blog_imagetype;
public $type_name;
public $width;
public $height;
public $type;
public $active = 1;
public static $definition = array(
'table' => 'smart_blog_imagetype',
'primary' => 'id_smart_blog_imagetype',
'multilang' => false,
'fields' => array(
'width' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
'height' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
'type_name' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
'type' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
),
);
.
.
.
}
Finally, I find this way:
I created the class AdminQuoteController in /controllers/admin/AdminQuote.php that extends ModuleAdminController. With this code inside:
class AdminQuoteController extends ModuleAdminController {
public function renderList() {
return $this->context->smarty->fetch(_PS_MODULE_DIR_.'preventivi/views/templates/admin/content.tpl');
}
}
This works even without declare display(), init(), initContent() or __construct() as I read in other previous threads.
I am developing an image gallery and I want to check if the input file has a file set or not.
This is my try, where only the title is checked, but if the user hasn't set an image it is not detected, what am I doing wrong?
Form
namespace Backoffice\Form;
use Zend\Form\Form;
class GalerieForm extends Form
{
public function __construct($galerieContent = null)
{
parent::__construct('galerie-form');
$this->setAttribute('action', '/backoffice/galerie/add');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->setAttribute('enctype', 'multipart/form-data');
$this->setInputFilter(new \Backoffice\Form\GalerieFilter());
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
'id' => 'title',
'value' => $galerieContent->title,
'class' => 'form-control'
),
'options' => array(
'label' => 'Picture Title:',
'label_attributes' => array(
'class' => 'control-label'
)
),
));
$this->add(array(
'name' => 'picture',
'attributes' => array(
'type' => 'file',
'id' => 'picture-selector',
'value' => $galerieContent->picture,
'class' => 'btn btn-file',
),
'options' => array(
'label' => 'Picture:',
'label_attributes' => array(
'class' => 'col-xs-1 control-label',
)
),
));
$this->add(array(
'name' => 'update-from',
'attributes' => array(
'type' => 'hidden',
'value' => 'galerie'
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Update Gallery Content',
'class' => 'btn btn-primary'
),
));
}
}
InputFilter
namespace Backoffice\Form;
use Zend\Form;
use Zend\InputFilter\InputFilter;
use Zend\Validator\File\IsImage;
class GalerieFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'title',
'required'=> true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
),
));
$this->add(array(
'name' => 'picture',
'required'=> true
));
}
}
Controller
public function addAction()
{
if ($this->getRequest()->isPost())
{
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
var_dump($post);
$form = new \Backoffice\Form\GalerieForm();
$form->setData($post);
if ($form->isValid()) {
var_dump($post);
}
}
else {
$form = new \Backoffice\Form\GalerieForm();
}
return new ViewModel(array(
'form' => $form
));
}
I had the same problem before, then i putted this into my controller:
if ($request->isPost()) {
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
// To get the required error message
if (!$post['picture']['tmp_name']) {
$post['picture'] = null;
}
$form->setData($post);
}
I suggest to extend your InputFilter with an additional Valitator e.g. UploadFile which checks if there is an uploaded file. This would be more maintainable than define an additional validation rule within your controller action.
InputFilter code..
$this->add(array(
'name' => 'picture',
'required' => true,
'validators' => array(
new \Zend\Validator\File\UploadFile()
)
)
ZF2 has several standard validators for File validation and already InputFilters especially for File Uploads.
I have a requirement of rendering and processing the same form again if user check a select box. I looked into form collections but it didn't exactly access my problem because I don't need to render a set of fields instead my requirement is to render the complete form again. So what I added another function getClone($prefix) to get the clone of form which returns me the clone of form object after adding a prefix in the name of form. Like this
<?php
namespace Company\Form;
use Zend\Form\Form;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class CompanyAddress extends Form implements InputFilterAwareInterface {
protected $inputFilter;
public function __construct($countries, $name = 'null') {
parent::__construct('company_address');
$this->setAttribute('method', 'post');
$this->setAttribute('id', 'company_address');
$this->add(array(
'name' => 'street_1',
'attributes' => array(
'id' => 'street_1',
'type' => 'text',
),
'options' => array(
'label' => 'Street *',
)
));
$this->add(array(
'name' => 'street_2',
'attributes' => array(
'id' => 'street_2',
'type' => 'text',
),
'options' => array(
'label' => 'Street 2',
)
));
$this->add(array(
'name' => 'country_id',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'country_id',
),
'options' => array(
'label' => 'Country',
'value_options' => $countries
)
));
$this->add(array(
'name' => 'city_id',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'city_id',
),
'options' => array(
'label' => 'City ',
'value_options' => array()
)
));
$this->add(array(
'name' => 'zip',
'attributes' => array(
'id' => 'zip',
'type' => 'text',
),
'options' => array(
'label' => 'ZIP',
'value_options' => array()
)
));
$this->add(array(
'name' => 'address_type',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'zip',
),
'options' => array(
'label' => 'Type',
'value_options' => array(
'' => 'Select Type',
'default' => 'Default',
'invoice' => 'Invoice',
'both' => 'Both',
)
)
));
}
public function getClone($prefix){
$form = clone $this;
foreach($form->getElements() as $element){
$name = $element->getName();
$element->setName("$prefix[$name]");
}
return $form;
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'street_1',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array('message' => 'Street cannot be empty'),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'street_2',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'city_id',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array('message' => 'City cannot be empty'),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'country_id',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array('message' => 'Country cannot be empty'),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'zip',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array('message' => 'ZIP cannot be empty'),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'address_type',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array('message' => 'Address Type cannot be empty'),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
and than in my action I did this
public function testAction() {
$countries = $this->getServiceLocator()->get('Country')->getSelect();
$companyAddressForm = new CompanyAddressForm($countries);
$clone = $companyAddressForm->getClone('address2');
if($this->request->isPost()){
$data = $this->request->getPost();
$companyAddressForm->setData($data);
$clone->setData($data['address2']);
$isFormOneValid = $companyAddressForm->isValid();
//$isFormTwoValid = $clone->isValid();
}
$view = new ViewModel();
$view->companyAddressForm = $companyAddressForm;
$view->clone = $clone;
return $view;
}
This is working as I expected it to work, forms are rendering and validating correctly, I want to know whether it is a correct way or a hack?
If both of the forms are exactly the same then you don't need to declare and validate two forms in the controller action.
Reason:
If there are two forms rendered your HTML page. Only one of the forms can be posted by the users at a time and you need to validate only one form at a time. What you are doing now is to validate the same form with the same post data over again. So either $isFormOneValid, $isFormTwoValid are both true or both false.
Instead, declare only one form, render it twice in your view, and upon post, validate it with the post data. If the two forms differ in properties, filters, validators, etc. then you can create two methods like $form->applyForm1Validarots() and $form->applyForm2Validarots, check the value of your select element, and call the appropriate method to apply the validaorts / filters before calling isValid() on it.
Hope I helped
I'm rendering a form to add a class (course) to a database. The class has a certain starttime and endttime. Both are time of day fields. I created a fieldset for the class:
<?php
namespace Admin\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterProviderInterface;
class ArtClassFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('artclass');
$this->add(array(
'name' => 'dayofweek',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Day of week:',
'value_options' => array(
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
),
),
));
$this->add(array(
'name' => 'starttime',
'type' => 'Zend\Form\Element\Time',
'options' => array(
'label' => 'Start time:',
'format' => 'H:ia',
),
)
);
$this->add(array(
'name' => 'endtime',
'type' => 'Zend\Form\Element\Time',
'options' => array(
'label' => 'End time:',
'format' => 'H:ia',
),
)
);
$this->add(array(
'name' => 'teacher',
'type' => 'Admin\Form\TeacherSelectorFieldset',
'options' => array(
'label' => 'Teacher:',
)
)
);
}
public function getInputFilterSpecification()
{
return array(
'dayofweek' => array(
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => array(
'min' => 1,
'max' => 6,
),
),
),
),
'starttime' => array(
'required' => true,
),
'endtime' => array(
'required' => true,
),
'teacher' => array(
),
);
}
}
In my Form class I simply add this fieldset to my form:
<?php
namespace Admin\Form;
use Zend\Form\Form;
class ArtClassAdd extends Form
{
public function __construct()
{
parent::__construct("artclass-add");
$this->setAttribute('action', '/admin/artclass/add');
$this->setAttribute('method', 'post');
$this->add(array(
'type' => 'Admin\Form\ArtClassFieldset',
'options' => array('use_as_base_fieldset' => true)
)
);
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save'
)
)
);
}
}
The format of the two time fields is 'H:ia' so that means I will get something like '11:00am'. What I would like to do now is to validate that the starttime is before the endtime. The question is how do I do that? I'm thinking I should probably use Zend\Validator\Callback, but not sure.
You're on the right track. You have to use the Callback validator. Use something like this for the endtime input spec:
return array(
'endtime' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
'callback' => function($value, $context)
{
$endtime = DateTime::createFromFormat'H:ia', $value);
$starttime = DateTime::createFromFormat('H:ia', $context['starttime']);
return $endtime > $starttime;
}
),
),
),
),
);
I have set my form for validation using $form->setData().
After validation I am not receiving all of my properties back using $form->getData().
I am using following lines in controller
and somehow $form->getData() is not returning all fields anyone has any idea why?
if ($request->isPost())
{
$company = new Company();
$form->setInputFilter($company->getInputFilter());
$form->setData($request->getPost());
print_r($request->getPost()); // getPost shows all fields fine
if ($form->isvalid())
{
print_r($form->getData()); // is returning only select and text type fields which are in input filter. why?
}
}
my form is looks like this.
class Companyform extends Form
{
public function __construct()
{
parent::__construct('company');
$this->setAttribute ('method', 'post');
$this->setAttribute ('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden'
),
));
$this->add ( array (
'name' => 'title',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'title',
'options' => array(
'mr' => 'Mr',
'miss' => 'Miss',
'mrs' => 'Mrs',
'dr' => 'Dr'
),
'value' => 'mr'
),
'options' => array(
'label' => 'Title'
)
));
$this->add(array(
'name' => 'fname',
'attributes' => array(
'id' => 'fname',
'type' => 'text',
'placeholder' => "First Name",
),
'options' => array(
'label' => 'First Name'
)
));
$this->add(array(
'name' => 'surname',
'attributes' => array(
'id' => 'surname',
'type' => 'text',
'placeholder' => "Surname Name",
),
'options' => array(
'label' => 'Surname Name'
)
));
$this->add(array(
'name' => 'companyName',
'attributes' => array(
'id' => 'companyName',
'type' => 'text',
'placeholder' => "Company Name",
),
'options' => array(
'label' => 'Company Name'
)
));
$this->add(array(
'name' => 'address1',
'attributes' => array(
'id' => 'address1',
'type' => 'text',
'placeholder' => "Address Line 1",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address2',
'attributes' => array(
'id' => 'address2',
'type' => 'text',
'placeholder' => "Address Line 2",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address3',
'attributes' => array(
'id' => 'address3',
'type' => 'text',
'placeholder' => "Address Line 3",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'btnsubmit',
'attributes' => array(
'id' => 'btnsubmit',
'type' => 'submit',
'value' => 'Add',
'class' => 'btn btn-primary'
),
));
}
}
and This is the input filter which I am using in entity company
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'companyName',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 255,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Every single Form-Element has to get validated! Even if the validator is empty like
$inputFilter->add($factory->createInput(array(
'name' => 'title'
)));
Only validated data get's passed from the form.