Zend Framework set on bootstrap.php custom locale - zend-framework

how to set custom locale in zend framework by create a function in bootstrap file? I need to change error messege in zend form.
Thanks for support

You can use this code to set the locale
// within your bootstrap
$locale = new Zend_Locale('de_AT');
Zend_Registry::set('Zend_Locale', $locale);
// within your model or controller
$date = new Zend_Date();
print $date->getLocale();
echo $date->getDate();
More info on the Zend_Locale
For adding translated error message to the form validators you have to add them to the Zend_Validate_Abstract
From the ZF manual
Zend Framework is shipped with more than 45 different validators with
more than 200 failure messages. It can be a tedious task to translate
all of these messages. But for your convenience Zend Framework comes
with already pre-translated validation messages. You can find them
within the path /resources/languages in your Zend Framework
installation. So to translate all validation messages to German for
example, all you have to do is to attach a translator to Zend_Validate
using these resource files.
$translator = new Zend_Translate(
'array',
'/resources/languages',
$language,
array('scan' => Zend_Locale::LOCALE_DIRECTORY)
);
Zend_Validate_Abstract::setDefaultTranslator($translator);
More info on the validator error message is in their manual Using pre-translated validation messages
I havent personally used the translation adaptors, but from the manual Creating CSV source files for Translation it seems the below code will add the translator to the application
$translate = new Zend_Translate(
array(
'adapter' => 'csv',
'content' => '/path/to/mytranslation.csv',
'locale' => 'de'
)
);
Also a sample of the .csv format
message1;Nachricht1
message2;Nachricht2

Related

Laravel 4.1, call error message for specific validation point of one input / possible?

I have a question concerning the Laravel 4.1 validators.
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users')
);
Is it possible to call a specific validation error for the case when the entered email is not unique? Reading the docs I only saw that one is able to define the error message if the validation for 'email' fails. However, if someone enters an email address but this one is already in the database it would be awesome to show the user exactly that he passed "required" but did not pass "unique".
The third parameter of Validator::make() lets you pass in an array of messages. More on this here: http://laravel.com/docs/validation#custom-error-messages
As it says in the above link, you can specify field-and-rule-specific messages by using the dot syntax email.unique. In your case this would be:
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users'),
array('email.unique' => 'This email is already being used by another user.'),
);

Created a New Zend Controller but can't access when Specified in Layout?

I created a New Controller with:
zf create controller email
but the resulting files weren't put in application/modules, they were put in application/...
Anyway I moved the controller and view files into the correct directory
then I used:
EMAIL
After I refresh the page it gives a internal Error 500
Don't Really know what's cutting -> it's Zend Version: 1.12.1
$this->url() view helper works by matching the parameters given and the route name, to create a URI.
In order for your code to work, you have to:
1 - In your router file, specify the route, for example:
$router->addRoute(
'email',
new Zend_Controller_Router_Route_Regex('([\w\d\.\_]+)#([\w\d\.]+)([\s]*)',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array(
1 => 'username',
2 => 'host'
),
'%s#%s'
)
);
The first param for addRoute is the name of the route and that's what you use as the second parameter for $this->url(). The second parameter is a Router adapter. There are many different adapters (check the manual), this one is the regex adapter.
First param - the regex string for checking the uri. You will want to modify this to a more e-mail wise regex, but for now this should be enough.
The second param is your defaults.
The third - variables. For each group in your regex you should specify a name, so the group before the # will become 1 => 'username' and after - 2 => 'host'.
And finally the fourth param - very important - the reversed route. This one is actually the one Zend uses to glue the pieces. You create it by changing each group to a "symbol", so the whole thing becomes "%s#%s" - you'll have to read some articles on how to create those - I just go with %d for digits only, %w for letters only and %s for mixed, didn't ever need anything more.
2 - After you setup the router, go back to your view file and modify the function so it looks like this:
$this->url(array('username' => 'test', 'host' => 'test.com'), 'email');
Your resulting html should look like this:
EMAIL
3 - Turn on error_reporting, either in your server configuration or the zend application ini file. You'll have a much greater insight into what's actually malfunctioning.
I am using the configs/application.ini file to configure and bootstrap:
So adding the following lines to the file works!
resources.router.routes.email.route = /email
resources.router.routes.email.defaults.module = default
resources.router.routes.email.defaults.controller = email
resources.router.routes.email.defaults.action = index
So the routes apparently need to be explicitly declared and are not handled by zf tool

Zend Routes translate URL's

1) I have a controller "calendar" and have action "showDate" which gets date via url. So, url is something like "calendar/show-date/date/2012-07-22"
2) I have a link to display all entries, "calendar/"
So, I want to create routes so my links look like "kalendar/2012-07-22" and "kalendar/".
Can anyone help me?
According to this post:
http://www.z-f.fr/forum/viewtopic.php?id=5138
The solution is to add '#locale' => $lang to the params.
$this->url(array('lang'=>'it','#locale'=>'it'))
It works very well for me.
I've been looking into translating the URL with Zend_Translate and I came across this sites' plugin that attempts to auto-translate URL segments (module/controller/action).
http://blog.helmich.cz/305-howto-simple-multilingual-routes-in-zend-framework/
The nice thing is that it's a modified custom router class that can function similar to Zend_Router so it's relatively familiar off the bat.
$pages = new MyApp_Controller_Router_Route(
':locale/:#controller/:#action/*',
array(
'controller' =>; 'index',
'action' => 'index',
'locale' => 'cs'
)
);
$router->addRoute('pages',$pages);
The thing you'll need is to have a language ID in your URL (called :locale in the above example) so your Zend_Translate can set the proper language.
www.example.com/en/calendar/2012-06-22/
www.example.com/fr/calendrier/2012-06-22/
www.example.com/de/kalender/2012-06-22/
www.example.com/it/calendario/2012-06-22/
I've only slightly played around with this concept but I recall that it had promise. You'll have to get more familiar with Zend_Translate: http://framework.zend.com/manual/en/zend.translate.html
I hope that helps!
Cheers!
You could re-route all calls of calendar to kalendar. There are two possibilites, either you do it with Zend (preferable) or you change your webserver configuration to rewrite calls to calendar with a HTTP 302 (ugly).
You should however consult the official Zend Documentation, which is pretty good
You have to setup custom routes, this is my way:
in folder application/configs/ create file named "routes.ini"
Put in file your route:
;index-homepage, parameter date isn't required
;"index" is key of your route
routes.index.route = "kalendar/:date"
routes.index.defaults.controller = calendar
routes.index.defaults.action = show
routes.index.defaults.date =
So in your bootstrap.php define that config file:
protected function _initRoute() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addDefaultRoutes();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router->addConfig($config, 'routes');
}
And that's it, you can call URL
www.website.com/kalendar
and
www.website.com/kalendar/2012-1-1
See answers in this question for details:
Simple rewrites in Zend Framework

Zend Validator location

Do I need to save my custom Zend Validator location to "Zend/Validate"? I'd rather create a folder for all my custom validation scripts, but cannot find anything in the Zend documentation other than changing the namespace.
Here is my error message.
"Plugin by name 'UserNameValidate' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/"
I'd like to tell it to search additional paths.
I was able to solve my problem using
addElementPrefixPath('Application_Validate',
'../application/validate',
'validate');
Did you check the API docs? Zend_Validate has an addDefaultNamespaces method...
public static function addDefaultNamespaces($namespace){
I usually keep my custom validators (e.g. My_Validate_Age) in APPLICATION_PATH/validators named, e.g. . In this case the php file would be: APPLICATION_PATH/validators/Age.php. With this setup I need to add the validators path to the Zend_Autoloader. For this purpose in the Bootstrap.php I have:
protected function _initAutoload() {
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');
$autoLoader->pushAutoloader($resourceLoader);
}
Hope this will be of help to you.

zend framework custom validation classes

I'm writing a custom validator that is going to check for the existence of an email such that if it already exists in the database, the form is not valid. I'm having a hard time figuring out helper paths and namespaces for custom Zend_Validation classes.
I'd like to call the class My_Validate_EmailUnique but I keep getting error messages such as:
there is an error exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'My_Validate_EmailUnique' was not found in the registry; used paths: My_Validate_: /var/www/site/arc/helpers/
The class looks like this:
<?php
class My_Validate_EmailUnique extends Zend_Validate_Abstract
{
const EMAIL_UNIQUE = 'notMatch';
Can someone help me with where I register for the Zend_Form to look for custom validators?
You can directly add the validator to your form element. There is no need to write a custom validator for this.
In your form:
$element->addValidator(new Zend_Validate_Db_NoRecordExists('mytablename', 'myemailcolumnname'));
+1 for Db_NoRecordExists - the docs have an example showing exactly what you want to do.
Otherwise, custom validators can be loaded like regular library classes, so try placing it on your include path.
/library/My/Validate/EmailUnique.php
You can also add a new entry to the Zend_Application_Module_Autoloader if you want to keep it in your application folder, as opposed to your library:
// Bootstrap.php
protected function _initAutoloader()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'My_',
'basePath' => dirname(__FILE__),
));
$autoloader->addResourceType('validator', 'validators', 'Validate')
return $autoloader;
}
And put the class My_Validate_EmailUnique in:
/application/validators/EmailUnique.php
I've never registered a custom validator as a plugin before, so I can't help you there sorry.