Validating user data

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Validating user data

Bjarte Kalstveit Vebjørnsen
Hello I'm new to the list.

I've been playing around with the framework a bit especially
Controller,View and Zend_Db_Table. What I'm missing is an easy way to
validate data before I update my tables, without duplicating code in
my controller. Like you can with the PEAR pacakages, db_table (through
quickform) or db_dataobject (alone or through formbuilder).

I.e I could do this:
require_once 'Zend/Filter.php';

class UserTable extends Zend_Db_Table
{
    public function validateFirstname($value)
    {
        return Zend_Filter::isName($value);
    }
}

$table = new UserTable();
$user = $table->fetchNew();
$user->setFromArray(array('firstname'=>'test342'));
if($user->validate()) {
 $user->save();
}

//and i could have this in the view (assuming i add $user to the view)
if(!$this->user->isValid('firstname')) {
 echo "Firstname was not valid";
}

I solved this by subclassing both table and row, and made table return
my custom row in the fetchNew and fetchRow methods. Then shamelessly
copied the validate method from db_dataobject into my custom row
class.

Now I'm wondering, is there plans to implement something like this, or
are there better ways to do this ? :)
Reply | Threaded
Open this post in threaded view
|

Re: Validating user data

Art Hundiak
The good news is that several people have requested the ability to specify
the class of the returned row or rowset.
http://framework.zend.com/developer/ticket/26

It's trivial to implement so hopefully it will make it into the next release.

As far as actual validation goes, there are rumors about replacing the
entire DB_Table system with some kind of active row stuff.  No idea what
the status is or if it will have validation.

Validation can be tricky.  You might consider putting the validation code
into it's own class just to avoid cluttering up the row class.  Maybe we
need a DB_Table_Row_Validate class to extend from.

>
> Hello I'm new to the list.
>
> I've been playing around with the framework a bit especially
> Controller,View and Zend_Db_Table. What I'm missing is an easy way to
> validate data before I update my tables, without duplicating code in
> my controller. Like you can with the PEAR pacakages, db_table (through
> quickform) or db_dataobject (alone or through formbuilder).
>
> I.e I could do this:
> require_once 'Zend/Filter.php';
>
> class UserTable extends Zend_Db_Table
> {
>     public function validateFirstname($value)
>     {
>         return Zend_Filter::isName($value);
>     }
> }
>
> $table = new UserTable();
> $user = $table->fetchNew();
> $user->setFromArray(array('firstname'=>'test342'));
> if($user->validate()) {
>  $user->save();
> }
>
> //and i could have this in the view (assuming i add $user to the view)
> if(!$this->user->isValid('firstname')) {
>  echo "Firstname was not valid";
> }
>
> I solved this by subclassing both table and row, and made table return
> my custom row in the fetchNew and fetchRow methods. Then shamelessly
> copied the validate method from db_dataobject into my custom row
> class.
>
> Now I'm wondering, is there plans to implement something like this, or
> are there better ways to do this ? :)
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Validating user data

Bjarte Kalstveit Vebjørnsen
Hmm it might just be silly to dig too much into it then.

With validation code what do you mean exactly? The validate() method
and the isValid($col) method? It could be in another class but I'd
still like to be able to call validate on the row object (or the
active record object). To avoid much extra code.
How do you see usage of a DB_Table_Row_Validate class?

On 5/17/06, Art Hundiak <[hidden email]> wrote:

> The good news is that several people have requested the ability to specify
> the class of the returned row or rowset.
> http://framework.zend.com/developer/ticket/26
>
> It's trivial to implement so hopefully it will make it into the next release.
>
> As far as actual validation goes, there are rumors about replacing the
> entire DB_Table system with some kind of active row stuff.  No idea what
> the status is or if it will have validation.
>
> Validation can be tricky.  You might consider putting the validation code
> into it's own class just to avoid cluttering up the row class.  Maybe we
> need a DB_Table_Row_Validate class to extend from.
>