I have this in add.ctp:
<!-- File: /app/views/posts/add.ctp -->
<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>
and this in my controller:
function add(){
if (!empty($this->data)) {
if($this->Post->save($this->data)){
$this->Session->setFlash('Your post has been saved');
$this->redirect(array('action' => 'index'));
}
}
}
My question is how does CakePHP know that when the user hits submit, to send "data" to the function "add" in the controller?
By default CakePHP will send the form to the same action that displayed it.
You can change it in the view as follows:
echo $form->create('Post', array('action' => 'whatever'));
or if you want to redirect to another controller as well you can use this
echo $form->create('Post', array('url' => '/controller_name/action_name'));
As per the updated syntax below will work (CakePHP 2.4.x):
echo $this->Form->create('RegistrationsInout', array('action' => 'startroom'));
For cakephp 3.x
$this->Form->create('Post', ['url' => ['action' => 'post']]);
See doc
Related
can i pass form dropdown value in my controller and after this, i can send the value in my DB table....please help me i m new in cakephp here is my job_content.ctp
echo'<div class="AcceptButtonFormData">';
echo $this->Form->create('Job' ,array('action' => 'view'));
$ipr_value=array('0'=>0.0,'1'=>.1,'2'=>.2,'3'=>.3);
echo $this->Form->input('IPR_teeth_pair12',array('type' => 'select','name'=>'drop12', 'options' => $ipr_value,'default'=>0));
echo $this->Form->input('IPR_teeth_pair23',array('type' => 'select','name'=>'drop23', 'options' => $ipr_value,'default'=>0));
echo $this->Form->input('IPR_teeth_pair34',array('type' => 'select','name'=>'drop34', 'options' => $ipr_value,'default'=>0));
echo $this->Form->end();
echo '</div>'
yes you can save it. As per above form this will post to you controller action in view
public function view() {
// Has any form data been POSTed?
if ($this->request->is('post')) {
// If the form data can be validated and saved...
if ($this->Job->save($this->request->data)) {
// Set a session flash message and redirect.
$this->Session->setFlash('JobSaved!');
$this->redirect('/jobs');
}
}
// If no form data, find the recipe to be edited
// and hand it to the view.
$this->set('jobs', $this->Job->findAll());
}
below is just sudo code you can change as per you need and for more understanding you can visit cakephp.org
In AppController:
public $helpers=array("Session","Html","Form");
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
)
);
In MainPagesController:
public function front()
{
$this->Session->setFlash('Your stuff has been saved.');
debug($this->Session->read('Message'));
//etc...
In default layout (default.ctp)
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash(); ?>
The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.
Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.
Simple way to create flash messages is to create their ctp file in app/view/element dir
Try
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash(); ?>
You need to define flash('auth') in your view to see authentication session flash messages.
My setflash works in this way..
Keep this in the App controller
function setFlash($message, $type = 'blue') {
//what ever the color you need..
$arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
$this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
}
and then call it from the controller action where you need to make the flash message as below..
$this -> setFlash("This is flash message","red");
now you need to make the flash in the layout(if you are using) or in your ctp file..
<?php echo $this->Session->flash() ?>
<?php echo $this->Session->flash('red') ?>
I think it is because you have an error here:
<?php echo "Flash:" ?>
You are missing the semi-colon
<?php echo "Flash:"; ?>
I'm new to cakePHP and I've made a simple form following some tutorial. On this html form I've used validation. Now the problem is that the validation is working but the message is not displaying what I want it to display. I tried the code below.
Model
public $validate = array(
'title' => array(
'title_required' => array(
'rule' => 'notEmpty',
'message' => 'This is required field'
),
'title_unique' => array(
'rule' => 'isUnique',
'message' => 'This should be unique title'
)
)
);
Controller
public function add() {
if ($this->request->data) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Post has been added successfully');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Error occured, Please try agan later!');
}
}
}
View
<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>
The validation error which I've seen is not the message I mentioned in my controller.
That's built-in browser validation.
Since 2.3 the HTML5 required attribute will also be added to the input based on validation rules.
Your title has the notEmpty rule, so Cake is outputting
<input type="text" required="required" ..
and your browser is triggering that message.
Edit: to override this behaviour, you can do:
$this->Form->input('title', array('required'=>false));
or
$this->Form->submit('Submit', array('formnovalidate' => true));
When you submit the form, your model validation will fire.
From your code what i can see is that you havent included helpers.
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
Just add to your controllers and try..
Your Form-create() options are invalid, first argument is the model-name, second is for options:
<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>
If the form-helper does not know which 'model' it is creating a form for, I won't check for field validation in the right place, hence, it won't output the validation errors for 'title'
[update] solution above didn't solve the problem. OP has modified the question
Some ideas:
Be sure to enable 'debug' (App/Config/core.php set Configure::write('debug', 2); Otherwise CakePHP may be using a 'cached' version of your model.
If you've named your Model incorrectly, Cake may be automatically generating a model for you, in which case your own model is never actually used, try this for debugging to see if we even 'get' to your model:
Add this to your model;
public function beforeValidate($options = array())
{
debug($this->data); exit();
}
I'm currently using CAKEPHP 2.3 and I'm trying to edit the user's information. but when I submit the information, it does not update the information in to the database. it instead creates a new user with the new information i inserted. i want it to update the user, not create a new one.
My code for the edit.ctp is:
<h1>Edit Account information</h1>
<?php
echo $this->Form->create('User', array('action' => 'edit'));
echo $this->Form->input('username', array('value' => $this->Session->read('Auth.User.username')));
echo $this->Form->input('name', array('value' => $this->Session->read('Auth.User.name')));
echo $this->Form->end('Submit');
?>
And then my edit function in the users controller is:
public function edit() {
// debug($this->User->save($this->request->data));
$this->User->id = $this->Session->read('Auth.User.id');
$this->request->data['User']['id'];
if ($this->request->is('post')) {
if ($this->User->save($this->request->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));
}
} else {
$this->request->data = $this->User->read(null);
isset($this->request->data['User']['password']);
}
}
Here is the index to get to the edit user page.
<h1>Users Home</h1>
<p>Welcome <?php print $this->Session->read('Auth.User.name');?> <br/></p>
<table border="0" width="200" text-align="center">
<tr>
<td width="50"><?php echo $this->Html->link('Log out', array('action' => 'logout')); ?></td>
<td width="50"><?php echo $this->Html->link('Edit', array('action' => 'edit')); ?></td>
<td width="50"><?php echo $this->Html->link('Add User', array('action' => 'add')); ?></td><!-- should only show for admin -->
<td width="50"><?php echo $this->html->link('Manage Users', array('action' => 'usermanage')); ?></td><!-- should only show for admin -->
</tr>
</table>
Many Thanks.
lose the $id in your edit method. usually a user can only - and only himself - edit his own record.
public function edit() {
if ($this->request->is('post')) {
$this->request->data['User']['id'] = $this->Session->read('Auth.User.id');
if ($this->User->save($this->request->data)) {
...
and value is wrong for your forms. never use that. it breaks your form on post and validation errors (see http://www.dereuromark.de/2010/06/23/working-with-forms/ for the "why" - you will also find how you can set default values if you really need to).
since you pass down data via $this->request->data you need to leave them as they were:
echo $this->Form->input('username');
also lose the action. the form will automatically post to itself!
last but not least dont use read(), use find(first)
You have to specify the $id of the User you want to edit. In this case, as only one user can edit his own profile, we do it at the controller:
public function edit() {
$this->User->id = this->Session->read('Auth.User.id');
//whatever...
}
If you are on the edit view and the $id is passed by param, you don't even need to do it, just create the form like this and CakePHP will automatically generate the form for the edit action:
echo $this->Form->create('User');
Also, your inputs seems to be wrong, you don't need the options variable:
echo $this->Form->input('username', array('value' => $this->Session->read('Auth.User.username')));
echo $this->Form->input('name', array('value' => $this->Session->read('Auth.User.name')));
You can also include it in the form. By default, the id field will be hidden in the form. Doing it with session is more secure but you can do it this way for admin functions or verify it before a save.
echo $this->Form->input('User.id');
my problem is the following:
i would like to use a login element instead of a login view.
so i can use the login in my default.ctp... i want the user to have the possibility to login from every page. It should be a sort of dropdown menu.
How can i tell my controller to use the element and get the element data and not the view anymore?
My LoginsController login function:
function login()
{
$this->set('headline','Melden Sie sich an...');
if($this->request->is('post'))
{
if($this->Auth->login())
{
//$this->redirect($this->Auth->redirect);
$this->redirect(array('action' => 'index'));
$this->Session->setFlash('Ihr Login war erfolgreich!');
}
else
{
// $this->Session->setFlash('Ihre Email/Passwort ist falsch!' . ' ' . $this->request->data['Login']['email'] . ' ' . $this->request->data['Login']['password']);
$this->Session->setFlash('Ihre Email/Passwort ist falsch!');
}
}
$this->render('logins/login');
}
My View/Element:
<aside id="left">
<div class="whitebox einloggen">
<div class="rahmen">
<h2>Login</h2>
<div class="inside">
<?php echo $this->Html->para(null,'Sind Sie bereits als Nutzer registriert?');
echo $this->Form->create('Login', array('action' => 'login'));
echo $this->Form->input('email', array ('label' => false, 'type'=>'text','class'=>'text', 'value'=>'E-Mail','id'=>'LoginEmail', 'onfocus'=>"resetinput(this);", 'onBlur'=>"fillinput(this);"));
echo $this->Form->input('password', array ('label' => false, 'type'=>'text','class'=>'text', 'value'=>'Passwort','id'=>'LoginPassword', 'onfocus'=>"resetinputpw(this);", 'onBlur'=>"fillinput(this);"));
echo $this->Form->end(array('label'=>'Einloggen','class' => 'button long','type' => 'submit','name'=>'submit'));
echo $this->Html->para('forgetpw', $this->Html->link('Passwort vergessen?', array('controller' => 'login', 'action' => 'forgotpwd'), array('label' => false, 'class' => 'forgetpw', 'title'=>'Passwort vergessen')));
echo $this->Html->link('', array('controller' => 'login', 'action' => 'fblogin'), array('class' => 'facebook-button', 'title'=>'Mit Facebook einloggen'));
?>
</div>
</div>
</div>
I call the element in the default.ctp like this:
<?php echo $this->element('/logins/login'); ?>
Its always complaining about missing the login view...
If this isnt a good practise, please teach me otherwise ;-)
Sorry for my bad english and thanks!
Have you tried disabling the view in the controller?
If the element is included in default.ctp, adding
$this->autoRender = false;
in place of
$this->render('logins/login');
in your login controller will stop the Login Controller trying to specifically render the view.
You will need to place your element in the Elements directory (under 'Views') and then update your code in default.ctp to be
<?php echo $this->element('login'); ?>
Also, using the cakephp way of setting up the links and forms may help you in the element.
For full guidance for the form, try here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
For how to do links the cake way, try: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html