cakephp Form helper $this->data empty - cakephp

I have a problem with the Form Helper that returned $this->data keeps being empty. In my Forms before there was no problems and I cant figure out what's different here.
For this Form there's not a model containing the data, its just user input for doing a search.
This is my View:
<?php
echo $this->Form->create();
echo $this->Form->input('Postleitzahl');
$options=array('10'=>10,'20'=>20);
echo $this->Form->input('Entfernung',array('type'=> 'select' , 'options'=>array(($options))));
echo $this->Form->end('Suchen');
?>

<?php
echo $this->Form->create(null, array('type' => 'post')); # not sure if that's needed
echo $this->Form->input('Search.Postleitzahl');
$options=array('10'=>10,'20'=>20);
echo $this->Form->input('Search.Entfernung',array('options'=> $options)); # seems shorter and should work
echo $this->Form->end('Suchen');
?>
The above should result into a $this->data array containing something similar to this:
['Search']
['Postleitzahl']: 102929
['Enfernung']: 'foobar'

Just don't double array your array:
'options'=>$options

Not necessarily related to Cake, but the answer to the problem when I had it: if you're including a file upload in your POST, double-check that the file you're uploading isn't larger than the limit specified in your php.ini file.

Related

How can I use the same form in multiple views

I am using CakePHP 2.4. I have a blog where I can add and edit posts. When I implemented my edit.ctp, I recognized, that I have the same code in the view add.ctp:
<?php
echo $this->Form->create();
echo $this->Form->input('headline');
echo $this->Form->input('text', array('type' => 'textarea');
echo $this->Form->end('Save');
?>
(simplified code)
Regarding CakePHP´s recommendation, I want to keep my code DRY. What is the best way to define the form only one time and use it in both views?
Create a view in the folder Element with the form code
// app/View/Elements/postForm.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('headline');
echo $this->Form->input('text', array('type' => 'textarea');
echo $this->Form->end('Save');
?>
Then include it in your desired views
echo $this->element('postForm');

formhelper cakephp dont work

I use cakephp 2.3.1 and trying to creat a form with formhelper.
This is my addStudent.ctp :
<?php
$this->Form->create("Test");
$this->Form->input("stuId",array('class'=>'inputField', 'placeholder'=>'SVxxxxxxxx'));
$this->Form->input("stuName",array('class'=>'inputField', 'name'=>'stuName'));
$this->Form->input('submit',array('type'=>'submit'));
$this->Form->end();
?>
I've already add : var $helpers= array('Form'); in AppController.
But it show nothing ? what is the problem here:(
You need to echo the output to the browser. Remember that you are using functions here that return html. But in order for it to be displayed on the page it has to be echo-ed. Try this:
<?php
echo $this->Form->create("Test");
echo $this->Form->input("stuId",array('class'=>'inputField', 'placeholder'=>'SVxxxxxxxx'));
echo $this->Form->input("stuName",array('class'=>'inputField', 'name'=>'stuName'));
echo $this->Form->input('submit',array('type'=>'submit'));
echo $this->Form->end();
?>

blackhole cakephp 2 associated entities

My Goal:
Reuse of a contact form to be related to several different entities I call "Parents" ie Group has contact information, Member has contact info etc....
The way I tried doing it was:
1. Creating a view file for contact, named "form.ctp" which doesn`t create a new form, nor submits, just echo's the contact's fields.
2. Calling this file using requestAction
My Problem:
The form's _Token get crumbled.
Parent add.ctp example
<?php echo $this->Form->create('Group');?>
<fieldset>
echo $this->Form->input($field_prefix.'contact_id',array('type'=>'hidden'));
<?php echo $this->requestAction(array('controller' => 'contacts', 'action' => 'form'), array('named' => array('index'=>'0','parent'=>'Group',
'fields'=>array(
'email'=>array('value'=>'xx#yy.com','hidden'=>1)
))));
inside the form.ctp I have:
//Associated Model
echo $this->Form->input('Contact.0.city',array('type'=>'hidden'));
echo $this->Form->input('Contact.0.postcode');
echo $this->Form->input('Contact.0.phone');
echo $this->Form->input('Contact.0.cellphone');
echo $this->Form->input('Contact.0.email',array('value'=>""));
echo $this->Form->input('Contact.0.id',array('type'=>'hidden'));
?>
Looking at the HTML source code that is generated, I see that whether I use the request action or just copy the contect of the form.ctp into the "Parent's" add file, I get the same HTML result.
HOWEVER!!! when I use the form.ctp Action Request, I get the blackhole, the tokens are being messed up!!!
Any Ideas?
Thanks in advance
Orly
If your problem is solely reusing a form, you can use the form as a Element, and then you could call it multiple times, substituting in the exact values you need.
As for SecurityComponent, I would recommend (at least as a temporary fix) disabling SecurityComponent for that specific action by using $this->Security->unlockedActions(); in your controller's beforeFilter()

'Notice (8): Undefined index' in the view when looping through 'containable' results

I'm using containable to pull associated records into the view action and am getting an error message when looping through results.
Also, I'm using a 'sluggable' behaviour, so the find operation has a condition to search by this variable.
When I debug the find variable in the view, I do see the correct records. But when I try and loop through them in the view I get the 'Notice (8): Undefined index: error.' Ideally I liked to understand how to trouble shoot this error since it happens occasionally.
The model setup is:
tournaments have many tournamentDetails
tournamentDetails have many updates
The records I'm trying to display are:
tournament->tournamentDetails->updates
The tournament controller looks like this:
$tournament = $this->Tournament->find('first', array(
'conditions' => array( 'slug' => $slug),
'contain' => array(
'TournamentDetail' => array(
'Update' => array('order' => 'Update.id DESC'),
))));
The tournament view action looks like this:
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update ['Update']['date']; ?></h3>
<h4><?php echo $update['Update']['title']; ?></h4>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
The 'update' data in the view when in debug looks like:
[Update] => Array
(
[0] => Array
(
[id] => 2
[title] => Indoor Challenge
[date] => 2010-03-19
[body] => Congratulations 2010 Champion
[tournament_detail_id] => 4
[homeStatus] => no
)
[1] => Array
(
[id] => 1
[title] => first round matches start today
[date] => 2010-03-19
[body] => this tournament's first round matches start today.
[tournament_detail_id] => 4
[homeStatus] => no
)
Is there something really obvious that I'm overlooking when looping through the 'updates' ?
Thanks, Paul
Resolution
Thanks everyone for the input. After taking a few steps back it occured to me what was happening with the 'undefined index' and comments about how to loop.
The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.
The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.
Here's the code that resolved this in the view.
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<?php foreach ($tournamentDetail ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
<?php endforeach; ?>
If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.
Cheers, Paul
Paul,
given that the other things are right, could it be that you are mixing singular, plural, uppercase and lowercase? In particular the update/updates in various combinations.
Edit 0:
Quick shot?:
There simply is no such index defined, notice that you are working with an array consisting of arrays. It seems you have to use another loop nested in the first.
Edit 1:
Unfortunately, I can only provide quite generic help, as I do not sit infront of your code.
From your post and comments, I would guess that some variable names got mixed up. Just going over the whole dataflow normally does it. What is the exact variable name in the controller which delivers the correct data? If there is one, is it properly set from the controller to the view? Does the view address it correctly.
You are thinking great, this is what I did the last week, but I am quite sure that should do it. Clean out all the view code which currently makes use of any variables from the controller, proceed as described above keeping the dataflow in mind (but do not spend more than one hour on this).
If it does not work:
Something is foobared. Before you spend another week in despair, I would
try it without the containable behavior, see if it works
try to set up exactly the
same scenario in a completely new
CakePHP environment (in your case
1.2.5), see if it works
If you achieve your goals, try to see what went wrong in the original (often a face slapping moment).
If not:
try to see if there is a known bug
consider upgrading (or try to achieve your goals in 1.3.7 first)
Good luck, Benjamin
I think i understand you problem and please Try looping in this way
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
RSK's answer should be right, so since it's still not working you're probably not giving all the information needed. What does this output:
debug($tournament); ?
This all assumes your debug information is debug( $tournament ) and not debug( $update )
/* your old code */
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update ['Update']['date']; ?></h3>
<h4><?php echo $update['Update']['title']; ?></h4>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
If the debug value you provided was a debug of the $tournament variable then the first part of your loop simply assigns the numerically keyed arrays inside of the tournaments variable to the $update value.
So, when you send $tournament[ 'Update' ] through the loop you are getting a structure in the $update array like the following.
array(
[id] => 2
[title] => Indoor Challenge
[date] => 2010-03-19
[body] => Congratulations 2010 Champion
[tournament_detail_id] => 4
[homeStatus] => no
)
But in your loop you are trying to access the keyed values as if they exist under an additional layer keyed with 'Update'. That key does not exist in your interior array.
So if my assumption is right - your loop should look like:
/* your old code with edits removing the additional interior Update key */
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
There also seemed to be some spacing in your brackets in the original code - I don't know if this is an issue or not but it looked odd to me.
Resolution
Thanks everyone for the input. After taking a few steps back it occurred to me what was happening with the 'undefined index' and comments about how to loop.
The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.
The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.
Here's the code that resolved this in the view.
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<?php foreach ($tournamentDetail ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
<?php endforeach; ?>
If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.
Cheers, Paul

how to create form in cake php

I am very new in cake php, i want to know how to create form in cake php,please describe,when we go to create a form then what i have to do,like create model and controller everything
In the view file, something like this would work:
<?php
echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'Enter your first name:'));
echo $this->Form->input('email', array('label' => 'Enter your email address:'));
echo $this->Form->input('password', array('label' => 'Enter your password:'));
echo $this->Form->end('Save');
?>
In your controller:
if($this->request->is('post')){
$this->User->save( $this->request->data );
}
You can apply some sort of validation in your model, look in the documentation for that.
Best option to learn about cakephp is it's own doc book
But I'm providing you some basic code to create form :
$this->Form->create('ModelName');
$this->Form->input('ModelName.fieldname', array('type'=>'text', 'label'=>'Modified-Name'));
$this->Form->end(__('Submit'));
Here array('type'=>'text'...): type shows which type of input field you want.
(...'label'=>'Modified-Name'): By default it shows field text as fieldname but by using 'label' you can modify your field text.
$this->form->create('controlpage',
array(
'action'=>'controll',
'class'=>'class',
'enctype' => 'multipart/form-data',
'onsubmit'=>'return valid()'
));
Block quote
Create form in html save it as ctp
Block quote
And call it in view. enter code hereUse cake php book to read further.

Resources