Welcome Guest, Not a member yet? Register   Sign In
Problem generating random field names using form validation
#1

[eluser]Neil_W[/eluser]
Hello,
I'm trying to generate a random name for fields and subsequently check using custom validation, using the form validation library, but it just isn't working.

Firstly, after searching the forum I found you can pass in a second parameter to the set_rules method and this works just fine, e.g. in the index method of the form controller I put:
Code:
$this->form_validation->set_rules('answer','Answer','callback_checkanswer[answerfield]');
Then in my method:
Code:
function checkanswer($str,$fieldname){}

The second parameter gets the value 'answerfield' so you can then use the post data to get the value (assuming this is the name of a field). This works fine.

However, what I want to do is something like:
Code:
$data['answerfieldname']=random_string('unique');
$this->form_validation->set_rules('answer','Answer','callback_checkanswer[' . $data['answerfieldname'] . ']');

So when the form is first generated the field gets a unique name (which it does), however as soon as I click submit the callback method is called but it then seems prior to that it calls the index method again and this and the string is altered because when I interrogate the field in the checkanswer method it is a completely different value.

I tried to do a check before setting the variable so it is only called once, e.g. if($this->input->post('submit'), but that just failed miserably.

Can anyone help? I guess what I'm missing is the way in way the form and controller are generated/called and verified.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB