Cakephp form creation gives me missing database table - forms

According to the manual, I should be able to do this which is to leave Model as null
<?php
echo $this->Form->create(null, array('url' => '/recipes/add'));
// or
echo $this->Form->create(null, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
But in reality, I got error saying missing database table? Why?
I have my Model which is not directly mapping to any table. Why can't I leave it as null? Something like this:
<?php echo $this->Form->create(null,array('url' => array('my_account','action' => 'change_avatar'),'type' => 'file'));?>

Try passing false for the model instead of null:
<?php
echo $this->Form->create(false, array('url' => '/recipes/add'));
// or
echo $this->Form->create(false, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
From the manual:
You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.

make a table with name recipes in your database. The submitted date from the form will be saved at recipes table.

Related

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

CakePHP Text as Form Submit

I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

Display boolean as radio button in CakePHP form

Does anyone know how to display two radio buttons in a form in CakePHP and have them save to a boolean field in the DB?
I can get my boolean value to display as a checkbox (default behaviour) but I need to display two radio buttons, however when I do this the changes don't get saved to the DB.
I feel like it's something very simple. Here's my code:
<h1>Edit Content</h1>
<?php
$thisVal = $this->data['LessonContent']['is_exercise'] ? "1" : "0";
$options = array(
'0' => 'Learning Material',
'1' => 'Exercise'
);
echo $this->Form->create('LessonContent', array('action'=>'edit'));
echo $this->Form->input('name', array('label' => 'Name'));
echo $this->Form->input('description', array('label' => 'Description'));
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'name' => 'Type',
'options' => $options,
'value' => $thisVal
));
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->input('lesson_id', array('type'=>'hidden'));
echo $this->Form->end('Save Content');
echo $this->Html->link('Cancel', array('controller'=>'Lessons', 'action'=>'view', $this->data['Lesson']['id']));
?>
Thanks
You're overriding the name of the input, therefore the value for is_excercise will be sent as Type.
Remove the name option;
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'options' => $options,
'value' => $thisVal
));
note after making this change, you probably don't even have to manually set the value of the radio-button.
debugging
In situations like this, always check/debug the posted form-data, either via FireBug or by debugging it in CakePHP, by putting this in your controller;
debug($this->request);
Even better, install the CakePHP DebugKit Plugin this plugin shows all the information (request data, queries, session variables etc.) without having to add debug-lines

Redirection from module to application

how can we go back form one module to parent application in ZEND. I use this code:-
<?php echo $this->url(array('controller' => '', 'action' => '')); ?>
If you add a 'module' key to the URL array, you can easily traverse modules. If you're trying to get to the "Parent" (the default module, perhaps?), you'd change your code to:
<?php echo $this->url(array('module' => 'default', 'controller' => 'index', 'action' => 'index)); ?>

How do I take over a value sent by a "null form" redirecting to another controller?

This is my form:
echo $this->Form->create(null, array('url' => '/offices/addOffice'));
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $this->data['Agency']['id']));
echo $this->Form->end(__('Add Office', true));
Now in the addOffice function I would like to get the value sent from this form.
What I did is:
function addOffice($id = null){
$this->set(compact('id'));
}
but it doesn't send the $id to the view. What am I doing wrong?
I'm not sure what you're asking, because if you want to "get the value sent from this form", then "send the $id to the view" doesn't have anything to do with it.
If you want to retrieve the the data from the form, you shouldn't put null as the model's name. Use the relevant model, which I assume is "Office" in this case.
echo $this->Form->create('Office', array('url' => '/offices/addOffice'));
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $this->data['Agency']['id']));
Now the id can be retrieved from $this->data[ 'Office' ][ 'id' ] in the controller.
If the question is how you can set the id in the first place using the function parameter, you have to use the $id parameter you've set in the controller:
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $id));