Welcome Guest, Not a member yet? Register   Sign In
Form validation validate two dependent fields
#1

[eluser]mjsilva[/eluser]
Hi there,

I've step into a problem using Validation class, I'm building a role access base application and I divided Roles in two groups Module and Action.

When I was building the form to add a new Role I added two input boxes, one for Module and other for Action. The thing is that I need to make sure that the conjunction of the two don't exists already in the DB.

Code example:

Code:
class Roles extends Controller {

    function Roles(){
        parent::Controller();

        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->library('form_validation');
        
        $this->load->model('Roles_model');


    }

    function addRole(){

        $this->form_validation->set_rules('name', 'Name' , 'trim|required');
        $this->form_validation->set_rules('module', 'Module' , 'trim|required');
        $this->form_validation->set_rules('action', 'Action' , 'trim|required');
        $this->form_validation->set_rules('description', 'Description' , 'trim|required');
        
        $this->form_validation->set_error_delimiters('<div class="formError">', '</div>');

        if ($this->form_validation->run() == FALSE ) {
            $this->load->view('roleAdd_view.php');
        }else{
            $this->Roles_model->addNewRole();
            $data['previousPage'] = current_url();
            $this->load->view('operationSuccess_view', $data);
        }
} //END CLASS


Now I need to validate against the DB if module AND action already exists. Only when they exists together I don't want to insert another records, eg:

Code:
Role 1:
$module = 'clients';
$action = 'add';

Role 2:
$module = 'clients';
$action = 'delete';


If I try to insert another:
Code:
$module = 'clients';
$action = 'add';
I want the application to scream that cannot be done, since the role already exists.


Can this be done with form validation class or do I have to come up with a different approach?


Thanks,

MJS
#2

[eluser]davidbehler[/eluser]
Maybe this thread helps: http://ellislab.com/forums/viewthread/123810/
#3

[eluser]mjsilva[/eluser]
Thanks a lot waldmeister for the quick answer!

Didn't know it was possible to pass a second argument in a rule!


Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB