Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - form validation and POST data

I have a form that displays questions from a questionnaire. Some questions are True/False (radio buttons) but some are free text input. Each questionnaire can have any number of questions, which are stored in a lookup table along with the answer provided by the user. To take advantage of CodeIgniters form validation library, I figured I would use an array for the input names and store the primary key of the survey question as the index:

View:

<input type="radio" name="question[<?=$id;?>]" value="<?=$answer?> <?=set_radio('question['. $id . ']', $answer) ?> />

Controller:

$this->form_validation->set_rules('question[]', 'Questions', 'required');

CodeIgniter doesn't seem to validate radio buttons properly when there is no default value set. Any ideas on whether or not I am doing this correctly?

like image 410
Matt Koch Avatar asked Sep 03 '26 13:09

Matt Koch


1 Answers

Reference: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

Check the arrays as fields reference, but you need to call the specific array, if your input is question[pie], your validation needs to check for question[pie] and not questions[].

I'm just making assumptions here as I don't know what you get as a response or what you have tried.

like image 50
Jakub Avatar answered Sep 06 '26 04:09

Jakub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!