Zend Validator location - zend-framework

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.

Related

Zend Framework set on bootstrap.php custom locale

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

Zend_Form submission > Resource 'controller::action' not found

I'm taking over a site that's gone through several developers. The site is using Zend version 1.12.0, according to Zend_Version::VERSION, which is a new framework for me. On the site, there's a form class called App_Form_Customers_Edit, which extends Zend_Form. The form's action is /customers/edit, and when submitted, the method editAction of CustomersController is executed.
So, to create a new form, I created a new class App_Form_Customers_EditAddress in the same directory as App_Form_Customers_Edit, and set it's action to /customers/editaddress, created a function called editaddressAction in the CustomersController class and tested the form.
But I get an error saying "Resource 'customers::editaddress' not found"
The form itself is displaying properly, and as far as I can tell I'm using the exact same pattern as the other form which works, and apart from not using the zf command, the same method prescribed here in the Zend documentation: http://framework.zend.com/manual/1.12/en/learning.quickstart.create-form.html
What do I need to do to get my new form working? Do I need to update .zfproject.xml? I can't see anything in there that's related to the working form.
Here's the code for App_Form_Customers_Edit:
class App_Form_Customers_Edit extends Zend_Form
{
public function init ()
{
$this->addPrefixPath('App_Form', 'App/Form/');
$this->setMethod('post');
// ... The rest is just calls to $this->addElement
}
}
And for EditAddress:
class App_Form_Customers_EditAddress extends Zend_Form
{
public function init ()
{
$this->addPrefixPath('App_Form', 'App/Form/')
->setMethod('post')
->setAction('/customers/editaddress');
$this->addElement('submit', 'active', ['value' => 'Activate']);
$this->addElement('submit', 'remove', ['value' => 'Remove']);
$this->addElement('hidden', 'id');
}
}
Check for acl declarations. If you are using acl and you have not declared rules for the action, you might get this type of error.
Best guess:
Your former developer has implemented a custom route somewhere. Probably in the application.ini or the boostrap.php. This custom route is looking for specific urls and /customers/edit conforms to a valid route but /customers/editaddress does not.
I think this is likely because your error is a missing resource rather then a 'page not found' or a missing controller or missing action message. So it seems as though the router is trying to match an invalid resource to a valid route.
Good Luck

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 framework : Cannot access database configuration through application.ini

I am new to Zend. The problem i am getting is that i am not able to access resources.db.* configurations from application.ini
The way i am using to access is
$application->getOptions()
. It does not shows the resource.db.* properties.
Can any one help me ?
Do this instead
$params = Zend_Db_Table::getDefaultAdapter()->getConfig(); //return associative array
To do it the way you asked about, the easiest way I've found is to put everything in the registry during bootstrap:
//bootstrap.php
public function _initConfig {
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
to use these configs elsewhere in your application:
$db = Zend_Registry::get('config')->resources->db;
although if you are just trying to access the adapter registered in the application.ini:
$db = Zend_Db_Table::getDefaultAdapter();

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.