Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.0

[eluser]WanWizard[/eluser]
This has been discussed a few hours ago: http://ellislab.com/forums/viewthread/196220

[eluser]Andy78[/eluser]
I'm a bit confused as to how exactly datamapper validation errors should be displayed in the in view.

I got it working as follows:
Controller:
Code:
function user_add()
    {
        if (!empty($_POST)) {
            //Create user object
            $user = new User();
            $user->username = $this->input->post('username');
            $user->password = $this->input->post('password');
            $user->confirm_password = $this->input->post('confirm_password');
            $user->email_address = $this->input->post('email');
            $user->user_type_id = $this->input->post('type');
            $user->user_status = $this->input->post('status');
            $user->validate();
            //get user types
            $user_type = new User_type();
            $user_type->get();
            //Save object and redirect
            if($user->valid){
                 if($user->save())
                    {
                        $this->session->set_flashdata('success', 'User added successfully');
                        redirect('admin/user_manager');
                    }
                }
            //display validation error messages    
            else
                {
                    $data['type'] = $user_type;
                    $data['page'] ='user_manager';
                    $data['page_sub'] = 'add_user';
                    $data['main_content'] = 'admin/add_user';
                    $data['errors'] = $user->error;    
                    $this->load->view('admin/template', $data);        
                }        
            
        }
        else{
            
             //get user types
             $user_type = new User_type();
             $user_type->get();
                
             $data['type'] = $user_type;
             $data['page'] ='user_manager';
             $data['page_sub'] = 'add_user';
             $data['main_content'] = 'admin/add_user';
             //$data['errors'] = $user->error;    
             $this->load->view('admin/template', $data);      
        }
    }

part of the View:

Code:
<table class="content-table">    
    <tr>
        <th>
        Username :
        </th>
        <td class="id-form">
          &lt;?php echo form_input('username', $this->input->post('username')); if(isset($errors)){echo $errors->username;} ?&gt;
        </td>
    </tr>
    <tr>
        <th>Password :</th>
        <td class="id-form">&lt;?php echo form_password('password', $this->input->post('password')); if(isset($errors)){echo $errors->password;} ?&gt;</td>
    </tr>
    <tr>
        <th>Confirm Password :</th>
        <td class="id-form">&lt;?php echo form_password('confirm_password', $this->input->post('confirm_password')); if(isset($errors)){ echo $errors->confirm_password;} ?&gt;</td>
    </tr>
     <tr>
        <th>Email :</th>
        <td class="id-form">&lt;?php echo form_input('email', $this->input->post('email')); if(isset($errors)){ echo $errors->email_address;} ?&gt;</td>
    </tr>
     <tr>
        <th>User type :</th>
        <td class="id-form">&lt;?php
                    foreach($type as $type){
                                    
                    $options[$type->id] = $type->user_type;
                  }
                  echo form_dropdown('type', $options);  ?&gt;
        </td>
    </tr>
     <tr>
        <th>User Status :</th>
        <td class="id-form">&lt;?php  $options = array('active'  => 'Active', 'inactive'   => 'Inactive', 'suspended' => 'Suspended');
             echo form_dropdown('status', $options); ?&gt;</td>
    </tr>
    <tr>
        <td></td>
        <td>&lt;?php echo form_submit('submit', 'submit');?&gt;</td>
    </tr>
</table>

Is there a better way of doing this as doesn't feel right having to check if the errors array is set in order to display it without errors?

[eluser]WanWizard[/eluser]
There is no set way of doing this, like there is no set of way of using CI's form validation.

Datamapper provides the validation errors as part of the object, either as an array to iterate over, directly accessable by fieldname, or as a pre-formatted string.

It it up to you how you translate this to the screen, as it will depend on your application design and your applications styling. Some have error messages as a list at the top of the content body, some besides or under every form field.

[eluser]heldrida[/eluser]
Hi,
I would like to know if there are any kind of cache / caching functions on datamapper ?

thanks for looking!

[eluser]WanWizard[/eluser]
Not built-in.

When I needed caching in my apps, I used Phil Sturgeon's caching library. See http://philsturgeon.co.uk/code/codeigniter-cache for more info.

[eluser]heldrida[/eluser]
Once again WanWizard, thanks a lot for your help!

[eluser]Maglok[/eluser]
@lars Steen: I am pretty sure that a unix_timestamp is not a int field in mysql. There is a 'timestamp' type, maybe try that?

Now I also have a question relating to automated timestamps. I got it working, it is very nice. I defined the names for my created_field and updated_field in the config.

The question: Is DataMapper supposed to not write the created and updated fields on the join tables? I would like to know when a certain person got assigned a book for example. DM doesn't seem to update my join table between user and book.

[eluser]WanWizard[/eluser]
It is perfectly fine to store a unix timestamp in an INT(11), or an INT(10) UNSIGNED. I do it all the time.

Automatically adding timestamps is a model feature. As join tables don't have models, that feature is not available. This will be addressed in 2.0, which will use models for join tables too...

[eluser]Maglok[/eluser]
Then I don't know for the timestamp Smile

I seem to be hitting things that will be introduced in 2.0. Thanks for the quick response!

[eluser]ricardocasares[/eluser]
Hi guys, I'm struggling 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 in Related validation, 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:

Code:
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 (specificly how to show the errors for this)?

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:

Code:
Users table
------------------
id   |  username  |
------------------

Code:
Products table
------------------------
id  | user_id |  name   |
------------------------

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB