0

How do I set validation rules in codeIgniter if the inputs have same validation rules like in this case its required.

            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('firstname', 'Firstname', 'required');
            $this->form_validation->set_rules('middlename', 'Middlename', 'required');
            $this->form_validation->set_rules('lastname', 'Lastname', 'required');

1 Answer 1

2

you can simply make an array of fields and then iterator over the array

$fields = array('username' => 'Username', 'password' => 'Password', 'firstname' => 'Firstname');
foreach($fields AS $key=>$val){
 $this->form_validation->set_rules($key, $val, 'required')
}

next time, you just need to add new item(s) to the $fields array and it will auto add validation rules for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.