ZF2- syntax error, unexpected '=>' (T_DOUBLE_ARROW) - php

My zend framework 2 project concerns an online restaurant Menu and something is wrong with my code because I am having this error:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in
C:\wamp\www\pizza\module\Pizza\src\Pizza\Model\pizza.php on line 57
Please help me to find what's wrong with my code. Here is pizza.php file:
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwareInterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct()
{
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] :null;
$this->name = (!empty($data['name'])) ? $data['name'] :null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] :null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] :null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] :null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] :null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] :null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter)
{
throw new \Exception("not used");
}
public function getInputFilter()
{
if(!$this->inputFilter)
{
$inputfilter = new InputFilter();
$inputfilter->add(array('name' => 'name',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array( //line 57
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,)
))
)
);
$inputfilter->add(array('name' => 'ingredients',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,)
))
)
);
$inputfilter->add(array('name' => 'smallprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'bigprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'familyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'partyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$this->inputFilter=$inputfilter;
}
return $this->inputFilter;
}
}

You are missing an array in 'filters':
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
Should be:
'filters' => array(array('name' => 'StringTrim'),
array('name' => 'StringTags')),
Same goes for other input.

your first array is just end just before 'validator' index on line number 57
validators index is not part of any array. its gone outside of array. so 'validators' => showing the error . make sure 'validators' part of which array...

Related

Create New Menu Tab In The Backoffice

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.

String to be escaped was not valid UTF-8 or could not be converted zend 2.3

Hello all I am creating form on zend 2.3 framework. And getting the following error message String to be escaped was not valid UTF-8 or could not be converted. In my editor zend studio that is I changed encoding to utf-8 before importing the project into a workspace. This is the stack tracce of this error:
#0 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Escaper\Escaper.php(162): Zend\Escaper\Escaper->toUtf8('?????????')
#1 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\Helper\EscapeHtmlAttr.php(25): Zend\Escaper\Escaper->escapeHtmlAttr('?????????')
#2 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\Helper\Escaper\AbstractHelper.php(51): Zend\View\Helper\EscapeHtmlAttr->escape('?????????')
#3 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\View\Helper\AbstractHelper.php(233): Zend\View\Helper\Escaper\AbstractHelper->__invoke('?????????')
#4 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\View\Helper\FormInput.php(128): Zend\Form\View\Helper\AbstractHelper->createAttributesString(Array)
#5 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\View\Helper\FormInput.php(101): Zend\Form\View\Helper\FormInput->render(Object(Zend\Form\Element\Submit))
#6 [internal function]: Zend\Form\View\Helper\FormInput->__invoke(Object(Zend\Form\Element\Submit))
#7 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php(399): call_user_func_array(Object(Zend\Form\View\Helper\FormSubmit), Array)
#8 C:\xampp\htdocs\disability\module\Admin\view\admin\admin\addstudent.phtml(36): Zend\View\Renderer\PhpRenderer->__call('formSubmit', Array)
#9 C:\xampp\htdocs\disability\module\Admin\view\admin\admin\addstudent.phtml(36): Zend\View\Renderer\PhpRenderer->formSubmit(Object(Zend\Form\Element\Submit))
#10 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php(506): include('C:\xampp\htdocs...')
#11 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\View.php(205): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel))
#12 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\View.php(233): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#13 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\View\View.php(198): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#14 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\View\Http\DefaultRenderingStrategy.php(103): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#15 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#16 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#17 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent), Array)
#18 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(352): Zend\EventManager\EventManager->trigger('render', Object(Zend\Mvc\MvcEvent))
#19 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(327): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#20 C:\xampp\htdocs\disability\public\index.php(17): Zend\Mvc\Application->run()
#21 {main}
Here is the code of the form's view
<?php
// module/Album/view/album/album/add.phtml:
$title = 'add student';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<div id="allarea">
<?php
// $form->setAttribute('action', $this->url('Admin\Controller\AdminController', array('action' => 'Admin\Form\AddstudentForm')));
$form->setAttribute('action', $this->url('admin', array('action' => 'addstudent')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('fio'));
echo $this->formRow($form->get('gender'));
echo $this->formRow($form->get('birthdate'));
echo $this->formRow($form->get('edge'));
echo $this->formRow($form->get('university'));
echo $this->formRow($form->get('group'));
echo $this->formRow($form->get('department'));
echo $this->formRow($form->get('grate'));
echo $this->formRow($form->get('enterence'));
echo $this->formRow($form->get('financesource'));
echo $this->formRow($form->get('studyform'));
echo $this->formRow($form->get('homeaddress'));
echo $this->formRow($form->get('actualaddress'));
echo $this->formRow($form->get('phone'));
echo $this->formRow($form->get('workplace'));
echo $this->formRow($form->get('services'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
</div>
here is the file Form/Addstudent.php
<?php
namespace Admin\Form;
use Zend\Form\Form;
class AddstudentForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('addstudent');
$this->add(array(
'name' => 'fio',
'type' => 'Text',
'options' => array(
'label' => 'fio',
),
));
$this->add(array(
'name' => 'gender',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'gender',
'value_options' => array(
'0' => 'm',
'1' => 'f',
),
),
));
$this->add(array(
'name' => 'birthdate',
'type' => 'Text',
'options' => array(
'label' => 'birthdate',
),
));
$this->add(array(
'name' => 'edge',
'type' => 'Text',
'options' => array(
'label' => 'edge',
),
));
$this->add(array(
'name' => 'university',
'type' => 'Text',
'options' => array(
'label' => 'vuz',
),
));
$this->add(array(
'name' => 'group',
'type' => 'Text',
'options' => array(
'label' => 'gruoup',
),
));
$this->add(array(
'name' => 'department',
'type' => 'Text',
'options' => array(
'label' => 'department',
),
));
$this->add(array(
'name' => 'grate',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'greate',
'value_options' => array(
'0' => '1',
'1' => '2',
'2' => '3',
'3' => '4',
'4' => '5',
'5' => '6',
),
),
));
$this->add(array(
'name' => 'enterence',
'type' => 'Text',
'options' => array(
'label' => 'enterance year',
),
));
$this->add(array(
'name' => 'financesource',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'finance source',
'value_options' => array(
'0' => 'butget',
'1' => 'contract',
),
),
));
$this->add(array(
'name' => 'studyform',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'studyform',
'value_options' => array(
'0' => 'on side',
'1' => 'distance',
),
),
));
$this->add(array(
'name' => 'homeaddress',
'type' => 'Text',
'options' => array(
'label' => 'Home address',
),
));
$this->add(array(
'name' => 'actualaddress',
'type' => 'Text',
'options' => array(
'label' => 'actual address',
),
));
$this->add(array(
'name' => 'phone',
'type' => 'Text',
'options' => array(
'label' => 'phone',
),
));
$this->add(array(
'name' => 'workplace',
'type' => 'Text',
'options' => array(
'label' => 'workplace',
),
));
$this->add(array(
'name' => 'services',
'type' => 'Zend\Form\Element\Textarea',
'options' => array(
'label' => 'services',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'save',
'id' => 'submitbutton',
),
));
}
}
this is the filter
<?php
namespace admin\Model;
// Add these import statements
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Admin implements InputFilterAwareInterface
{
public $fio;
public $gender;
public $birthdate;
public $edge;
public $university;
public $group;
public $department;
public $grate;
public $enterence;
public $financesource;
public $studyform;
public $homeaddress;
public $actualaddress;
public $phone;
public $workplace;
public $services;
protected $inputFilter; // <-- Add this variable
public function exchangeArray($data)
{
$this->fio = (isset($data['fio'])) ? $data['fio'] : null;
// $this->title = (isset($data['title'])) ? $data['title'] : null;
$this->gender = (isset($data['gender'])) ? $data['gender'] : null;
$this->birthdate = (isset($data['birthdate'])) ? $data['birthdate'] : null;
$this->edge = (isset($data['edge'])) ? $data['edge'] : null;
$this->university = (isset($data['university'])) ? $data['university'] : null;
$this->group = (isset($data['group'])) ? $data['group'] : null;
$this->department = (isset($data['department'])) ? $data['department'] : null;
$this->grate = (isset($data['grate'])) ? $data['grate'] : null;
$this->enterence = (isset($data['enterence'])) ? $data['enterence'] : null;
$this->financesource = (isset($data['financesource'])) ? $data['financesource'] : null;
$this->studyform = (isset($data['studyform'])) ? $data['studyform'] : null;
$this->homeaddress = (isset($data['homeaddress'])) ? $data['homeaddress'] : null;
$this->actualaddress = (isset($data['actualaddress'])) ? $data['actualaddress'] : null;
$this->phone = (isset($data['phone'])) ? $data['phone'] : null;
$this->workplace = (isset($data['workplace'])) ? $data['workplace'] : null;
$this->services = (isset($data['services'])) ? $data['services'] : null;
$escaper = new Zend\Escaper\Escaper('utf-8');
}
// Add content to these methods:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'fio',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'birthdate',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'university',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'group',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'department',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'enterence',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'homeaddress',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'actualaddress',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'phone',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'workplace',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'services',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 2000,
),
),
),
));
}
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
}
?>
Well, I solved the problem. The problem was in the controller. It is strange, but the controller file had cp1251 charset, despite the fact that I changed charset globaly in zend studio. So I changed the controller's encoding in its properties and now it is working properly.
Add this inside your inputFilter method.
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 200,
],
],
],
Also, in your code, where you create a new instance to Zend\Escaper\Escaper add 'utf-8' as a parameter to the constructor like that: $escaper = new Zend\Escaper\Escaper('utf-8');
If this doesn't work, show us your inputFilter code.
EDIT: I strongly recommend you to upgrade to zend 2.4.6 due to the huge amount of bug/security fixes. Just keep in mind that the Zend\Db component had some major changes in version 2.4 onward.

PHP Zend Framework validators (String) (Error messages on my language)

I have the next code in a model:
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'query',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 256,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
What I have to add, to the code become with error messages in my language ( non english)? (Errors : "required", "StringLength")
Will have seen an information about the validator:
Zend 2.1 Validator

ZF2- Interface '..\Model\InputFilterAwarelnterface' not found

My zend framework 2 project concerns an online restaurant Menu and something is wrong with my code because I am having this error:
Fatal error: Interface 'Pizza\Model\InputFilterAwarelnterface' not found
C:\wamp\www\pizza\module\Pizza\src\Pizza\Model\pizza.php on line 9
Please help me to find what's wrong with my code.
Here is pizza.php file:
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwarelnterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct()
{
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] :null;
$this->name = (!empty($data['name'])) ? $data['name'] :null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] :null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] :null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] :null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] :null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] :null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter)
{
throw new \Exception("not used");
}
public function getInpunFilter()
{
if(!$this->inputFliter)
{
$inputfilter = new InputFilter();
$inputfilter->add(array('name' => 'name',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,),
))
)
);
$inputfilter->add(array('name' => 'ingredients',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,),
))
)
);
$inputfilter->add(array('name' => 'smallprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'bigprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'familyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'partyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
return $this->inputFilter;
}
return $this->inputFilter;
}
}
Simple typo:
class Pizza implements InputFilterAwarelnterface
^
This character should be an 'I', not a 'l'

Zend Framework 2 InputFilter error when insert and update data

Catchable fatal error: Argument 1 passed to
Zend\Form\Form::setInputFilter() must implement interface
Zend\InputFilter\InputFilterInterface, null given, called in
C:\xampp\htdocs\pizza\module\Pizza\src\Pizza\Controller\PizzaController.php
on line 52 and defined in
C:\xampp\htdocs\pizza\vendor\zendframework\zendframework\library\Zend\Form\Form.php
on line 652
this is the file :
<?php
namespace Pizza\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Pizza\Form\AddPizzaForm;
use Pizza\Form\EditPizzaForm;
use Zend\View\Model\ViewModel;
use Pizza\Model\Pizza;
use Zend\View\Model\JSonModel;
class PizzaController extends AbstractActionController{
protected $pizzaTable;
public function indexAction()
{
return new ViewModel(array('pizzas' => $this->getPizzaTable()->find_all()));
}
public function addAction()
{
$add_form = new AddPizzaForm();
$request = $this->getRequest();
if ($request->isPost()) {
$pizza = new Pizza();
$add_form->setInputFilter($pizza->getInputFilter());
$add_form->setData($request->getPost());
if ($add_form->isValid()) {
$pizza->exchangeArray($add_form->getData());
$this->getPizzaTable()->save($pizza);
}
return $this->redirect()->toRoute('pizza');
}
return array('form' => $add_form);
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id',0);
if ($id == 0) {
//redirect to index action Route.
return $this->redirect()->toRoute('pizza');
}else{
$pizza = $this->getPizzaTable()->find_by_id($id);
$edit_form = new EditPizzaForm();
$edit_form->bind($pizza);
$request = $this->getRequest();
if ($request->isPost()) {
$edit_form->setInputFilter($pizza->getInputFilter());
$edit_form->setData($request->getPost());
if ($edit_form->isValid()) {
$this->getPizzaTable()->save($pizza);
return $this->redirect()->toRoute('pizza');
}
}
return array('id'=> $id,'form' => $edit_form);
}
}
public function deleteAction()
{
$id = (int) $_POST['id'];
$this->getPizzaTable()->delete($id);
$msg = "good";
$response = new JSonModel(array('response' => $msg));
return $response;
}
public function getPizzaTable()
{
if (!$this->pizzaTable) {
$sm = $this->getServiceLocator();
$this->pizzaTable = $sm->get('Pizza\Model\PizzaTable');
}
return $this->pizzaTable;
}
}
the Model File : Pizza
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwareInterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct(){
}
public function exchangeArray($data){
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->name = (!empty($data['name'])) ? $data['name'] : null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] : null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] : null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] : null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] : null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] : null;
}
public function getArrayCopy(){
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter){
throw new \Exception("not Used");
}
public function getInputFilter(){
if (!$this->inputFilter) {
$inputfilter = new InputFilter();
$inputfilter->add(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,
))
)
));
$inputfilter->add(array(
'name'=> 'ingredients',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,
))
)
));
$inputfilter->add(array(
'name' => 'smallprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'bigprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'familyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name'=> 'partyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$this->inputFilter = $inputfilter;
}else{
return $this->inputFilter;
}
}
}
Please remove else in your getInputFilter method. I think this is what caused the error. This is why you got the error : null given, the $inputfilter is never returned the first time.
I corrected it for you. Replace your method by this one :
public function getInputFilter(){
if (!$this->inputFilter) {
$inputfilter = new InputFilter();
$inputfilter->add(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,
))
)
));
$inputfilter->add(array(
'name'=> 'ingredients',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,
))
)
));
$inputfilter->add(array(
'name' => 'smallprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'bigprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'familyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name'=> 'partyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$this->inputFilter = $inputfilter;
}
return $this->inputFilter;
}
You should check that $pizza->getInputFilter() is returning an object that implements a Zend\InputFilter\InputFilterInterface interface. Because setInputFilter() method of your Form class only accepts this kind of objects.

Categories