CakePHP is it possible to instance multiple controller actions in one view - php

I'm a little bit of a beginner to CakePHP and PHP in general, but I have OOP experience.
I'm trying to make a mini Twitter to get used to the Cake framework.
I have a PostsController class that handles all creating blog posts, editing deleting etc, but I'm having trouble adding and add post form to the same page above the View Posts.
i.e. adding posts works fine when I link to a new page
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
but while trying to put a form in the same page as the view I don't know how to call the 'add' action to save and use the data taken in in the form.
echo $this->Form->create('Post',array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');

echo $this->Form->create('Post', array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
putting random arrays in your code will not do anything.

Missing semicolon after the array
Not assigning or using the array in anything.
Trying putting your array as the second parameter in the form create method.

Related

CakePHP Array Checkbox, Black hole error

I am new to CakePHP i tried to submit a form, which has an array input field.
Below is the code i used.
<?php echo $this->Form->checkbox('statusid.', array('value'=>$o['Order']['id'])); ?>
When i tried to submit the form i got a blackhole security error.
How can i fix it ?
I noticed that when i submitted the code below it works.
<?php echo $this->Form->checkbox('statusid', array('value'=>$o['Order']['id']));?>
Is there a way to make an array checkbox in CakePHP please guide me.
You can use the FormHelper and make a checkbox array named statusid this way.
<?php
echo $this->Form->create();
echo $this->Form->input('statusid.0', array('type' => 'checkbox', 'value' => $o['Order']['id']));
echo $this->Form->end();
?>
The issue with blackhole appears when 'hiddenField' is set to false. Although it's set to true by default, it's possible that OP has it defined somewhere globally for entire Form object.
<?php echo $this->Form->checkbox('statusid.', array(
'value' => $o['Order']['id'],
'hiddenField' => true)); ?>

Function that creates a input name from a string in CakePHP

I would like know if there is a function in CakePHP that transforms Mymodel.Mycolumn into data[Mymodel][Mycolumn].
I know how to do this with PHP only, but I would like to know if there is a built-in CakePHP function for it.
EDIT:
I don't need the input, only the name.
See the pretty complete CakePHP Cookbook. To use the current model:
echo $this->Form->input('Mycolumn');
Or to specify the model:
echo $this->Form->input('Mymodel.Mycolumn');
Creates:
<input type="text" id="MymodelMycolumn" name="data[Mymodel][Mycolumn]">
if you're sending, you can do this:
<?php
echo $this->Form->create('Mymodel');
echo $this->Form->input('Mymodel.Mycolumn', array('label' => 'add data for Mycolumn'));
echo $this->Form->submit('submit', array('class' => 'form-submit', 'title' => 'Enter'));
?>

CakePHP: Bad format URLs

I'm trying to create a simple form in CakePHP detached from a model, I'm having problems with the generated URL:
I get this: (doesn't work -> paypal lowercase)
http://local.dev/integration-cloud/public_html/paypal/checkout
Instead of this: (works -> Paypal camel case)
http://local.dev/integration-cloud/public_html/Paypal/checkout
This is my code:
Paypal/view.ctp
<?php echo $this->Form->create(false, array('action' => 'checkout')); ?>
<?php echo $this->Form->end('Finish'); ?>
UPDATE:
If I try this:
<?php echo $this->Form->create(false, array('url' => array('controller' => 'Paypal', 'action' => 'checkout'))); ?>
<?php echo $this->Form->end('Finish'); ?>
It's work, but I don't want to write on all my views the reference's controller, is there another way to do this?
btw I'm on a Linux server, could be a case sensitive problem?
Thanks,
cakephp conventions want controller name in url to be lowercase and underscored
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#url-considerations-for-controller-names
It works even with uppercase controller names but you have to set it manually in every form
You can do as below if you want:
<?php echo $this->Form->create(false, array('url' => '/Paypal/checkout')); ?>
<?php echo $this->Form->end('Finish'); ?>
But the below one is recommended in CakePHP:
<?php echo $this->Form->create(false, array('url' => array('controller' => 'Paypal', 'action' => 'checkout'))); ?>
<?php echo $this->Form->end('Finish'); ?>
The problem here was a mix up between the Paypal's plugin and the name of my controller PaypalController.
public $name = 'Paypal';
When I call paypal (lowercase) CakePHP is trying to call the plugin instead my PaypalController class.
http://local.dev/integration-cloud/public_html/paypal/checkout
So to fix this problem I had to change the name of my controller to PaypalPaymentProcessor

Ajax request won't trigger. [Yii framework]

I have a little problem with a Controller method AJAX call in Yii. The thing is that I'm trying to filter the data of one dropDownList based in the value of a previous selected item.
In the view file, where I figured out is the source of the problem, I have this piece of code:
<?php echo $form->labelEx($model,'Estado'); ?>
<?php echo $form->dropDownList($model,'estado',CHtml::listData(Estado::model()->findAll(),'id','nombre'),array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createAbsoluteUrl('buscar/select'),
'update'=>'#'.CHtml::activeId($model,'tbl_municipio_id'),
),
'class'=>'form-control'
));
?>
<?php echo $form->error($model,'Estado'); ?>
On the Controller side, I got this:
public function actionSelect(){
echo "Hello world";
$data = Municipio::model()->findAll('tbl_estado_id=:tbl_estado_id',
array(':tbl_estado_id'=>(int) $_POST['Consultorio_estado']));
$data = CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
The ajax call to the Select method isn't triggered when the dropDownList is clicked. I tracked the request using Firebug and no error nor fail message is dropped.
Anyone knows what can I do?.
Thanks in advance.
With my knowledge in Yii 1.1.13, there is no such option for ajax for form->dropDownList, just Chtml::dropDownList does.
Therefore you have option to manually custom event change of form->dropDownList or add more jQuery script to handle it by yourself, or simply switch to use Chtml::dropDownList like below example
<?php
echo CHtml::dropDownList('inst_province','',
array(1=>'A',2=>'B',3=>'C', 4=>'D'),
array(
'prompt'=>'Select City',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('city/selectAll'),
'update'=>'#city_area',
'data'=>array('city_param'=>'js:this.value'),
)));
?>
http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

how to display the comments on the same page? cakephp

I am starting to learn PHP framework, particulary cakephp. I know how to insert into database, but I am curious about displaying the entries on the same page. I don't want to reload or redirect the page just to display the results.
I have only three four fields in the database. id, name, email, and the date. I have used the same validation in the model page.
Controllers:
<?php
function index(){
if($this->request->is('Post')){
$this->Save->save(data);
}
//display the entries from the database
$this->set('saves', $this->Save->find('all'));
}
?>
Index.ctp:
<?php
echo $this->Form->create('Save');
echo $this->Form->input('name', array('class'=>'name', 'required' => true));
echo $this->Form->input('email', array('class'=>'email', 'required' => true));
echo $this->Form->input('message', array( 'required' => true));
echo $this->Form->submit('submit', array('formnovalidate' => true));
//i want to display the entries here
foreach($saves as $row){
echo $row['Save']['name'];
echo $row['Save']['comment'];
}
?>
The problem is that , it affects the textarea. It reduces the size to half. Need help. thanks a lot. I am new to cakephp and I have been googling about it, but have not find the any results. Thanks
From what I understand it seems that your problem is more CSS issue than CakePHP issue. I don't think printing the data will cut the field into half.
Try put after echo $this->Form->submit('submit', array('formnovalidate' => true)); because the submit method does not include afterwards.
You can this $this->Form->end if you don't want to add the form closing tag manually.

Categories