Jump to content

Validation for set_relation_n_n field.

validation rules n-n set_relation_n-n

  • Please log in to reply
2 replies to this topic

#1 Paulo Coelho Alves

Paulo Coelho Alves


  • Members
  • Pip
  • 2 posts

Posted 26 February 2013 - 12:59 PM



I have the following section in my db.




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');  

#2 Paulo Coelho Alves

Paulo Coelho Alves


  • Members
  • Pip
  • 2 posts

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.

#3 davidoster


    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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/...n.html#tutorial



rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---



David Oster aka George Pasparakis,

Also tagged with one or more of these keywords: validation, rules, n-n, set_relation_n-n

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users