having problems with Zend framework validators - zend-framework

I'm having problem with custom builded validator that does not returns any error. I copied a file NotEmpty.php form folder library/Zend/Validate, rename class Zend_Validate_NotEmpty to My_Validate_EmailConfirmation and put it into folder My/Validate.
If I call this class like
->setRequired(true)->addValidator('NotEmpty',true,array('messages' => array('isEmpty' => "bla")));
I get the correct error, but if i call it like
->setRequired(true)->addValidator('EmailConfirmation',true,array('mess ages' => array('isEmpty' => "bla")))->addPrefixPath('My_Validate','My/Validate/','validate');
i get nothing...
What am i doing wrong?
Thanks is advanced for your answers...

Have you tried to set in your bootstrap file your new namespace?
Zend_Loader_Autoloader::getInstance()->registerNamespace('My');
Also, why not you just extends the NotEmpty validator instead of duplicate the class?

Related

Cakephp /Config/email.php into a Plugin

Hi i have a plugin called Contact and into it i have
/Config/email.php file.
Cake seems not to load that file.
In my main bootstrap.php file i tried this:
CakePlugin::loadAll(array('Contact'=>array('bootstrap'=>true, 'email'=>true, 'routes'=>true)));
the bootstrap.php and routes.php file are loaded, the email.php no
Thanks
That's not how CakePlugin::load/loadAll() works, there is no email option, only bootstrap, routes and ignoreMissing.
Check the coobook and the API documentation
http://book.cakephp.org/2.0/en/plugins.html#advanced-bootstrapping
http://api.cakephp.org/2.4/class-CakePlugin.html#_load
If you like to load more than one bootstrap file for a plugin. You can specify an array of files for the bootstrap configuration key...
So something like this should work for you:
CakePlugin::loadAll(array(
'Contact' => array(
'bootstrap' => array(
'bootstrap',
'email'
),
'routes'=>true
)
));
That would load the files /Plugin/Contact/Config/bootstrap.php and /Plugin/Contact/Config/email.php.
However it won't work in case that file contains an EmailConfig class definition and your app also loads the app/Config/email.php file where such a class definition already exists. In that case you should choose another way to define your email configuration settings.

Zend Framework 2 Override an existing Service?

I am using a zf2 module called GoalioRememberMe and now I want to override its service by my customized service. Or if it is not possible, I want to override the Module.php with my config. Is it possible?
In the Application module. I wrote this line in module.config.php:
'GoalioRememberMe\Service\RememberMe' => 'Application\Service\RememberMe'
Thanks in advance!
This is exactly the reason it is recommended to name the service as the type of the object that is returned. The object GoalioRememberMe\Service\RememberMe is named goaliorememberme_rememberme_service in the service manager. You can check that here.
So the solution is simple, instead of this:
'GoalioRememberMe\Service\RememberMe' => 'Application\Service\RememberMe'
Write this
'goaliorememberme_rememberme_service' => 'Application\Service\RememberMe'
As Jurian said, the service name is goaliorememberme_rememberme_service and it has been set in the getServiceConfig() method. So I wrote this code in the Module.php file in the Application Module:
$serviceManager->
setAllowOverride(true)->
setInvokableClass('goaliorememberme_rememberme_service', 'Application\Service\CustomRememberMe')->
setAllowOverride(false);
And it replaced successfully with my customized service!
Thanks very much to Jurian for the big help!
Actually the service manager first runs a method "canonicalizeName()" which "normalizes" the names as follows:
All _ / \ and - are stripped out
The key is made lowercase
Thus both "GoalioRememberMe\Service\RememberMe" and "goaliorememberme_rememberme_service" become "goalioremembermeremembermeservice" (i.e. they're both the same), thus the error message.
The quickest way to override an existing service is to create a *local.php or *global.php file in the /config/autoload folder. (That folder is identified in config/application.config.php.) Any override files in this folder are process after modules are loaded. If you have duplicate service manager keys, the last one wins.

How can I access custom validators globally?

I created a my own validation class under /library/My/Validate/
In my form I have $this->addElementPrefixPath('My_Validate', 'My/Validate', 'validate');
I am using my validator like so:
$this->addElement('text', 'aField', array(
'validators' => array(
array('TestValidator', false, array('messages' => 'test failed')
),
));
This all works. However, I am interested in improving this in two ways.
I would like to make it so that all forms have access to my validator. Calling addElementPrefixPath() in every form doesn't seem to be a clean way of doing this.
I would like to pass in My_Validate_TestValidator instead of TestValidator so other developers know what they are working with right away.
To answer your first question, the only real easy way to do this would be to create your own instance of the form - My_Form_Abstract - which has an init() method that sets the prefix path - and then of course calls the parent init().
I'm not aware of a way to make your second method work flawlessly. You need to store a prefix in order to build the validator loader correctly. However, as an alternative, you might try creating new instances of the class using the full name, and then adding it to the element:
$element = $this->getElement('aField');
$myValidateTestValidator = new My_Validate_TestValidator();
$element->addValidator($myValidateTestValidator);

Zend framework Fatal error: Class 'Application_Model_DbTable_Albums' not found in

I am trying to learn Zend framework from "Getting Started with Zend Framework" By Rob Allen. I have used the same example that has been given, but getting the error -
Fatal error: Class 'Application_Model_DbTable_Albums' not found in /var/www/html/workbench/sreekantk/zf-tutorial/application/controllers/IndexController.php on line 14 .
I think I have to set path to models folder, but don't know how to do it. Could anyone please help me out of this.
This is my Bootstrap.php file.
// application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/application/modules'));
return $autoloader;
}
}
Thanks Just H. It worked. Actually I changed the folder structure and after the again added appnamespace="Application" to the application.ini file. Thanks you all for your comments.
As long as you get to the controller your primary setup seems to be fine. So, if you have the class in a separate file the problem is probably a simple typo somewhere.
a) with all the following, look out for lower/upper case
b) note that the models folder is plural whereas the class is Model singular
c) make sure the class is named Application_Model_DbTable_Albums
d) make sure the file is named Albums.php and in a folder named application/models/DbTable
Good luck learning ZF
Since version 1.9.2, the default module will automatically initialise an autoloader for the namespace configured in appnamespace (defaults to "Application" on a vanilla install). You can remove your _initAutoload() method.
So long as your class exists in application/models/DbTable/Albums.php and is named Application_Model_DbTable_Albums, it should be able to autoload the class on first use.
Be mindful of path case sensitivity.
I'm following the same tutorial and what Adrian World said on Aug 9'11 at 13:26 helped me get rid of the error. My Bootstrap now is:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/application/models'));
return $autoloader;
}
}
Where the only thing that changed was going from modules to models
You should define Bootstrap class of the current Module. Then it will be fine.

Can I do more than one form for the same model class in symfony?

Well, imagine that we have a register form of a class Customer and we only ask three fields (name,surname,email) and after, when this user logged first time we want to complete this information.
First, we have in lib/form/doctrine a file called 'CustomerForm.class.php' wich is generated automatic on command line. In this file we 'setup' only 3 fields and validators and if we wanna use we do something like that:
$this->form = CustomerForm();
Second, we create manual another form named 'CustomerFormStep1.class.php' where we can setup for validate the other fields. But when we do..
$this->form = CustomerFormStep1();
it returns error: Fatal error: Class 'CustomerFormStep1' not found
What is wrong?
Thanks.
Assuming you have the form defined as:
class CustomerFormStep1 extends sfForm
or similar (sfFormDoctrine etc), and named correctly like you say (CustomerFormStep1.class.php) and in lib/form, then Symfony should just pick the definition up fine. Did you clear the cache after creating and placing it in the right place? (symfony cc).
Create the new CustomerFormStep1 class as #richsage instructed. Then, in your actions you can write something like:
public function executeLogin(){
//before login
$this->form = new CustomerForm();
}
public function executeLoggedIn(){
$this->form = new CustomerFormStep1();
//other steps
}
Haven't you read the tutorial? Extending forms is perfectly described in context with reh admin generator and can of course be applied to any case.