Non-english characters in Zend_Form - zend-framework

I'm creating a form using Zend_Form, and all he words that contains diacritics are not rendered. The encoding is set to UTF-8, the collation of the database is set to utf-8_unicode. What else should I do/check?
The page header:
<meta content="text/html; charset=utf-8" http-equiv="content-type">
The Zend_form part:
$user = Doctrine::getTable("aclUser")->find(1, Doctrine_Core::HYDRATE_ARRAY);
$this->addElement('text','providerName',
array(
'label' => 'Provider_name',
'required' => false,
'readonly' => true,
'value' => $user['name'],
'filters' => array('StringTrim'),
'decorators'=> array(new Application_Form_Decorators_Custom())
)
);

Could be an issue submitting the form itself
Does setting your form's accept-charset to UTF-8 help?
<form accept-charset="UTF-8">
[form elements]
</form>

Solved. I added this settings in my.cnf and now everything is rendered ok:
[client]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
default-character-set=utf8
character-set-server = utf8
collation-server = utf8_unicode_ci
[mysql]
default-character-set=utf8

Related

WkHTMLtoPDF Unicode Issue

I've already read several similar StackOverflow posts and none of them were able to resolve my issue.
The Issue
I have a PDF that's being generated by WkHTMLtoPDF which contains a unicode RIGHT SINGLE QUOTATION MARK (U+2019, or ’) character. Rendered in a browser, the output looks like the following:
When I run this through WkHTMLtoPDF, I get the following:
The Code
I'm using the following for my CSS:
#font-face {
font-family: localGeorgia;
src: url("file:///usr/share/fonts/truetype/georgia/GEORGIA.TTF");
}
body {
overflow: visible !important;
font-family: localGeorgia, Georgia, Times, "Times New Roman", serif;
font-size: 12px;
}
I have also copied the Georgia font from my local computer to the server (there are several files in the /usr/share/fonts/truetype/georgia/ directory) and I have run fc-cache -fv to clear the font cache and run fc-list to verify that Georgia was properly installed. The localGeorgia font family was added as a formality because I still wasn't getting a working display.
I've verified both via the online docs and my operating system's character map that the Georgia font does support the RIGHT SINGLE QUOTATION MARK (see below) although I don't know how to prove definitively that this glyph is in the TrueType file (I'm not familiar with opening or parsing TrueType files)
At this point it's unclear to me why WkHTMLtoPDF is displaying this mess of characters instead of the proper unicode glyph
Additional details (environment and such)
I'm running Ubuntu 16.04
Laravel version 5.3
I'm using Laravel-Snappy version 0.3.3 (which is using KNP-Snappy version 0.4.3)
My config for Snappy is pretty straight-forward:
<?php
return array(
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
'image' => array(
'enabled' => false,
'binary' => '/usr/local/bin/wkhtmltoimage',
'timeout' => false,
'options' => array(),
'env' => array(),
),
);
The installed wkhtmltopdf version is 0.12.3 (with patched qt)
To generate the PDF I'm calling ->render() on the View, passing this to PDF::loadHTML, then calling ->inline() on the result and returning a response. Here's a minimal example of how I'm generating the PDF:
$property = Property::find(1);
$view = View::make("pdf.flier")->with(["property" => $property]);
$pdf = PDF::loadHTML($view->render())->inline();
return response($pdf)->header("application/pdf")->header("Content-Disposition", "attachment; filename=flier.pdf");
The HTML is incredibly simple:
<html>
<head>
<base href="{{ url("/") }}" />
<link rel="stylesheet" type="text/css" href="css/flier.css" />
</head>
<body>
<img src="{{ $property->image }}" />
<h1>{{ $property->title }}</h1>
</body>
</html>
The CSS gives the h1 an absolute position over top of the image
After a couple of days, I've finally figured this out
The issue does not lie with the font. If it did, I would see a glyph fail to load (e.g. - a box or a question mark would appear in place of the unicode character)
Instead what I'm seeing is several incorrect glyphs appear in place of the desired unicode character. This indicates an encoding issue, not a font issue. WkHTMLtoPDF is interpreting the 3-byte unicode character as 3 individual 1-byte ASCII characters
The problem is that my browser has a default encoding of UTF-8, but WkHTMLtoPDF does not (at least not in version 0.12.3). The fix was simple: update my config file
<?php
return array(
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
'timeout' => false,
'options' => array(
'encoding' => 'utf-8'
),
'env' => array(),
),
'image' => array(
'enabled' => false,
'binary' => '/usr/local/bin/wkhtmltoimage',
'timeout' => false,
'options' => array(
'encoding' => 'utf-8'
),
'env' => array(),
),
);
Note: In my research I found some examples of people claiming the "--encoding" option did not work for them, however adding a meta charset tag to the HTML did:
<meta charset="utf-8">

Disable autocomplete in CakePHP form input box

I am creating form inputs with the CakePHP Form helper and some inputs (Most of the time 'username' and 'password') are being autocompleted on create actions, login actions, etc.. this is annoying. I am guessing those are just more common so the browser is using its cookies to try to complete the inputs.
Anyways.. how do I disable this?
In my view:
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2'
));
...
What am I missing?
You can specify attributes to be sent to the form helper. Specify the attribute 'autocomplete' and set its value to 'off'.
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
...
Which results in something like this for your HTML:
<input name="data[Model][username]" autocomplete="off" class="pure-u-1-2" id="ModelUsername" type="text">
You may also do this on the whole form instead of just each input. Just specify the same attribute and value in the form create like so:
...
echo $this->Form->create('Model', array(
'class' => 'class',
'autocomplete' => 'off'
));
This will give you something like this in your HTML:
<form action=".../Model/Action" class="class" autocomplete="off" id="ModelActionForm" method="post" accept-charset="utf-8">
NOTE Several browsers will now ignore autocomplete="off" or autocomplete="false". The workaround is to place a hidden text and password field before all other inputs on your form. The browsers will fill those instead of the ones you want to leave alone.
The best solution is to use autocomplete = new-password
It works great in Chrome and Firefox
Like this:
$this->Form->input('password', array('type' => 'password', 'autocomplete' => 'new-password'));

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

Cakephp Form Labels Encoding Utf8

In my php application since the beginning that i set everything with utf8 to avoid future problems. I set my database:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'aquitex',
'prefix' => '',
'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'aquitex',
'prefix' => '',
'encoding' => 'utf8',
);
}
The file core.php:
Configure::write('App.encoding', 'UTF-8');
And the default layout of the views:
<?php echo $this->Html->charset(); ?>
However, i'm still having problems in some elements like labels of forms.
In my index.ctp file, this line:
echo $this->Html->link("Segurança", array('controller' => 'Posts','action'=> 'add'), array( 'class' => 'button'));
works perfectly and there's no problem with the 'ç' character.
But in forms, like this:
echo $this->Form->create('Post');
echo $this->Form->input('Nome Produto');
echo $this->Form->input(utf8_encode("Código Produto"));
echo $this->Form->input("Versão");
echo $this->Form->input('Data');
//echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Criar Ficha');
there's no way i can get the words on the labels of the form with 'ó" or 'ç' characters showing properly. As you can see i even tried the utf8encode() in one of them.
Any hints? Thank you!
there is no need to use utf8_encode() in your views.
you simply forgot to save the view file properly.
save it as "utf8 without bom" and you will be fine.
files that do not contain any special utf8 char can still stay as ansi (since there is no difference between them then).
but every file that does contain such a character you need to save as utf8 (even controllers and models if you plan on using utf8 characters there for error messages etc).
PS: in general it is wiser to use english and to translate it via PO file into your language.
this way you can leave the files as they are and you are more flexible (you can add new languages on the fly just by creating a new PO file then).
EDIT
After figuring out together that your inputs() use utf8 chars, I will need to update:
It is wise to use "underscore_field_names" for your db fields (and therefore your input fields) - and in English:
echo $this->Form->input("version"));
you can easily translate them via PO file afterwards or specifying the label:
echo $this->Form->input("version", array('label' => 'Versão'));
but the first way is recommended to keep it dry.
App.encoding just tells Cake to send data in UTF8. If you're using MySQL, make sure the database itself is set to utf8_general_ci collation.

Zend Form decorators trouble

How do I achieve the following with form decorators for form elements:
<dt>
<ul>
<li>The errors</li>
<li>The errors</li>
</ul>
<label>The label</label>
</dt>
<dd>
<input type="text" value="The input field">
</dd>
In other words, in stead of Errors appended after the input field, I want them prepended before the Label. I do however want to keep the <dt> and <dd> tags as illustrated above.
Alright, I found out how to do it. Gradually the decorators are starting to make sense to me:
$decorators = array(
'Label',
array( 'Errors', array( 'placement' => 'prepend' ) ),
array( array( 'dt' => 'HtmlTag' ), array( 'tag' => 'dt' ) ),
array( array( 'ddOpen' => 'HtmlTag' ), array( 'tag' => 'dd', 'openOnly' => true, 'placement' => 'append' ) ),
array( 'ViewHelper' ),
array( array( 'ddClose' => 'HtmlTag' ), array( 'tag' => 'dd', 'closeOnly' => true, 'placement' => 'append' ) )
);
What this does is the following:
First render the Label
Then prepend (default = append) the Errors
Wrap (default) all previous content in a HtmlTag (dt)
Next, append (default = wrap) a opening HtmlTag (dd)
Then append (default) the ViewHelper
Next, append (default = wrap) a closing HtmlTag (dd)
Then set the decorators:
// be sure to only set them, after you have added the relevant elements to the form
$this->setElementDecorators( $decorators );
PS:
Be aware though that my particular example produces invaliid html. ;-) I only found out later that <ul> elements are not allowed in <dt> elements with DOCTYPE HTML 4.01 strict
In your form class, try this:
$this->setElementDecorators(array(
'Errors',
'ViewHelper',
'Label',
));