Welcome Guest, Not a member yet? Register   Sign In
Form validation twice in a single controller
#1

[eluser]BilliamTheSecond[/eluser]
I need to run form_validation twice, and I'm not sure how to go about it.

The simple version:
I have a blog post. Blog posts can be in one of several categories. The post admin form has a dropdown with existing categories.
The validation rule for categories is something like is_natural_no_zero|exists[category.id]

The poster can select a category, or select 'create new', enter a new category name and have that created and assigned to the post at submit time.

So, when creating a new category in the add/edit blog post form:
The new_category_name field is required, and has some other rules.
If the new category passes validation, it's added ( $new_id = category_model->add($new_name) ), and the new category id is used for the post.
And _then_ the post is validated.

All help appreciated.
#2

[eluser]pickupman[/eluser]
I think what you are asking would fall under callbacks. If you read the form validation in the user guide you can learn more about callbacks. The basics are that the field value will be passed to a function to be evaluated. You return TRUE or FALSE for the callbacks. Example:
Code:
private function _check_category($category){
  if($this->model->add($category))
     return TRUE; //New category added successfully
  
  return FALSE; //New category failed
}

//In form validation
$this->form_validation->set_rules('category_id','category','required|trim|callback__check_category');
#3

[eluser]BilliamTheSecond[/eluser]
<nevermind>




Theme © iAndrew 2016 - Forum software by © MyBB