Checking the Zend Model

I am working on a Zend application, but I don’t have much experience with Zend, so I simply rely on my RubyOnRails experience.

From the articles in pairs, I found that most of the validation is done at the Forms level - with Zend_Form. But for me it looks a little strange - how about checking at the model level - create a model based on the form data, and run smth, like $model->isValid(); this makes sense as I create some models without post post requests. p>

My current model stream:

 ProductMapper extends Zend_Db_Table Product extends Zend_Db_Table_Row 

And given that I'm doing something like

 $mapper = new ProductMapper(); $product => $mapper->find(...); // Do some staff with this model // And want to check if it valid before saving if ($product.isValid()) { $product.save(); // Zend_Db_Table_Row method } else { ... } 

I understand that I can just check with RegExp inside the isValid method, but I would like to use already implemented methods from Zend_Form , for example addValidator , addFilter and all that is useful.

Is this also the right way to manage models in Zend?

Any help or suggestions would be greatly appreciated!

+4
source share
3 answers

You can use the same filters and / or validators as Zend_Form, but like this

 $validator = new Zend_Validate_Allnum(); if ($validator->isValid($data) do some code 

or

 $filter = new Zend_Filter_StringTrim(); $filteredVal = $filter->filter($val); 

so that you can create your own isValid () method in a string class where you can execute your own validation and filtering logic

+2
source

Models in Zend are not only representations of Db objects, most objects actually have nothing to do with the database and are models for business logic. In this context, having a global validation method does not make sense. Zend_Db_Table and Row will do some checks for you when playing with the db object, for example, checking what is the main key, but if you want something like Active Record, you have to distribute these classes yourself.

+1
source

I do not agree that db-layer (ZF objects Table, Row) is the right place to check. IHMO validation is application logic or business constraint.

The verification process should be placed in a specific model. The database level should be clean and only responsible for the simple management of the database (insert, update, delete, select).

Your model needs to know which attributes are needed (or attribute data type), so there is a right place to check.

As Elzo said - some models do not represent database objects - so you must make the interface IPersistenceable , which has one Validate method for database-driven models. These models must implement their own verification algorithm.

This approach is useful - each model can have a different way of checking. The next approach is that you can create an abstract class with basic validation of the primary key + verification method, and each stable model extends this class.

0
source

All Articles