CodeIgniter Forums

Full Version: Editing unique data post input.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using the set rules for the form validation.
All is okay but when editing a page it says the slug is taken {of course it is, it belongs to the post I'm editing} but I need it there still obviously because I'm trying to prevent it being shared with another. Anybody have any suggestions as a work around?

PHP Code:
public function set_rules()
    {
        
$config = array(
            array(
                
'field' => 'title'
                
'label' => 'Title'
                
'rules' => 'trim|min_length[5]|required|xss_clean'
            
),
            array(
                
'field' => 'slug'
                
'label' => 'Slug'
                
'rules' => 'trim|min_length[5]|is_unique[pages.slug]|alpha_dash|required|xss_clean'
            
),
            array(
                
'field' => 'order'
                
'label' => 'Order'
                
'rules' => 'trim|numeric|required|xss_clean'
            
),
            array(
                
'field' => 'body'
                
'label' => 'Body'
                
'rules' => 'trim|min_length[5]|required|xss_clean'
            
)
        );
        
$this->form_validation->set_rules($config);
    } 
Hi,

1. First you should remove below line from your edit method against slug field.

|is_unique[pages.slug]

2. Once validation is passed, do below steps.

2.1 Fire a simple db query to check if posted slug is equal to the existing db row slug (DB row should be fetched via DB ID which has been posted via hidden form field). If yes than update the db row straight forward.
2.2 If it is different then check if posted slug has not been used by any other DB entry (Simple select with slug as a where clause). If there is no row returned by query then update the DB entry with newly provided slug else inform user that provided slug is already used.

Thanks & Regards
Tapan Thapa
Validating that data has not changed is always an issue with RESTless applications like the ones you get in a web browser. One solution is to create and store a hash value of the values you want to validate (MD5 is fine as it is for comparison, not security) and store that as a field in the database. Before you write your changes back, you need to check you existing hash and compare it to the value in the database. If they are not the same then someone has modified the record since you fetched it and you will need to abandon the changes. If the changes are part of a group (e.g. editing a line in an order) then you may need to abort all the updates. to do this you will need to $this->db->transaction->begin() before the first record and $this->db->transaction->end() after the last record or £this->db->transaction->rollback() if you want to abort the updates.