I am trying to add a child in a parent view in CakePHP.
Meaning, I have a Lesson I want to add to a Course in the Course view.
I tried doing it via a simple form helper - adding a $course['Lesson']:
<div class="lessons form">
<?php echo $this->Form->create($course['Lesson']); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id', array(
'value'=>$course['Course']['id']
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
But that doesn't seem to do it.
Am I missing something in the Controller/Model?
let's think course controller
//course controller
public function add_lesson() {
if ($this->request->is('post')) {
$this->Lesson->create();
if ($this->Lesson->save($this->request->data)) {
$this->Session->setFlash(__('The Lesson has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Lesson could not be saved. Please, try again.'));
}
}
$this->set('courses', $this->Course->find('list'));
}
//add_Lesson View
<?php echo $this->Form->create('Lesson', array('url' => array('controller'=>'courses', 'action'=>'add_lesson')) ); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
If you want to use Course controller you can save your data like this:
$this->Course->Lesson->save($this->request->data);
in this case you will need to make sure after submitting your form your data is structure as below:
array(
'Course' => array(
'id' => 1
),
'Lesson' => array(
'name' => 'name'
'description' => 'balbalbala'
'course_id' => 1
),
);
Related
i am new in cake php.I want to insert form data into database.But could not inserted.please help
controller:
officetypesController.php
<?php
class OfficetypesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
}
$this->Session->setFlash(
__('The data could not be saved. Please, try again.')
);
}
}
}
?>
view
add.ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php echo $this->Form->input('name');
//echo $this->Form->input('password');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
model
officetype.php
<?php
class Officetype extends AppModel {
}
database:
table name :officetypes ,
fields name :id,name,under
when i click submit button than showing the message "The data could not be saved. Please, try again."
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
} else {
$this->Session->setFlash(__('The data could not be saved. Please, try again.'));
}
}
}
Check the database to see if the records are added.
Why is the form related to users if you want to add office types?
<div class="office_type form">
<?php echo $this->Form->create('Officetype'); ?>
<fieldset>
<legend><?php echo __('Add Office type'); ?></legend>
<?php echo $this->Form->input('name');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
I have 2 templates,
- /view/opinions/add.ctp - add form
- /view/opinions/list.ctp - displays opinions
and I want them diplay in /views/opinions/index.ctp is it possible?
Is the only way to do it by $this -> element() ? if so, can I include templates from /view/opinions instead of /view/elements ?
#edit
OpinionsController.php
class OpinionsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
var $name = 'Opinions';
function index() {
$opinions = $this->Opinion->find('all');
if(isset($this->params['requested'])) {
return $opinions;
}
$this->set('opinions', $opinions);
}
public function add() {
if ($this->request->is('post')) {
$this->Opinion->create();
if ($this->Opinion->save($this->request->data)) {
$this->Session->setFlash(__('saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Unable to add.'));
}
}
}
}
index.ctp
$this->extend('/Opinions/view');
$this->extend('/Opinions/add');
add.ctp
echo $this->Form->create('Opinion', array('type' => 'file'));
echo $this->Form->input('author_name', array('label' => 'Imię'));
echo $this->Form->input('author_signature', array('label' => 'Podpis'));
echo $this->Form->input('text', array('rows' => '5', 'cols' => '30', 'label' => 'Opinia'));
echo $this->Form->input('author_pic', array('type' => 'file', 'label' => 'Zdjęcie'));
echo $this->Form->input('author_pic_dir', array('type' => 'hidden'));
echo $this->Form->end('Dodaj opinię');
view.ctp
`
<?php foreach ($opinions as $opinion): ?>
<div class="opinion">
<div class="author">
<div class="pic"><?php echo $this->Html->image("defaultAvatar.jpg", array('alt' => $opinion['Opinion']['author_name'])); ?></div>
<div class="signature"><b><?= $opinion['Opinion']['author_name']?></b><br><i><?= $opinion['Opinion']['author_signature']?></i></div>
</div>
<div class="text">
<blockquote><p><?= $opinion['Opinion']['text']?></p></blockquote>
</div>
<div class="clear"><!-- . --></div>
</div>
<div class="clear"><!-- . --></div>
<?php endforeach; ?>
<?php unset($post); ?>
`
In web frameworks like CakePHP, we want to keep our code DRY. In order to do what you want, I think the better approach is to create elements to list your opinions and another with the form to add a new one.
Then in index.ctp you would put these two elements. And in add.ctp you put only the element with the form and in view.ctp you should put the element that list the opinions.
When using the conditions paramater of find, the user id doesn't submit along with the other fields.
public function add() {
if ($this->request->is('post')) {
$this->Thing->create();
if ($this->Thing->save($this->request->data)) {
$this->Session->setFlash(__('The Thing has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Thing could not be saved. Please, try again.'));
}
}
$users = $this->Thing->User->find('list', array(
'fields' => array('User.username'),
'conditions' => array('User.ownedby' => '3')
));
$this->set(compact('users'));}
Here is my view:
<div class="things form">
<?php echo $this->Form->create('Thing'); ?>
<fieldset>
<legend><?php echo __('Add Thing'); ?></legend>
<?php
echo $this->Form->input('user_id');
?>
<div id="user-info">
</div>
</fieldset>
<?php echo $this->Form->end(__('Submit'
)); ?>
</div>
However, when I omit the condition parameter, it submits just fine. Any ideas? In case you were wondering, I'm using the conditions parameter to sort the list of users that results in the select dropdown this creates.
Thanks.
I'm trying to get drop down list in Cake for below two finds by list:
function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The User has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
}
$group = $this->User->Group->find('list');
$commune = $this->User->Commune->find('list');
$this->compact('group','commune');
}
}
The model already has belongsTo defined in User model for Group and Commune models with their ids but I cannot seem to get group and commune to show up as drop down list in the add view page.
Below is what I have for add view page.
<div class="Users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('group_id', array('type' => 'select'));
echo $this->Form->input('commune_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users', true), array('action' => 'index'));?></li>
</ul>
</div>
Is there a reason why it's not working?
you need to use plurals in order to automatically load those options into the form fields.
$groups, $communes
Try this in the view:
$this->Form->input('Group');
Alternatively:
$this->Form->input('group_id', array('type' => 'select', 'options' => $group));
You're setting $group and $commune but the view is looking for $group_id and $commune_id.
I'm trying to add a layout for a cakephp application but now my validation message is no longer being displayed. When validating a comment on a blog entry, the validation message thats suppose to be at the top is not displayed.
If you changing the layout means that you missed to add
<?php
if ($this->Session->check('Message.flash')){
echo $this->Session->flash();
}
?>
before the
Other possible place is in the current controller.
Search if you have code like:
$this->Session->setFlash('...');
The first code is responsible for displaying the message, while the second one is responsible for setting the message.
But the code definitely will help more :)
Here is my add function in comments_controller.php
function add(){
debug($this->data);
//if the user submitted a comment post
if (!empty($this->data)){
//display the 'add view'
$this->Comment->create();
if ($this->MathCaptcha->validates($this->data['Comment']['security_code'])) {
if ($this->Comment->save($this->data)){
$this->Session->setFlash(__('The Comment has been added.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
//failed validation
else{
debug($this->data);
$this->Session->setFlash(__('Comment could not be saved. Please try again.', true));
}
}
else {
$this->Session->setFlash(__('Please enter the correct answer to the math question.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
Here is my entry.ctp where my posts and comments reside:
<div id="article">
<h2><?php echo $entry[0]['Post']['title']; ?></h2>
<p class="date"><em>Modified:</em> <?php $date = new DateTime($entry[0]['Post']['modified']);
echo $date->format('Y-m-d');?></p>
<p class="date"><em>Author:</em> <?php echo $entry[0]['User']['username']; ?></p>
<p class="intro"><?php echo $entry[0]['Post']['content']; ?></p>
<h2>Comments:</h2>
<div id="comments_success"></div>
<!-- show the comment -->
<?php
echo $form->create('Comment', array('action' => 'add'));
echo $form->input('name', array('class' => 'validate[required] text-input'));
echo $form->input('email', array('class' => 'validate[required,custom[email]] text-input'));
echo $form->input('text', array('id' => 'commenttext', 'type' => 'textarea', 'label' => 'Comment:', 'rows' => '10', 'class' => 'validate[required] text-input'));
//captcha
echo $form->input('security_code', array('label' => 'Please Enter the Sum of ' . $mathCaptcha));
echo $form->input( 'Comment.post_id', array( 'value' => $entry[0]['Post']['id'] , 'type' => 'hidden') );
echo $form->end('Submit');
?>
<!-- comments -->
<ol>
<?php
foreach ($entry[0]['Comment'] as $comment) :
?>
<li>
<h3><?php echo $comment['name']; ?></h3>
<p class="date"><em>Date:</em> <?php echo $comment['created']; ?></p>
<p class="text"> <?php echo $comment['text']; ?></p>
</li>
<?php
endforeach;
?>
</ol>
</div>
This is the index function in my posts_controller
function index($entry_id = null) {
if (isset($entry_id)){
$entry = $this->Post->findAllById($entry_id);
$comments = $this->Post->Comment->getCommentsFromPostID($entry_id);
$this->set('entry' , $entry);
$this->set('mathCaptcha', $this->MathCaptcha->generateEquation());
$this->render('entry');
}
else{
$posts = $this->Post->find('all');
$this->set(compact('posts'));
}
}
I know this is old, but ust make sure you have the following code somewhere visible in your app/views/layouts/default.ctp (or whatever is your layout for this application)
<?php echo $this->Session->flash(); ?>
It will echo nothing if there is no message to be displayed, but if there is a message, then it will be output accordingly.