1

I am creating a project which is similar to online exam system where teachers create questions and answer type like input or radio button etc. I save those records in database and user logs in and see the form accordingly with appropriate input types.

How can i create those fields on the user form? My database is very simple like below

Questions       information
id              id
teacher_id      user_id 
subject_id      question_id
question        answer 
input_type
is_required

So i am stuck with a problem that how could i create those fields on the user view file? Should i only create one instance of information model and prints the fields with array? or should i create different instance of information model like below

foreach($modelInformation as $i=>$modelInformation){

   echo $form->field($modelInformation, "[{$i}]answer")->textInput();
}

Something like this? or there is some easy way to do this? I am confused because i am not sure how can i give validation if i go this way? Because some of the question can be optional and others are must to answer. If there are any example projects available for similar scenario than point it to me and i will work the rest. Thank you

2 Answers 2

1

It is possible to do this using the html helper. Assuming that you have defined relations between you question and information` models, you can create an active field, and pass the field type as a text parameter, like this.

echo Html::activeInput($modelInformation->question->input_type, $modelInformation, 'answer');

For validation, you will probably need to create a standalone validator and use a switch statement to change the validation depending on the value of $model->question->input_type

Sign up to request clarification or add additional context in comments.

Comments

0

I think there is many way to create that project it depends what client wants or what developer want to implement. I can suggest to following way you can try this:

  1. CMS model : try to make a admin panel in which all question will display from database and you can select question 1 by one to define question should be objective , descriptive etc and that will generate the question on view form accordingly.
  2. If your question's will fixed every time than make a view page and define which question should look like.according to you choice.But this will have to change every time if you change the question . So i would suggest try CMS pattern it will automate your project very well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.