简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM