⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Validation for set_relation_n_n field.



Paulo Coelho Alves

Paulo Coelho Alves
  • profile picture
  • Member

Posted 26 February 2013 - 12:59 PM

Hello!

 

I have the following section in my db.

 

Screen%20Shot%202013-02-26%20at%2012.49.

 

I'm using set_relation_n_n to allow the content manager to select questions for the quiz. Fairly straightforward.

 

But in my case, quizzes should have 7 questions — no more, no less. Is there any way to set this validation?

 

Here's how I'm setting the relation:

$form1->set_relation_n_n('Questions', 'quiz_has_questions', 'quiz_questions', 'quiz_id', 'question_id', 'text', 'quiz_order'); 

 

I tried setting a rule like this, but it's not doing anything. Should it? If not, is there any workaround?

$form1->set_rules('Questions', 'Questions', 'callback_questions_check');  

Paulo Coelho Alves

Paulo Coelho Alves
  • profile picture
  • Member

Posted 27 February 2013 - 12:56 PM

Alright, I've come up with a solution, kind of. If anyone comes across this problem, or something similar, you can just do:

 

$form1->callback_before_insert(array($this, 'validateQuiz'));
$form1->callback_before_update(array($this, 'validateQuiz'));

 

And then:

 

public function validateQuiz($post_array) {
    if (count($post_array['Questions']) != 7) {
        return false;
    }
}

The form data will be passed as an array. Including the question ids, which you can then do whatever you please with.

 

The only catch that will only output a generic error popup. I tried using:

 

$this->validation->set_message('validateQuiz', 'The quiz needs to have 7 questions.');

 

But it's not working. This thread suggests using set_rules but, as mentioned before, that doesn't work. Is there any way to call validation->set_message from my callback function? It feels like I should be able to do that.


davidoster

davidoster
  • profile picture
  • Member

Posted 03 March 2013 - 08:56 AM

Did you update your view file to display this?

$this->validation->set_message('validateQuiz', 'The quiz needs to have 7 questions.');

 

by using this, 

<?php echo validation_errors(); ?>

 

Read here: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#tutorial