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;
}
}
?>
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.
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.