1

I am using the form validation library however I have come across something I cannot work out how to do.

I have several fields,

I have:

  • page_title => required
  • menu_title => not required
  • parent_id => null or required

Now if menu_title is not set then it should take on the value of page_title.
Then, I need to do a check to make sure that, at the given level the menu_title is unique.

So,

if($menu_title == '')
    $menu_title = $page_title;

return $this->db->
select('menu_title')->
from('cart_categories')->
where(array('menu_title' => $menu_title,
            'parent_id' => $category_parent))->
get()->num_rows() == 0;

But I don't know how to actually use that in the form validation library?

7
  • Where did you set up your validation rules, or did you get that far? Commented Aug 21, 2012 at 12:10
  • Yeah, I have the start of the validation rules when the form is submitted, e.g. I have the page_title required check and an exists callback running on the parent_id, it's just the more complicated part that I don't get. Commented Aug 21, 2012 at 12:12
  • I could technically use a callback function and inside the function access the other two variables via $this->input->post('whatever') but that doesn't seem right to me. Commented Aug 21, 2012 at 12:13
  • Well, yeah, you will need to set up your own callback function (e.g $this->form_validation->set_rules('menu_title', 'Menu Title', my_callback_function); , and evaluate the query result in the function. See the docs for an example of how to write it, ctrl-f and look for Callbacks. Commented Aug 21, 2012 at 12:17
  • Yeah, I am using a callback to ensure that parent_id actually exists, and I was guessing that I will use a callback for the menu_title, I guess what I am most curious about is how I would a) pass the multiple parameters through to the callback, or should I just access them using the post results directly? Commented Aug 21, 2012 at 12:22

1 Answer 1

1

You'll need to use a callback function in order to do that. Just define one (changed code around slightly to fit formatting). Note, this is off the top of my head (and the docs):

public function check_title($string) {
    $this->db->select('menu_title')->from('cart_categories');
    $this->db->where(array('menu_title' => $string,'parent_id' => $this->input->post('category_parent')));
    if ($this->db->get()->num_rows() != 0 {
        $this->form_validation->set_message('check_title', '%s must be unique');
        return FALSE;
    }
    return TRUE;
}

Then set the input for that particular field to use the callback:

$this->form_validation->set_rules('menu_title', 'Menu Title', 'callback_check_title');

I think you can add an additional underscore to the left side of the callback function name to make sure CI does not route it (as it is public), and just reflect that where you enter the callback name to the rules. I'm not positive about that, though.

As far as passing the additional field as the callback only takes the string, you could just set a class variable.

You can also just handle the validation yourself, which might be cleaner in your case.

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.