How can I use the same form in multiple views - cakephp

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');

Related

Cake Bake issue with views using cakephp3.0

I am new to cakephp framework and i am using cakephp3.0 now i used baking concept instead of using scaffolding. After baking it automatically generated all pages based on my tables in database and it generated code in models,controllers and views.Now my question is "if it is possible to change the code in views to change field types (from text box to radio button) according to my requirements."
Please help me.
Thanks in advance
Yes, after you bake your project you can change the fields types. Navigate to the folder where all your views are located, for example: app\View\MyViewName. Baking is a great tool to get something up and running fast. If you have a fairly structured website mostly used for data entry this is a great tool. I used it for simple data entry websites and it has saved me so much typing! Just add some data validation/constraints in the model and you're good to go!
A freshly baked view will look a little something like this:
<div class="MyForm Form">
<?php echo $this->Form->create('MyForm'); ?>
<fieldset>
<legend><?php echo __('Add My Form'); ?></legend>
<?php
echo $this->Form->input('field1');
echo $this->Form->input('field2');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List My Forms'), array('action' => 'index')); ?></li>
</ul>
</div>
Change the input methods to look something like this:
$options = array('Y' => 'Yes', 'N' => 'No');
echo $this->Form->radio('myFields', $options);

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();
?>

cakephp select field of relationed elements for show in a view

I have this in cakephp, I try choose the field to be displayed in the combobox when creating related data in cake php.
<?php echo $this->Form->create('Round'); ?>
<fieldset>
<legend><?php echo __('Add Round'); ?></legend>
<?php
echo $this->Form->input('description');
echo $this->Form->input('text_marked');
echo $this->Form->input('project_id');
echo $this->Form->input('User');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
I try select one project with project_title, in the table project, not by 'project_id'. Title are not unique but is more descriptive than id. Can I do this in cake php?
The answer was easy,Thus we choose the field to display in the other views that are associated. it was put:
public $displayField = 'titte'
in the Round model.

cakephp Form helper $this->data empty

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.

CakePHP - Have the label and form in Form helper on different line

I'm using this code now
echo $form->input('username');
How do I make sure the label shows up on a different line than the input field?
I managed to imitate what I'm trying to do, just want to make sure that I'm using it the right way.
echo $form->label('username', 'Username');
echo $form->input('username', array('label' => false));
Thanks,
Tee
The core of your request is putting a line-break between the <label> and <input> tags created by the FormHelper::input method. You can accomplish this in several ways. Probably the simplest option is the following:
echo $form->input('User.username', array('between'=>'<br />'));
Or you could also use a pure CSS solution, something like:
<style type="text/css">
div.input label { display: block; }
</style>
<?php echo $form->input('User.username'); ?>
This second option would leave you with cleaner PHP in your views, at the cost of more potential layout/stylesheet headaches.
Try this out.
<p>Username</p>
<?php echo $form->input('username', array('div' => false, 'label' => false)) ?>

Resources