We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with accented character in Volt text field

I'm using volt to write my views, in an page i'm receive values from database and load in text_field to permit edition and posterior update, the problem is:

view_page.volt
//my_var is the var wich i receive from controller

{{ text_field('text', 'value': my_var.text) }} //this works if the text no has accent

<input type="text" name="text" value="{{ my_var.text }}" /> //this way works independent of accented or not

What the problema??? for what Volt sintax dont work when the string contains accent???

The database collation is utf8_general_ci



51.2k

Well

  1. Use only one questionmark or exclamation mark by sentence. Multiple marks are sign of mental illnes :)
  2. It would be very heplpful for everyone, including you, to provide details about version that you are using, environment, etc.
  3. I have made this simple test using Phalcon 1.3.4 instaled on a Vagrant machine that it is running Ubuntu and PHP 5.5.6 and this works (My name contains an accent / diacritc). I also added Ñçé:
    public function indexAction()
    {
        $my_var = new \stdClass();
        $my_var->text = 'Călin Ñçé';
        $this->view->my_var = $my_var;
    }

view

{{ text_field('text', 'value': my_var.text) }}

It works like a charm :)

Well

  1. Use only one questionmark or exclamation mark by sentence. Multiple marks are sign of mental illnes :)
  2. It would be very heplpful for everyone, including you, to provide details about version that you are using, environment, etc.
  3. I have made this simple test using Phalcon 1.3.4 instaled on a Vagrant machine that it is running Ubuntu and PHP 5.5.6 and this works (My name contains an accent / diacritc). I also added Ñçé:
   public function indexAction()
   {
       $my_var = new \stdClass();
       $my_var->text = 'Călin Ñçé';
       $this->view->my_var = $my_var;
   }

view

{{ text_field('text', 'value': my_var.text) }}

It works like a charm :)

  1. I do not know where this might bother someone, maybe if it upsets you is a psychiatric problem your, suggest seek treatment ;)
  2. I really should have put the version.
  3. My version of Phalcon is the 1.3.3 with PHP 5.5.12, Windows 8.1 64bit

I try new tests but not works again

//controller
$this->view->text = 'Cão';

//view
{{ text_field('text', 'value':text) }}

.

Unfortunately, just checking the database collation isn't sufficient. You should also check the encoding and collation of the table column as well. Also, is the encoding of the page also UTF-8? I'm not completely knowledgeable in why there are different types of utf8 collations, but I've always used utf8_unicode_ci for my encoding.

Finally, if all that matches up, you should make sure your connection to the database uses the same encoding. I'm not sure what default Phalcon uses, but you can explicitely set it using the "charset" option. An example from my bootstrap.php file:

$DI->setShared('db',function() use($Config){
    return new \Phalcon\DB\Adapter\Pdo\Mysql([  'host'      => $Config->database->host,
                                                'dbname'        => $Config->database->database,
                                                'username'  => $Config->database->username,
                                                'password'  => $Config->database->password,
    /* this is the relevant line --> */        'charset'        => 'utf8'
                                            ]);
});

Unfortunately, just checking the database collation isn't sufficient. You should also check the encoding and collation of the table column as well. Also, is the encoding of the page also UTF-8? I'm not completely knowledgeable in why there are different types of utf8 collations, but I've always used utf8_unicode_ci for my encoding.

Finally, if all that matches up, you should make sure your connection to the database uses the same encoding. I'm not sure what default Phalcon uses, but you can explicitely set it using the "charset" option. An example from my bootstrap.php file:

$DI->setShared('db',function() use($Config){
  return new \Phalcon\DB\Adapter\Pdo\Mysql([  'host'      => $Config->database->host,
                                              'dbname'        => $Config->database->database,
                                              'username'  => $Config->database->username,
                                              'password'  => $Config->database->password,
  /* this is the relevant line --> */        'charset'        => 'utf8'
                                          ]);
});

Hello, your suggestion worked in part, when you did what recommended the string passed to display the value via volt syntax, but the accented characters were wrong, which has the accents were replaced by symbols. Any other suggestion to solve this? I thank you.

Hello, your suggestion worked in part, when you did what recommended the string passed to display the value via volt syntax, but the accented characters were wrong, which has the accents were replaced by symbols. Any other suggestion to solve this? I thank you.

I'm sorry, but I can't really understand what you're saying. I realize English isn't your first language - could you please try explaining what worked and what didn't?

After making a change that u suggested, the strings that were not shown to be passed but not the right way, for example: "NAÇÃO" instead of "NAÇÃO"

That's definitely an encoding issue. If you did ALL of the things I suggested, I'm out of ideas, as those suggestions dealt with all the possible places encoding can be wrong.

The strange thing is that as I said in the original post, with HTML syntax works, but with volt syntax does not.

Maybe try dropping down into raw PHP and see what the encoding of the string is:

<?php echo mb_detect_encoding($my_var->text); ?>

If the encoding is different, you could try the convert_encoding filter



51.2k

I had once a problem migrating data from an old DB to a new one, and I saved the new data using mb_convert_encoding method.

mb_convert_encoding($title, 'Windows-1252', 'UTF-8'),

So the last reply of @quasipickle might be helpful, but you should also try to update your data, not calling every time conver_encoding_filter . Also, sometimes, the <meta charset="utf-8"> tag makes the difference, or using the PHP:

header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding("UTF-8");


18.8k
Accepted
answer

I finally found the problem, i created another project to realize testings, and in new project like Calin Rada had spoke all works perfectly, so i back to my project and compare both configs files to find the difference and i found. In .htaccess of public directory "AddDefaultCharset" was set as "utf8_general_ci", then i changed to "UTF-8" and works perfectly.

Thanks to everybody