I am preparing 3 pages one is questions for Quiz,one is for questions page and another one is for Options.I used relationship concept in Quiz model i used the following code
var $hasMany = array('question');
}
?>
and in Question model i used the following code
<?php
class question extends AppModel {
var $name='question';
var $belongsTo = array(
'quiz'=>array(
'className'=>'quiz',
'foreignKey'=>'quiz_id',
'conditions'=>null,
'fields'=>null
)
);
}
?>
and in Quizzes,Questions and Chapters controller, i added Scaffolding concept
public $scaffold;
Now I got add Question page,add quiz page and add options page but the problem is all the fields are of textbox but for some fields i need radio buttons and drop down buttons and image type.Is there any way to change the field type from default text boxes to our required format using scaffolding or any other way...
Please help me out with this problem.
Thank you.
If I understand your question correctly, then you are asking how to automatically change the form input types like textbox, radio, and dropdown in scaffolding/bake. This answer is for the current version of CakePHP 2.5.
CakePHP determines the form input type based on the database column data type.
So, for fields with a database column of char, varchar, or other string, CakePHP will output a text box in the form. If you change a column type to boolean, you will see a checkbox.
For radio buttons and drop-downs, CakePHP also needs to know the list of options to present on the form. Your database column therefore needs to be a foreign key. An example:
Let's pretend one of the Questions on your Quiz is "What country are you from?". So you need a table that lists all Countries. Then in the Question table you would have a column called country_id. Remember to update your Questions and Country model with the hasOne / belongsTo relationship. Then bake/scaffold will output a form input with a dropdown for selecting the Country. To change this to a radio button list (and more configuration options) see FormHelper.
Related
I am working on custom quiz module, where we have Multiple choice questions. We have created separate field collections for Question and it's options. Author can add unlimited number of question and it's answers.
While creating a new multiple choice quiz, Drupal renders a question with one option, but by default we want to render 2 options for one question. How should I add one set of field collection to another filed collection in form alter or is there any field collection configuration?
My scenario:
Question (textarea)
Option (textarea)
We want it to be:
Question (textarea)
Option (textarea)
Option (textarea)
I have used below code to add field_collection to the form but while submitting the form the values are not submitted. In the node page I can't see the values.
module_load_include('inc', 'field_collection', 'field_collection.pages');
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_mc_options'));
field_attach_form('field_collection_item', $field_collection_item, $form['field_mc_questions']['und'][0]['field_mc_options'], $form_state);
Did you hve a look at the Built-in Multiple Choice Question Module ? It Inherits from the QuizQuestion-Class which has a getNodeForm()-Method. You can override it when you Subclass QuizQuestion...
I want to let user to publish or unpublish some articles at once. So, in articles list view, I want to put a checkbox beside each row (article) and send these checkboxes to controller.
I tried to use $this->Form->input('status') after each row in my loop, but it created same checkbox for each article. (inputs name and id are the same)
How to create an array of checkboxes or something like this? And how to check them in controller?
Note: Each article has a status field, which is a tinyint 1 character field. (so Cake can understand it's a checkbox)
You will need to specify an 'index' for each input.
In stead of this:
$this->Form->input('Article.status');
Use this:
$this->Form->input('Article.0.id');
$this->Form->input('Article.0.status');
$this->Form->input('Article.1.id');
$this->Form->input('Article.1.status');
// ....
$this->Form->input('Article.xxx.id');
$this->Form->input('Article.xxx.status');
It's the 'index' is just a counter to make sure that 'unique' inputs are generated. However, it is important that each row contains an input for the id of that row; CakePHP will need that to determin which record it should update the status for.
Further reading
Documentation on the naming of fields/inputs can be found here:
FormHelper field naming conventions
CakePHP - Create a form which edits multiple rows of the same model
I am using Cake PHP with scaffolding. I'm having a problem with the code that it generates and want to see if there is a way around it of if I should end up building custom views.
Lets say that I have two models Tests and Questions. Tests can have many Questions and a Question has only one test. I have setup the hasMany and belongsTo Associations.
Now, the scaffolded view that cake creates for Tests gives me a button at the bottom in the "Related Questions" to create a question. When I click this button, I get the 'Add' form for questions, but the right test is not auto selected.
Is there anyway I can make the button pass the test_id into the Question form and have that auto populate?
I see how you think that might work; but Cake doesn't know you want that behaviour out of the box.
You would need to adjust your Question Add method, or create a new one:
Example code:
// action: tests/view/1 (viewing test 1, and all related questions)
// create a link containing the ID of the current test as a param
<?php echo $this->Html->link('Add Question to Test',
array('controller'=>'questions',
'action' => 'add_question',
$test['Test']['id'])
);
?>
So - assuming you have access to id of the current test, you can pass it as a parameter to your questions controller (there are several ways to do this).
Then:
// view - questions/add_question/1
<h1>Adding A Question to Test 1</h1>
<?php
// create your add question form
$this->Form->input('test_id', array('type'=>'hidden',
'value' => $this->params['pass'][0]));
// create a hidden field with the value of the first passed param (the test id)
then in your controller, the test_id is already set, so when you save the question, it is saved with the appropriate test_id
If you want to apply this to all your CakePHP projects generated using cake bake, you can make a couple of small changes to the CakePHP core to enable this, as seen here:
https://github.com/srs81/cakephp/commit/7d92c8f676c79185fa6a74ab2070f240c555a2a0
Basically these two changes append the referring model/controller ID and name to the "add" action, and this is handled in the "add" action where the correct ID is selected.
This does NOT work for HABTM models, but should work fine for anything else.
You need to add var $uses = array('Question','Test'); to questions_controller.php
I have scenario where I have 3 different models data being saved from one form, by means of the saveAll pass through method in CakePHP 2.x
The models on the form are:
Project
ProjectImage
ProjectAttachment
1 and 2 are saving perfectly, but I am having problems with the ProjectAttachment fields. If you look at the following image, the fields in question are at the bottom right, highlighted by a red frame: http://img.skitch.com/20120417-bnarwihc9mqm1b49cjy2bp13cf.jpg
Here is the situation:
I have named the fields as follows: *ProjectAttachment.0.project_id*, ProjectAttachment.0.name .... *ProjectAttachment.6.project_id* etc where the numbers are relative to the already present amount of attachments the user has added, as there is no limit. So if the user has ALREADY added 6 documents, the field would be called ProjectAttachment.7.id and so on. This is because of the naming convention when using the saveAll method.
I have made use of two hidden fields, one to store the user ID, the other to store the project ID.
The problem is that if the user does not fill in the Document Title and select a file to upload, the form fails! If I remove all validation rules, the form no longer fails BUT a blank ProjectAttachment record is inserted.
I suspect the problem may also be that the project_id and user_id fields (that are hidden) already have values in them?
I gave it some thought, and came up with a simple concept: In my controller, before the saveAll call, I would check to see if a blank Document Title field was submitted, and if so, I would completely eliminate the relevant array entry from the $this->request->data['ProjectAttachment'] array, but this did not seem to work.
To clarify, I am not looking for validation rules. If the user decides they only want to upload images and not touch the ProjectAttachment form, them the saveAll operation must not fail, it must simple not attempt to save the blank fields in the form, if this is at all possible.
Any suggestions?
Kind regards,
Simon
Well it seems that the solution was far simpler than I had initially thought! I was partially on the right track, and I had to unset the variable ProjectArray key in the $this->request->data array if I had encountered any blank fields.
I did this as follows:
if(!empty($this->request->data['ProjectAttachment'])){
foreach($this->request->data['ProjectAttachment'] as $key => $value){
if(is_array($value) && array_key_exists('upload_file', $value)){
if($value['upload_file']['name'] == ''){ // Blank document file
unset($this->request->data['ProjectAttachment']);
}
}
}
}
I hope someone finds this somehow useful in some form or another.
Kind regards,
Simon
I'm learning cakephp for some time and it's very nice, I'm using the cake bake to create my classses.
Suppose I have a animal entity and a food entity and they have a many to many relation, and I'm on the add animal view, how can I add the option to add 3 new foods to this animal on this view? and what should the controller code look like in the add funciton?
Think of it like a BlogPost with Tags. To enter the tags on the entry form you'll have a text input field.
In the add/edit action of the controller, you need to explode the contents of the text input and save each one separately.
When you come to edit the BlogPost, you must join the tags into a string again so that it can be used to populate the text input field for addition or removal of tags.
This link should help: http://mrphp.com.au/code/working-habtm-form-data-cakephp