When using the line:
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
The form won't even submit. It is like the page gets reloaded. This code use to work. Also, if I use ad_photos. instead of ad_photos[] I get this error message:
Cannot get an empty property
If I just use ad_photos with no dot or brackets only one file shows up in this request->data. I have tried to debug and var_dump the data as shown below, but it doesn't even make it to that part of the code using brackets and throws an error with just the dot.
controller method:
public function add()
{
$listing = $this->HavesAndWants->newEntity();
$error = '';
if ($this->request->is('post')) {
debug($this->request->data()); exit;
echo "<pre>"; var_dump($this->request->data()); echo "</pre>"; exit;
$listing = $this->HavesAndWants->patchEntity($listing, $this->request->data);
$listing->user_id = $this->Auth->user('id');
$listing->ad_photos = '';
if ( $error ) {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
} else {
if ($this->HavesAndWants->save($listing)) {
$this->Flash->success(__('The listing has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
}
}
}
$this->set(compact('error', 'listing'));
$this->set('_serialize', ['listing']);
}
view form:
<?= $this->Form->create($listing, ['type' => 'file']) ?>
<h1><?= __('Add Listing') ?></h1>
<fieldset>
<legend><?= __('Contact Info') ?></legend>
<?= $this->Form->input('contact_name'); ?>
<?= $this->Form->input('contact_email'); ?>
<?= $this->Form->input('contact_phone'); ?>
<?= $this->Form->input('contact_street_address'); ?>
<?= $this->Form->input('contact_city'); ?>
<?= $this->Form->input('contact_state'); ?>
<?= $this->Form->input('contact_zip'); ?>
</fieldset>
<fieldset>
<legend><?= __('Listing Info') ?></legend>
<?= $this->Form->input('ad_title'); ?>
<?= $this->Form->input('ad_street_address'); ?>
<?= $this->Form->input('ad_city'); ?>
<?= $this->Form->input('ad_state'); ?>
<?= $this->Form->input('ad_zip'); ?>
<?= $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); ?>
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I feel kind of stupid, but the security component was blackholing me. All I had to do was unlock the ad_photos field and I was able to upload multiple files. Although, I am unsure why I was being blackholed in the first place. If anyone has any ideas please post them below. :)
Related
Here is my subjects add.ctp view
<?= $this->Form->create($subject) ?>
<fieldset>
<legend><?= __('Add Subject') ?></legend>
<?php
echo $this->Form->input('math');
echo $this->Form->input('english');
echo $this->Form->input('history');
echo $this->Form->input('science');
******this field will display all the users in drop down************* from users table
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
****UsersController.php ****
public function index()
{
$user= $this->set('users', $this->paginate());
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
How to achieve this one please help me...
If you want user select box(Dropdown) in the subject add.ctp then you should send all the list of user from add() of SubjectsController.
In subjects add method
public function add()
{
//Your previous logics
$this->loadModel('Users');
$users= $this->Users->find('list');
$this->set('users', $users);
$this->set('_serialize', ['users']);
//Your previous logics
}
In your subject add.ctp
<?= $this->Form->create($subject) ?>
<fieldset>
<legend><?= __('Add Subject') ?></legend>
<?php
echo $this->Form->input('math');
echo $this->Form->input('english');
echo $this->Form->input('history');
echo $this->Form->input('science');
echo $this->Form->input('user_id', ['options' => $users]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
In your UsersTable(Users Model), add/edit your initialize() as follow
$this->displayField('username');
$this->primaryKey('id');
at your Controller
$allUser = $this->Users->find(); // this will be get all user from user table
$this->set('allUser', $allUser);
at your View
/**#var array $allUser */// already type of array
<?= $this->Form->select('all_user', [
'multiple' => true,
'value' => $allUser
]); ?>
Use $this->Form->select() or $this->Form->input('users', ['options' => array()]);
if you do not want to do foreach loop.
try:
<?= $this->Form->select('users', $users); ?>
i have a simple search form with method get like this:
<?= $this->Form->create('Search',['type'=>'get']) ?>
<fieldset>
<legend><?= __('Search') ?></legend>
<?php
echo $this->Form->input('id');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
On my routes.php i have the router control:
$routes->connect(
'/test/search/:id',
array('controller' => 'test', 'action' => 'search'), // Local à ser redirecionado
array(
'pass' => array('id'),
'id' => '[0-9]+'
));
To control my url like: /test/search/:search_id.
The problem is my form send the request to /test/search?id=:search_id.
What i need to do, to form send to the correct url: /test/search/:search_id ??
Thanks.
This can be solved using simple approach
First use POST method
<?= $this->Form->create('Search',['type'=>'post']) ?>
<fieldset>
<legend><?= __('Search') ?></legend>
<?php
echo $this->Form->input('id');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
in your controller
use Cake\Event\Event;
public function beforeFilter(Event $event){
parent::beforeFilter($event);
if($this->request->is('post') && isset($this->request->data['id']){
return $this->redirect([
'action' => 'search',
$this->request->data['id']
]);
}
}
and
public function search($id = null){
//....
}
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
),
);
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.