Solved

Zend_Form has problem with swedish characters

Posted on 2010-08-19
12
673 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 250 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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 109

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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How is this connection happening? 3 20
reverse engineer .sql from php files 11 35
Log in through ID 5 19
Force PDF to open inline as opposed to dowload 16 23
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 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