Solved

Zend_Form has problem with swedish characters

Posted on 2010-08-19
12
669 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
 
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 108

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
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
LVL 108

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 108

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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress TK Title 8 24
Preventing users manipulating URL strings 10 33
PHP warning 4 28
is this a cms? 8 33
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now