blackhole cakephp 2 associated entities - cakephp

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

Related

Send image email cakephp without storing it

I'm using Cakephp 3 with Cake\Network\Email\Email.
I have a form to send a message with two input fields. Those fields are stored in database.
I'd like to join an image to this form without storing it: only in attachment.
Here is my input file :
<?php echo $this->Form->file('photo', ['class' => 'form-control','value'=>'','accept'=>'image/*']); ?>
My Controller :
$Email = new Email('default');
$Email->theme('Front')
->template('my_template1')
->viewVars(['sender'=> $user, 'recipes'=> $recipes])
->attachments([$this->request->data['photo'] => $this->request->data['photo']['tmp_name']])
->emailFormat('html')
->to('xx#xx.com')
->from($user['email'])
->send();
Text inputs are well stored and sent. But no image attached in my email...
What's wrong?
Thanks!
I was stuck here to i got it, in your create form add
'enctype'=>'multipart/form-data'
So it should be
echo $this->Form->create($email, ['enctype'=>'multipart/form-data']);
This is added in the other controller by default but when you use a model less form it doesn't do this.

"Record not found in table with primary key [NULL]" error in cakephp 3 association form

I'm stuck on how to save a BelongsToMany/Through association through the parent form.
I have a ProjectsTable that belongsToMany Characters through ProjectCharacters.
I have a CharactersTable that belongsToMany Projects through ProjectCharacters.
My projects/edit/# page contains a second form after the regular projects/edit form that looks like this:
<?= $this->Form->create($project_character); ?>
<?php
echo $this->Form->input('character_id', ['options' => $character_options]);
echo $this->Form->hidden('project_id', ['value' => $project['id']]);
?>
<?= $this->Form->button(__('Add Character')) ?>
<?= $this->Form->end() ?>
This is just a project id and character id that I want to create a new record for in ProjectCharacters. but when I press save, I get the following error:
Record not found in table "projects" with primary key [NULL]
What do I need to do to save a ProjectCharacter association through this form?
I would say your problem is here:
https://gist.github.com/sarahkeefe/a42d39efade836a675c8#file-projectscontroller-php-L156
You are trying to validate the ownership of a project for an action that does not receive any arguments (the add function). You need to conditionally execute that code or to always allow your add() action to your users.
In the future, you can look at the error page, it has a stack trace, which is a list of functions that got called before your error happened. They usually indicate the place where your error can be found.

CakePHP save form data, then pass it to a view through another controller method

I have a Game model, view and controller, and a newgame action which creates a new game record. That works fine.
I then want to make that form data available to main_game_view.ctp either in the session, or as a passed-in-array (but not visible as a URL parameter). I don't mind if it's done on the session, or passed-in, whichever is the more efficient.
I've tried various different combos of set this, write that, pass the other, but nothing's worked so far. Here's my code as it stands after my latest failure. The GamesController newgame action:
public function newgame() {
if ($this->request->is('post')) {
$this->Game->create();
$this->Session->write('Game',$this->request->data);
if ($this->Game->save($this->request->data)) {
// Put the id of the game we just created into the session, into the id field of the Game array.
$this->Session->write('Game.id',$this->Game->getLastInsertID());
$this->redirect(array('action' => 'main_game_view'));
} else {
$this->Session->setFlash('There was a problem creating the game.');
}
}
}
Writing the game id to the session works perfectly, I can see that in main_game_view when I read() it. But no matter where I put the session write for the request data, I can't find it in the main_game_view.
Similarly, if I try to pass, well, anything into the redirect, I can't find it in either the main_game_view action, or the main_game_view view itself.
Currently my main_game_view action is just an empty function, but I couldn't find anything in there after trying to pass the data via redirect.
Here's the main_game_view.ctp:
<?php debug($this->viewVars); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $this->Session->read('Game.game_name') ?></p>
Game.id is fine, but there's nothing in Game.game_name (which is a valid field name from the model. All my attempts to pass in variables have also failed, the debug line has only ever shown: array().
This seems so simple, having followed the CakePHP blog tutorial and adapting it to create/edit/delete game instances, but obviously something hasn't quite sunk in...
You dont have Game.game_new key in session just by writing: $this->Session->write('Game',$this->request->data); Obviously you have to do something like this in main_game_view:
<?php $game = $this->Session->read('Game'); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $game['Game']['game_name']; ?></p>

Cakephp Validation

I am using an Account Controller which doesnt have its own table but uses User Model.
All works fine except - when I validate any form. It says validation fails (when I try to fail the validation to check) but doesnt throw the error below the field
View
<?php echo $this->Form->input('id'); ?>
<label for="UserPassword">New Password:</label>
<?php echo $this->Form->text('password', array('type' => 'password', 'value' => 'harsha')); ?><em>Password must be min 6 characters.</em> <?php echo $form->error('password'); ?>
Controller Action
if($this->User->saveField('password', $this->data['User']['password'], array('validate' => 'first'))) {
$this->Session->setFlash('Password has been changed.', 'flash-success');
} else {
$this->Session->setFlash('There was some problem with our system. Please try after some time.', 'flash-warning');
}
Try debug()ing the contents of $this->validationErrors in your view, as well as $this->data in your controller just after a form submission. This should give you a lot more information to work from.
I suspect that your problem is Cake is building form inputs based on the wrong model -- building form fields for Account.id and Account.password instead of User.id and User.password. This is because FormHelper takes its default model from the controller/view it's invoked from, which in your case appears AccountsController.
In order to generate the User.id and User.password fields your controller's submission handling expects, you'll need to prepend User. in your FormHelper calls. Thus:
$this->Form->input('User.id');
$this->Form->text('User.password');
Have you tried:
echo $session->flash();
Note that whatever the manual says, it returns, not echoes. I logged this a while back and it has been changed in the 1.3 manual, but not the 1.2.
Hi you who's asking If you want to show error-message that return from UserModel's validate So you can add line code bellow after input form password
<?php
if ($this->Form->isFieldError('password')) {
echo $this->Form->error('password', array('class' => 'error'));
?>
and if you want to show error-message that set by method setFlash
you must redirect page and then use $this->Session->flash('flash-name') in page you want to show it
<?php
//in UsersController
$this->Session->setFlash('message here', 'flash-name');
//in view
echo $this->Session->flash('flash-name');
?>
Good luck!

How to save multiple individual records for a single model

I have a model(friends), where user can export his friend's info from facebook. I want save friends info(id and name) into the mysql database.
I am having trouble creating a form for the same using form helper as i don't know the exact number of friends for each user.
Is there a easier way to save user's friends into the database? (save multiple records for the same model)
Basically you should know how much is the number of friends for the current user, right?
If so, do a loop with following in your view:
echo $this->Form->create('User');
for($i=0;$i<$number_of_current_user_friends;$i++){
echo $this->Form->input('Friend.'.$i.'user_id', array('value'=>$current_user_id));
echo $this->Form->input('Friend.'.$i.'friend_name', array('value'=>$friends[$i]['name']));
echo $this->Form->input('Friend.'.$i.'fb_id', array('value'=>$friends[$i]['fb_id']));
}
echo $this->Form->end('Submit');
then in the controller save it with:
$this->Friend->saveAll($this->data);

Resources