Zend_Form has problem with swedish characters

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..?
LVL 4
davidsperlingAsked:
Who is Participating?
 
svgmucConnect With a Mentor Commented:
Pass the output through htmlentities().
0
 
davidsperlingAuthor Commented:
>>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
 
davidsperlingAuthor Commented:
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
svgmucCommented:
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
 
davidsperlingAuthor Commented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
 
Ray PaseurCommented:
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
 
davidsperlingAuthor Commented:
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
 
davidsperlingAuthor Commented:
And the output is:
ee.png
0
 
davidsperlingAuthor Commented:
I forgot to mention that Application_Form_Editprofile now extends Application_Form_Swedishworkaroundform, of course.
0
 
Ray PaseurCommented:
What a hoot!  You should copyright this just to annoy the Zend developers ;-)

Thanks for sharing it, ~Ray
0
 
davidsperlingAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.