2

I need to set up the validation rules to validate the related items on a specific object, ie: A user can have no more than 3 products related to it.

I believe DataMapper can check for this validation using _related_max_size rule, but I can't figure out how to use it on the $validation array in the model.

So far I've tried this in both my user and product models:

var $validation = array(
    'product' => array(
         'rules' => array('max_size' => 3)
    )
);

Can somebody show me an example on how to set up this at the model, controller and finally the view?

Edit: What I mean is, a user has many products, and can create a certain amount of them, let's say 3 products, when that amount is reached, the user can no longer create products, and this validation rule should not permit the user to create more products.

This would be the DB Schema:

Users table
------------------
id   |  username  |
------------------

Products table
------------------------
id  | user_id |  name   |
------------------------

More info here: http://codeigniter.com/forums/viewthread/178045/P500/

Thanks!

EDIT:

Ok, I got it all working now… Except, I need to do the following:

var $validation = array(
    'product' => array(
        'label' => 'productos',
        'rules' => array('required','max_size' => $products_limit)
    )
); 

The $products_limit comes from the “plan” the user has associated, and it’s stored in the session when the user logs in. When I try to run this I get:

Parse error: syntax error, unexpected T_VARIABLE in /var/www/stocker/application/models/user.php on line 11 

Is there any way to make this setting dynamic?

tereško
  • 58,060
  • 25
  • 98
  • 150
analogic.al
  • 739
  • 2
  • 11
  • 19
  • will you please explain it a little more, what exactly you want? an example of what the user will select and what you are trying to validate? – Shabib Feb 14 '12 at 08:37

3 Answers3

1

In model

var $validation = array(
    array(
        'field' => 'username',
        'label' => 'Username',
        'rules' => array('required')
    )
);

In controller. $this -> $object = new Your_model();

$object->validate();

    if ($object->valid)
    { $object->save();

        // Validation Passed
    }
    else
    { $data['error'] = $object->error;
        // Validation Failed
    }

In view.

echo $error->field_name
Didozavar
  • 62
  • 1
  • 5
  • Thank you @Didozavar, but I'm trying to validate a relationship, so in my user model, for example, I have `var $validation = array('product' => 'rules' => array('max_size' => 3));` And the thing is I need to validate how much products a user has created, before he can insert another product on the DB. So in short, what I think it's not working it's the way I save or validate the relationship. – analogic.al Feb 24 '12 at 03:31
  • Right now I'm getting this error: `Error Number: 1054 Unknown column 'products.product_id' in 'where clause' SELECT COUNT(*) AS `numrows` FROM (`products`) WHERE `products`.`user_id` = 1 AND `products`.`product_id` NOT IN (307) Filename: /var/www/stocker/libraries/datamapper.php Line Number: 2596` while trying to do this: `$u = new User(); $p = new Product(); $p->name = "PEPE"; $p->existence = 2; $p->threshold = 1; $p->measure_id = 3; $p->save(); $u->where('username','rcasares')->get(); echo $u->save($p);` – analogic.al Feb 24 '12 at 03:33
  • The product get saved, and `echo $u->save($p);` returns TRUE, when in fact this user has more than 3 products. – analogic.al Feb 24 '12 at 03:35
0

I never use Codeigniter before, but give me a chance to help you. So far I didn't found any built-in validation in Code-igniter (correct me if I'm wrong).

One workaround that I could think of is to Callback:Your own Validation Functions. Below is a snip. Pardon me if it didn't work as you want.

In Model: (create something like)

function product_limit($id)
{
    $this->db->where('product_id',$id);
    $query = $this->db->get('products');
    if ($query->num_rows() > 3){
        return true;
    }
    else{
        return false;
    }
}

In controller: (create something like)

function productkey_limit($id)
{
    $this->product_model->product_exists($id);
}

public function index()
{
    $this->form_validation->set_rules('username', 'Username', 'callback_product_limit');
}

For more information Please refer to the manual page which gives more complete. I am also new to CodeIgniter. But I hope this helps you, not complicate you.

Yeo
  • 11,416
  • 6
  • 63
  • 90
0

First, set up a custom validation rule in libraries/MY_Form_validation.php

If the file doesn't exist, create it.

Contents of MY_Form_validation.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
    function __construct($config = array())
    {   
        parent::__construct($config);
    }

    function valid_num_products()
    {
       //Perhaps it would be better to store a maxProducts column in your users table. That way, every user can have a different max products? (just a thought). For now, let's be static.
       $maxProducts = 3;

       //The $this object is not available in libraries, you must request an instance of CI then, $this will be known as $CI...Yes the ampersand is correct, you want it by reference because it's huge.
       $CI =& get_instance();

       //Assumptions: You have stored logged in user details in the global data array & You have installed DataMapper + Set up your Product and User models.
       $p = new Product();
       $count = $p->where('user_id', $CI->data['user']['id'])->count();
       if($count>=$maxProducts) return false;
       else return true;
    }
}

Next, set up your rule in config/form_validation.php.

Contents of form_validation.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array
(   
    'addProduct' => array
    (
        array
        (
                'field' => 'name',
                'label' => 'Product Name',
                'rules' => 'required|valid_num_products'
        )
    )
);

Next, set up your error message in language/english/form_validation_lang.php. Add the following line:

$lang['valid_num_products'] = "Sorry, you have exceeded your maximum number of allowable products.";

Now in the Controller, you'll want something along the lines of:

class Products extends MY_In_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    function add()
    {
        $p = $this->input->post();
        //was there even a post to the server?
        if($p){
            //yes there was a post to the server. run form validation.
            if($this->form_validation->run('addProduct')){
                //it's safe to add. grab the user, create the product and save the relationship.
                $u = new User($this->data['user']['id']);
                $x = new Product();
                $x->name = $p['name'];
                $x->save($u);
            }
            else{
                //there was an error. should print the error message we wrote above.
                echo validation_errors();
            }
        }
    }
}

Finally, you might wonder why I've inherited from MY_In_Controller. There is an excellent article written by Phil Sturgeon over on his blog entitled Keeping It Dry. In the post he explains how to write controllers that inherit from access-controlling Controllers. By using this paradigm, controllers that inherit from MY_In_Controller can be assumed to be logged in, and the $this->data['user']['id'] stuff is therefore assumed to be available. In fact, $this->data['user']['id'] is SET in MY_In_Controller. This helps you seperate your logic in such a way that you're not checking for logged in status in the constructors of your controllers, or (even worse) in the functions of them.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96