Zend custom form validator 'NOT FOUND' - zend-framework

I am stuck in the follow situation. To check a url with zend_form, I have to add a custom validator. I try to add the custom validator named 'IsUrl.php' in;
What I do now
I add IsUrl.php to;
Library/Lib/Validate/
In my boodstrap:
protected function _initLibAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Lib',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
Test in Controller by;
$test = new Zend_Validate();
$test = new Lib_Validate_IsUrl();
Fatal error;
Fatal error: Class 'Lib_Validate_IsUrl' not found in
Thanks in advice.
With kind regards,
Nick

You will have to tell ZF, that you have custom validators :) You could adjust your bootstrap like this:
protected function _initValidators () {
$autoloader = new Zend_Application_Module_Autoloader (array ('namespace' => '', 'basePath' => APPLICATION_PATH));
$autoloader->addResourceType ('Validator', 'validators', 'Validator_');
}

Related

Zend: Using forms in modules

I'm trying to use forms with modules, they should be stored inside the module. So at first my filestructure:
application/
(...other directories)
modules/
group/
controllers/
IndexController.php
(...controllers)
forms/
Create.php
views/
scripts/
(...view scripts)
Bootstrap.php
Within the IndexController, I'm trying to set the Form by
new Group_Form_Create()
and the class in Create.php is of course Group_Form_Create. I get the following error message:
Fatal error: Class 'Group_Form_Create' not found in (...)\application\modules\group\controllers\IndexController.php on line 380
The Bootstrap.php with the class Group_Bootstrap is just an empty class.
Actually, I'm using the default Zend structure, but it woun't work anyway. Any ideas wheres the problems or what could be a possible solution?
In my module bootstrap (APPLICATION_PATH/modules/group/Bootstrap.php), if use the following code:
//Loads the autoloader resources
$this->_moduleName = 'group';
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH ."/modules/".$this->_moduleName."/",
'namespace' => '',
'resourceTypes' => array(
//Tells the application where to find the forms
'form' => array(
'path' => 'forms/',
'namespace' => ucfirst($this->_moduleName).'_Form_'
),
//Tells the application where to find the models
'model' => array(
'path' => 'models/',
'namespace' => ucfirst($this->_moduleName).'_Model_'
)
)
));
I then call the forms or models like this:
$frm = new Group_Form_Create();
I use the same snippet in all my modules and I only change the value of the $this->_moduleName; each time.
Hope this helps !
It sounds like your module bootstraps are not being run. These are triggered by the module resource, which is loaded if you have:
resources.modules[] = ""
in your application.ini. So add this if it is not present.
Ideally, it should work out of box.
Add this in your bootstrap:
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Group_',
'basePath' => dirname(__FILE__),
));
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
return $autoloader;
}

How mvc works in Zend framework

Thanks for previous replies..
I am trying to print Hello_world using zend framework. I wrote php file in model folder and return string value as a "Hello_world". In controller i access the value of PHP like this
$value = new TextReturner();
$this->view->setValue = $value->hello_world(); . i dont know how to access the value from controller to the view php file. I am new to zend framework. I already go through the outline structure of zend framework, i dont know how to access through codings. If anyone have idea of how to print hello_world through MVC pls guide me.
You are trying to use class $value = new TextReturner(); but your controller doesn't see that class.
Set this in your Bootstrap file that will help:
protected function _initAutoLoad() {
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
),
)
);
return $resourceLoader;
}
This will be autoload all of your model class.
in view you can access your variable like this:
<?php echo $this->setValue;?>

Zend Framework - Where should we place our custom Validators?

We can read here how to write:
http://framework.zend.com/manual/en/zend.validate.writing_validators.html
class MyValid_Float extends Zend_Validate_Abstract
{
1)
Where should we place this?
application/default/validators ?
application/view/helpers/... ?
2)
Do we have to register this somewhere on our application ?
Update:
Here's an example of my bootstrap:
include_once 'config_root.php';
set_include_path ( $PATH );
require_once 'Initializer.php';
require_once "Zend/Loader.php";
require_once 'Zend/Loader/Autoloader.php';
// Set up autoload.
$loader = Zend_Loader_Autoloader::getInstance ();
$loader->setFallbackAutoloader ( true );
$loader->suppressNotFoundWarnings ( false );
// Prepare the front controller.
$frontController = Zend_Controller_Front::getInstance ();
$frontController->throwExceptions(true);
$frontController->registerPlugin ( new Initializer ( PROJECT_ENV ) );
// Dispatch the request using the front controller.
try {
$frontController->dispatch ();
} catch ( Exception $exp ) {
$contentType = "text/html";
header ( "Content-Type: $contentType; charset=UTF-8" );
echo "an unexpected error occurred.";
echo "<h2>Unexpected Exception: " . $exp->getMessage () . "</h2><br /><pre>";
echo $exp->getTraceAsString ();
}
SO, do I have to add here:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');
And then I should create a file IN: (note that this configuration is using default module):
application/default/validators/ValidateSpam.php
And on validateSpam.php have something like:
class My_Validate_Spam extends Zend_Validate_Abstract {
Can you please confirm ?
Thanks
Place your application/validators
then in your application's Bootstrap class, add the following function:
protected function _initAutoload () {
// configure new autoloader
$autoloader = new Zend_Application_Module_Autoloader (array ('namespace' => '', 'basePath' => APPLICATION_PATH));
// autoload validators definition
$autoloader->addResourceType ('Validator', 'validators', 'Validator_');
}
More detail(s) about Zend Bootstrap Autoloading.
Another way is described in this blog, where the constructor of the controller for the form that is using this custom validator has an extra line:
class JD_Form_Controller extends Zend_Form
{
public function __construct($options = null)
{
// path setting for custom classes MUST ALWAYS be first!
$this->addElementPrefixPath('JD_Form_Validator','JD/Form/Validator','validate');
...
}
...
}
I do it by adding the following line to application.ini :-
autoloadernamespaces[] = "App_"
Then I put my custom validators in (for example) /library/App/Validate/MyCustomValidator.php.
I can then write my validator using something like:-
class App_Validate_MyCustomValidator() extends Zend_Validate_Abstract
It works pretty well for me and is simple and easy to implement.

Model class is not found in zend framework project (quickstart)

The things I did is
zf create project demo1 in command prompt
add the lines to application.ini
appnamespace = "Application"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
add a layout with header and footer using partial() (They are perfectly worked)
create Data.php in models directory and add this simple class
<?php class Application_Model_Data{ }//Application for appnamespace
then I tried to load this class(by creating instance)from index controller index action
$data = new Application_Model_Data();
but when I test it even in this level it gives an error
Fatal error: Class 'Application_Model_Data' not found in C:\Zend\...\IndexController.php
Question
Do I want to add a autoloader to
load models in the application( I'm not used modules)
if not what was I missed to add
please help I'm stuck in the beginning,Thank you
this should work!!
add this function to bootstrap:
protected function _initResourceAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
));
$autoloader->addResourceType( 'model', 'models', 'Model');
return $autoloader;
}
You need to setup a resource Autoloader in your Bootstrap, something like this:
protected function _initResourceAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'path/to/application/directory',
'namespace' => 'Application_',
));
return $autoloader;
}
With that, Zend can load the modules in your application, and just not models, but DbTable, Forms, Plugins, etc.
write the following in your bootstrap file:
protected function _initDefaultModuleAutoloader()
{
$resourceLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH,
));
return $resourceLoader;
}
in your models folder create new file and name it "Data.php"
in the Data.php declare the class like this:
class Model_Data extends Zend_Db_Table_Abstract {.....}
you can now instantiate your data model like so:
$data = new Model_Data();
good luck :-)
in your application ini you should have
autoloadernamespaces.0 = 'Application' instead of appnamespace
then your model would be in
/library/Application/Model/Data.php
but why dont you use the default "models" folder in the suggested application structure.
I had forgotten to add the file extension .php to the file, just in case someone else makes the same mistake

Zend Framework autoloading classes extended by models

I'm new to zend framework so maybe this question is stupid..
I've got a default hierarchy
site
|--bootstrap.php
|--application
|--models
|-- Item.php
|-- ModelAbstract.php
|--...
Inside Item.php there's
<?php
//TODO: trying to remove this require...
require_once('ModelAbstract.php');
class CF_Model_Flower extends CF_Model_Abstract
{
...
Inside 'ModelAbstract.php' there's
<?php
class CF_Model_Abstract
{
...
And my application Bootstrap.php looks like
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'CF',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
...
If I removed the 'require_once' inside Item.php I get
Fatal error: Class 'CF_Model_Abstract' not found in /Mysite/application/models/Item.php on line 6
Why ? And how can I use autoloading to live without this require_once ?
In fact, renaming 'ModelAbstract.php' to 'Abstract.php' works. Can someone explain me why ?
Thx
I'm not familiar with Zend_Application_Module_Autoloader and such. But if they work anything like earlier ZF autoloading mechanisms, the autoloader will look for class CF_Model_Abstract in:
CF/Model/Abstract.php
or maybe with this namespace/basePath configuration in:
models/Model/Abstract.php
or:
models/CF/Model/Abstract.php
but probably not in:
models/ModelAbstract.php
So in other words, the underscores represent child directories.
Try specifying the _ in the namespace.
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'CF_',
'basePath' => dirname(__FILE__),
));
Put the following in your bootstrap file:
protected function _initAutoLoad()
{
$loader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'CF',
));
$loader->addResourceType('form', 'forms', 'Form')
->addResourceType('model', 'models', 'Model')
->addResourceType('mapper', 'models/mappers', 'Model_Mapper')
->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');
return $loader;
}
This will load models, forms, dbtables, and mappers for you. Hope this does the trick.