Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Zend_Form has problem with swedish characters

Posted on 2010-08-19
12
Medium Priority
?
677 Views
Last Modified: 2013-11-10
This code is supposed to render four cities in a dropdown in the viewscript (not shown here):
 
<?php
class Application_Form_Editprofile extends Zend_Form
{
.
.
.


    public function init()
    {
        $this->setMethod('post');

        
        $eCity=new Zend_Form_Element_Select("city");
        $eCity->setLabel("[please set label in view]");
//        $eCity->setMultiOptions($this->cities);

        //TEST CODE HERE:
        $kalle=array(4=>"Stockholm",7=>"Eskilstuna",12=>"Göteborg", 77=>"Malmö");
        $eCity->setMultiOptions($kalle);

        $sfCity=new Zend_Form_SubForm();
        $sfCity->addElement($eCity);        
        $this->addSubForm($sfCity, "city");
        }
}

Open in new window


But the html output is:
 
<form enctype="application/x-www-form-urlencoded" method="post" action=""><dl class="zend_form">

<dt id="city-label">&#160;</dt><dd id="city-element"><fieldset id="fieldset-city"><dl>
<dt id="city-label"><label for="city-city" class="optional">Stad</label></dt>
<dd id="city-element">
<select name="city[city]" id="city-city">
    <option value="4" label="Stockholm">Stockholm</option>
    <option value="7" label="Eskilstuna">Eskilstuna</option>
    <option value="12" label=""></option>
    <option value="77" label=""></option>
</select></dd></dl></fieldset></dd></dl></form>

Open in new window



See the pattern here..?
0
Comment
Question by:davidsperling
  • 7
  • 3
  • 2
12 Comments
 
LVL 14

Accepted Solution

by:
svgmuc earned 1000 total points
ID: 33480906
Pass the output through htmlentities().
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33482301
>>Pass the output through htmlentities().

Ok, let's see..

The view script attached will render:

 <form enctype="application/x-www-form-urlencoded" method="post" action=""><dl class="zend_form"> <dt id="city-label">&#160;</dt><dd id="city-element"><fieldset id="fieldset-city"><dl> <dt id="city-label"><label for="city-city" class="optional">Stad</label></dt> <dd id="city-element"> <select name="city[city]" id="city-city">  <option value="4" label="Stockholm">Stockholm</option>  <option value="7" label="Eskilstuna">Eskilstuna</option>  <option value="12" label=""></option>  <option value="77" label=""></option> </select></dd></dl></fieldset></dd></dl></form>

Same same ;)



<?php
$this->form->city->city->setLabel("Stad");
//echo $this->form;
echo htmlentities($this->form) ;
?>

Open in new window

0
 
LVL 4

Author Comment

by:davidsperling
ID: 33482389
Ok, may be you meant like this ;)

Still quite doesn't work though.


<?php
class Application_Form_Editprofile extends Zend_Form
{
 
	.
	.
	.

    public function init()
    {
        $this->setMethod('post');
        $eCity=new Zend_Form_Element_Select("city");
        
        //TEST CODE HERE:
        $kalle=array(4=>"Stockholm",7=>"Eskilstuna",12=>htmlentities("Göteborg"), 77=>htmlentities("Malmö"));

        $eCity->setLabel("[please set label in view]")
               ->setMultiOptions($kalle);
               
//        $eCity->setMultiOptions($this->cities);
        
        $sfCity=new Zend_Form_SubForm();
        $sfCity->addElement($eCity);
        
        $this->addSubForm($sfCity, "city");
        }
}

Open in new window

ee.png
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:svgmuc
ID: 33482611
I'd like to think that Zend is doing a re-encoding as long as it can read the original.
Probably, the special characters come in a strange encoding from the database?
You should try and align the encoding settings of database and Zend, ie. both UTF-8 or whatever fits best.
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33482984
The values are hard coded in this example and do not come from db.

How do you "set encoding in Zend"?

(main layout.phtml is set to iso-8859-1)
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 33483255
From the look of the PNG image above, it looks like the character strings are being entitized as they are put into the dropdown list.  This looks like the output of a double trip through htmlentities().  You did it once with this:

12=>htmlentities("Göteborg")

And something in the Zend code did it again.

Not sure what EE will do with the next line, so I will post a small code snippet, too.
G&amp;ouml;teborg

It looks like something in the class Zend_Form_Element_Select or perhaps Zend_Form_SubForm is "helping" you by removing fields that contain unusual characters.  You might consider using var_dump() to print out the objects are they are created.  Then you might be able to find the "helper" code that is deleting the strings.  This does not look so much like an encoding question as a simple programming error.  Almost as if a ctype filter is applied to the entire string and if any character fails, the string is deleted (instead of the bad character).
<?php // RAY_temp_zend_utf8.php
error_reporting(E_ALL);

echo "Goatberg";
echo "Göteborg";
echo htmlentities("Göteborg");

$g = htmlentities("Göteborg");
echo $g;

$h = htmlentities($g);
echo $h;

echo "<br/>VIEW SOURCE";

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33483282
Still guessing here, but have a look at the man page notes.
http://us2.php.net/manual/en/function.ctype-alpha.php

An interesting test might be a city named "foo!#$bar\t".
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33491429
Workaround invented.. :)

A better solution, anyone?

It's these kind of things you're using ZF for to avoid..









Swedishworkaroundform.php:

<?php
class Application_Form_Swedishworkaroundform extends Zend_Form
{

    public function  __construct() {
        parent::__construct();
    }

     /**
     * 'Cause there is a problem with swedish characters in Zend_Form..
     * Use from view script.
     */
    public function printSwedishWorkaround()
    {
        $s=htmlentities($this);
        $r=$s;
        $q="&amp;amp;";
        $r=str_replace($q."Aring;", "Å", $r);
        $r=str_replace($q."aring;", "å", $r);
        $r=str_replace($q."Auml;", "Ä", $r);
        $r=str_replace($q."auml;", "ä", $r);
        $r=str_replace($q."Ouml;", "Ö", $r);
        $r=str_replace($q."ouml;", "ö", $r);
        echo html_entity_decode($r);
    }
   
}

myview.phtml:

<p>This is html</p>
<?php
$this->form->city->city->setLabel("Stad");
//echo  $this->form;
$this->form->printSwedishWorkaround();
?>

Open in new window

0
 
LVL 4

Author Comment

by:davidsperling
ID: 33491433
And the output is:
ee.png
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33491445
I forgot to mention that Application_Form_Editprofile now extends Application_Form_Swedishworkaroundform, of course.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33491453
What a hoot!  You should copyright this just to annoy the Zend developers ;-)

Thanks for sharing it, ~Ray
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33492320
Actually you can boil it down to this:

:)
Swedishworkaroundform.php:
<?php

 /**
 * 'Cause there is a problem with swedish characters in Zend_Form..
 * Use from view script.
 * Usage:
 * echo $this->form; //as usual
 */
abstract class Application_Form_Swedishworkaroundform extends Zend_Form
{
    public function  __construct() {
        parent::__construct();
    }

    public function  __toString() {
        return html_entity_decode(parent::__toString()); //strings containing swedish chars (eg options in form) must be htmlentities()-ised or they'll be set to "" by here.
    }
}


myview.phtml:

<p>This is html</p>
<?php
$this->form->city->city->setLabel("Stad");
echo  $this->form;
?>

Open in new window

ee.png
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question