CakePHP 3 - Unable to add new record - forms

When i submit new Job form, hte action passes all the info to the url but the saving action is not happening and no flash message for success or error.
In variable tab in the debugkit all looks ok except maybe the id but it should auto increment anyway...
job (array)
[new] (true)
[accessible] (array)
* (true)
id (false)
The edit Job method works well.
add Job function
public function add()
{
$job = $this->Jobs->newEntity();
if ($this->request->is('post')) {
$job = $this->Jobs->patchEntity($job, $this->request->getData());
$job->user_id = $this->Auth->user('id');
if ($this->Jobs->save($job)) {
$this->Flash->success(__('Job hasbeen saved'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Please try again'));
}
$categories = $this->Jobs->Categories->find('list');
$users = $this->Jobs->Users->find('list');
$types = $this->Jobs->Types->find('list');
$this->set(compact('job', 'categories', 'users', 'types'));
$this->set('_serialize', ['job']);
}
view
<div class="col_12 column">
<form id="reg_form">
<fieldset>
<legend><?= __('Add Job') ?></legend>
<?php
echo $this->Form->create($job);
echo $this->Form->control('category_id', ['options' => $categories]);
echo $this->Form->control('user_id', ['options'=>$users]);
echo $this->Form->control('type_id', ['options' => $types]);
echo $this->Form->control('company_name');
echo $this->Form->control('title');
echo $this->Form->control('description');
echo $this->Form->control('area');
echo $this->Form->control('city');
echo $this->Form->control('contact_email');
echo $this->Form->button(__('Submit'));
echo $this->Form->end();
?>
</fieldset>
</form>
</div>

Related

Button submit's value not transmitted in POST

I am using Yii2 and I want to create comment functionality on post using Pjax widget.
The comment windows displays all the existing comments for the post adding an edit button to the ones that belongs to the connected user in order to permit changes.
Under this list of comments,there is a form including a textarea and a submit button to allow comment creation.
Each time the user click on an already existing comment 's edit button, a form is displayed to allow update.
How all this actually runs and the trouble I meet:
I can update an existing comment an infinite number of times, and it is what I want.
If, just after refreshing the view post page, I enter a new comment, it
is correctly submitted. But I cannot enter an other one and I want to be able to post more than one. The second time, it appears that the POST contains the content of the comment but nothing
regarding the submit button. Thus I cannot recognize which button has
been clicked.
The same thing happens after I have changed an
existing comment i.e. I cannot create a new comment for the same
reason – nothing about the submit button in the POST.
My question
How comes the submit button is transmitted in the POST only if it is the first one used and it is the first time it is used after a refresh of the page?
Here are the relevant code parts
1-Controller
/**
* display a post
*#param integer $id
*
* return a view
*/
public function actionView ($id){
$post = $this->findModel($id);
$comment=$this->manageComment($post);
return $this->render('view', [
'post_model' => $post,
'comment_model' => $comment,
]);
}
/*
* deals with the post according to the submit value
* if submitted and return the comment under treatment
* otherwise – not submitted – return a new comment
*
*#param $post the post that holds the comments
*
* return a Comment model
*/
protected function manageComment($post)
{
$comment=new Comment;
if(isset($_POST['Comment']) )
{
switch ($_POST['Submit']) {
case 'create':
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment,false))
{
//la création a réussi
if($comment->status==Comment::STATUS_PENDING)
Yii::$app->session->setFlash('success',
Yii::t('app','Thank you for your comment.
Your comment will be visible once it is approved.'));
//on renouvelle le commentaire pour éviter d'avoir
//un bouton update (mise à jour)
return new Comment;
}
break;
case 'update':
$comment=Comment:: find()
->where(['id' => $_POST['Comment']['id']])
->one();
if($comment->attributes=$_POST['Comment']){
if($post->addComment($comment,true))
{
//update successful
if($comment->status==Comment::STATUS_PENDING)
{Yii::$app->session->setFlash('success',
Yii::t('app','Thank you for your comment.
Your comment will be visible once it is approved.'));}
$comment= new Comment;
return $comment;
} else {
//la mise à jour a échoué
$comment= new Comment;
return $comment;
}
}else{echo'load failed'; exit();}
break;
case 'edit':
// echo $_POST['Comment']['id']; exit();
$comment->id = $_POST['Comment']['id'];
return $comment;
break;
default:
}
}
//creation successful
return $comment;
}
2-In Post view
<!--comment_model is passed by PostController-->
<?= $this->render('#app/views/comment/_create-form',
['model' => $comment_model, 'post' => $post_model]); ?>
3 - the _create-form view
<div class="comment-form">
<?php Pjax::begin([ ]);?>
<!-- this bloc must be part of the Pjax in order for the new
comment to appear immediately -->
<div class="comment">
<h4 ><?= Yii::t('app','Comments by users')?></h4>
<div class="comments">
<?php $comments= Comment::find()
->where (['post_id' => $post->id ])
->andWhere(['status' => true])
->all();
foreach($comments as $com){
$identity= User::findIdentity($com->user_id);
$firstname=$identity->firstname;
familyname=$identity->familyname;
$date= Utils::formatTimestamp($com->created_at);
$txt1 = Yii::t('app','Posted by ');
$txt2 = Yii::t('app',' on ');
$text_header =$txt1. $firstname.' '.$familyname.$txt2.$date;
//here we check that $model->id is defined an equal to $com->id
//to include edition form
if (isset($model->id) && ($model->id == $com->id) )//
{
// echo Yii::$app->user->identity->id. ' '.$com->user_id; exit();
echo $this->render(
'_one-comment',(['com' => $com,
'text_header' => $text_header,
'submit_btn' => false,
'with_edit_form' => true]));
} else
{
$submit_btn=false;
if (Yii::$app->user->identity->id == $com->user_id) $submit_btn=true; else $submit_btn=false;
echo $this->render('_one-comment',(['com' => $com, 'text_header' => $text_header, 'submit_btn' => $submit_btn,'with_edit_form' => false]));
}
}?>
</div>
<!--this div should be between Pjax::begin and Pjax::end otheswise it is not refreshed-->
<div id="system-messages" >
<?php foreach (Yii::$app->session->getAllFlashes() as $type => $message): ?>
<?php if (in_array($type, ['failure','success', 'danger', 'warning', 'info'])): ?>
<?= Alert::widget([
'options' => ['class' => ' alert-dismissible alert-'.$type],
'body' => $message
]) ?>
<?php endif ?>
<?php endforeach ?>
</div>
<?php
$form = ActiveForm::begin([
'options' => ['data' => ['pjax' => true]],//'id' => 'content-form'
// more ActiveForm options
]); ?>
<?= $form->field($model, 'content')
->label('Postez un nouveau commentaire')
->textarea(['rows' => 6]) ?>
<?= Html::submitButton('Create comment',
['content' => 'edit','name' =>'Submit','value' => 'create']) ?>
<?php
ActiveForm::end();?>
</div>
<?php Pjax::end()
?>
</div>
4 - the _one-comment view
<div class="one-comment">
<div class="comment-header">
<?php echo $text_header;?>
</div>
<div class="comment-text">
<?php echo $com->content;?>
</div>
<div class="comment-submit">
<?php
if ($with_edit_form)
{
// echo "with edit ".$com->id; exit();
echo $this->render('_update-one-comment',(['com' =>$com]));
} else
{
if($submit_btn){
$form = ActiveForm::begin([
'options' => [ 'data' => ['pjax' => true]],//'name' => 'one','id' => 'com-form-'.$com->id,
// more ActiveForm options
]); ?>
<?= $form->field($com, 'id')->hiddenInput()->label(false);?>
<div class="form-group">
<?= Html::submitButton('', [
'class' => 'glyphicon glyphicon-pencil sub-btn',
'content' => 'edit',
'name' =>'Submit','value' => 'edit']) ?>
</div>
<?php
ActiveForm::end();
}
}?>
</div>
</div>
5 the _update-one-comment form
//echo 'dans update-one-comment';print_r($com->toArray()); exit();
$form = ActiveForm::begin([
'options' => [ 'data' => ['pjax' => true]],//'name' => 'edit-one','id' => 'edit-com-form-'.$com->id,
// more ActiveForm options
]); ?>
<?= $form->field($com, 'id')->hiddenInput()->label(false);?>
<?= $form->field($com, 'created_at')->hiddenInput()->label(false);?>
<?= $form->field($com, 'updated_at')->hiddenInput()->label(false);?>
<?= $form->field($com, 'content')
->label('Mise à jour commentaire')
->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton($com->isNewRecord ?
Yii::t('app', 'Create comment') :
Yii::t('app', 'Update comment'), ['class' => $com->isNewRecord ?
'btn btn-success' :
'btn btn-primary','name' =>'Submit','value' => 'update']) ?>
</div>
<?php
ActiveForm::end();
?>
6- the Post model 's addComment()
public function addComment($comment, $up=false)
{
if(Yii::$app->params['commentNeedApproval'])
{$comment->status=Comment::STATUS_PENDING;}
else {$comment->status=Comment::STATUS_APPROVED;}
$comment->post_id=$this->id;
$comment->user_id=Yii::$app->user->identity->id;
if($up){
$test=$comment->update();
if($test) {//($comment->save(false)){
Yii::$app->session->setFlash('success',Yii::t(
'app','The comment has been succesfully updated.'));
return true;
}
Yii::$app->session->setFlash('failure',Yii::t(
'app','The comment could not be updatded.'));
} else
{ //création
if($comment->save()){
Yii::$app->session->setFlash('success',
Yii::t('app','The comment has been succesfully recorded.'));
return true;
}
Yii::$app->session->setFlash('failure',
Yii::t('app',$up.false.'The comment could not be recorded.'));
}
return false;
}
Why are you counting on the submit button name / value to figure out what you need to do?
$_POST['Comment']['id'] is a much better indicator of what needs to done. If it is there update otherwise create.
Also make sure the model does not allow editing of comments posted by others.
The model should not set flash messages, that is a job for the controller.
"I can update an existing comment an infinite number of times." of course you can, from what you told us there is nothing that would say you should not be able to. And you are not checking to prevent that.
As the submit button's value is not always transmitted in the post (it works with Chromium but not with Firefox), I solved my problem adding a hidden field in the forms and used it instead of the submit button's value.

How to save a binary directly to a database table field (JSON data) in Yii?

I'm working on a Yii project with a database, containing a table, where almost all it's data is saved in a field as JSON (it's crazy, but it is so as it is):
id INTEGER
user_id INTEGER
data LONGTEXT
This "JSON field" data has following structure and contains inter alia an image:
{
"id":"1",
"foo":"bar",
...
"bat":{
"baz":"buz",
"name":"Joe Doe",
"my_picture":"iVBORw0KGgoAAAANSUhEUgAAAGQA...", <-- binary
...
}
}
Displaying it is no problem, but now I want to make the data ediable. My form looks like this:
<?php
$form=$this->beginWidget('CActiveForm', array(
'id' => 'insurance-form',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableAjaxValidation'=>false,
));
?>
<div class="row">
<?php echo $form->labelEx($model, 'provider_name'); ?>
<?php
echo $form->textField($model, 'data[provider][name]', array(
'size'=>60, 'maxlength'=>255, "autocomplete"=>"off"
));
?>
<?php echo $form->error($model, 'data[provider][name]'); ?>
</div>
It works.
I know, that for image upload I need fileField(...), but cannot find out, how to configure it in order to save the image directly to the database. How to do his?
view
<div class="row">
<?php echo $form->labelEx($model, 'provider_name'); ?>
<?php
echo $form->fileField($model, 'data[provider][name]', array());
?>
<?php echo $form->error($model, 'data[provider][name]'); ?>
</div>
controller
public function actionUpdate($id)
{
$model = $this->loadModel($id);
if(isset($_POST['External'])) {
$modelDataArray = $model->data;
// adding the image as string to the POSted data
if (isset($_FILES['MyModel']['name']['data']['provider']['picture'])) {
$_POST['MyModel']['data']['provider']['picture'] = base64_encode(
file_get_contents($_FILES['MyModel']['tmp_name']['data']['provider']['picture'])
);
}
$inputFieldData = $_POST['MyModel']['data'];
$updatedDataArray = array_replace_recursive($modelDataArray, $inputFieldData);
$model->attributes = $_POST['MyModel'];
$updatedDataJson = json_encode($updatedDataArray);
$model->setAttribute('data', $updatedDataJson);
if($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('update', array(
'model' => $model,
));
}
CActiveRecord model
no special changes

cakephp multiple forms with same action

I've got on my page several News, to every News we can add comment via form.
So actually I've got 3 News on my index.ctp, and under every News is a Form to comment this particular News. Problem is, when i add comment, data is taken from the last Form on the page.
I don;t really know how to diverse them.
i've red multirecord forms and Multiple Forms per page ( last one is connected to different actions), and i don't figure it out how to manage it.
Second problem is, i can't send $id variable through the form to controller ( $id has true value, i displayed it on index.ctp just to see )
This is my Form
<?php $id = $info['Info']['id']; echo $this->Form->create('Com', array('action'=>'add',$id)); ?>
<?php echo $this->Form->input(__('Com.mail',true),array('class'=>'form-control','field'=>'mail')); ?>
<?php echo $this->Form->input(__('Com.body',true),array('class'=>'form-control')); ?>
<?php echo $this->Form->submit(__('Dodaj komentarz',true),array('class'=>'btn btn-info')); ?>
<?php $this->Form->end(); ?>
and there is my controller ComsController.php
class ComsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Com->find('all'));
}
public function add($idd = NULL)
{
if($this->request->is('post'))
{
$this->Com->create();
$this->request->data['Com']['ip'] = $this->request->clientIp();
$this->request->data['Com']['info_id'] = $idd;
if($this->Com->save($this->request->data))
{
$this->Session->setFlash(__('Comment added with success',true),array('class'=>'alert alert-info'));
return $this->redirect(array('controller'=>'Infos','action'=>'index'));
}
$this->Session->setFlash(__('Unable to addd comment',true),array('class'=>'alert alert-info'));
return false;
}
return true;
}
}
you are not closing your forms
<?php echo $this->Form->end(); ?>
instead of
<?php $this->Form->end(); ?>
for the id problem you should write
echo $this->Form->create(
'Com',
array('action'=>'add/'.$id
)
);
or
echo $this->Form->create(
'Com',
array(
'url' => array('action'=>'add', $id)
)
);

Passing variables in PHP Zend Framework

I think I have just been working too long and am tired. I have an application using the Zend Framework where I display a list of clubs from a database. I then want the user to be able to click the club and get the id of the club posted to another page to display more info.
Here's the clubs controller:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
}
the model:
class Application_Model_DbTable_Clubs extends Zend_Db_Table_Abstract
{
protected $_name = 'clubs';
public function getClub($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
}
the view:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href=''><?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
I think I am just getting confused on how its done with the zend framework..
in your view do this
<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
'controller' => 'club-description',
'action' => 'index',
'club_id' => $clubs->id
));?>">
...
That way you'll have the club_id param available in index action of your ClubDescription controller. You get it like this $this->getRequest()->getParam('club_id')
An Example:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
public function displayAction()
{
//get id param from index.phtml (view)
$id = $this->getRequest()->getParam('id');
//get model and query by $id
$clubs = new Application_Model_DbTable_Clubs();
$club = $clubs->getClub($id);
//assign data from model to view [EDIT](display.phtml)
$this->view->club = $club;
//[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
Zend_debug::dump($club, 'Club Data');
}
}
[EDIT]display.phtml
<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>
the view index.phtml
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- this method of passing a url is easy to understand -->
<td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
an example view using the url() helper
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- The url helper is more correct and less likely to break as the application changes -->
<td><a href='<?php echo $this->url(array(
'controller' => 'index',
'action' => 'display',
'id' => $clubs->id
)); ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
</table>
[EDIT]
With the way your current getClub() method in your model is built you may need to access the data using $club['data']. This can be corrected by removing the ->toArray() from the returned value.
If you haven't aleady done so you can activate error messages on screen by adding the following line to your .htaccess file SetEnv APPLICATION_ENV development.
Using the info you have supplied, make sure display.phtml lives at application\views\scripts\club-description\display.phtml(I'm pretty sure this is correct, ZF handles some camel case names in a funny way)
You can put the club ID into the URL that you link to as the href in the view - such as /controllername/club/12 and then fetch that information in the controller with:
$clubId = (int) $this->_getParam('club', false);
The 'false' would be a default value, if there was no parameter given. The (int) is a good practice to make sure you get a number back (or 0, if it was some other non-numeric string).

Forms CakePhp 1 form multiple tables

I have two controllers partners and deals.
When I add deal there is field partner id and in cakephp this generates a dropdown with all the partners. Is there away that if the user wants to add a new partner they could click a checkbox and add form would appear. Tried adding the partner input boxes and yes this creates new partner but it in the deal table it puts the partner id of the selected partner from the drop down not the new partner.
Deal Veiw
<div class="deals form">
<h2>Add New Deal</h2>
<p>Use the form below to fill in the new deals details.</p>
<?php echo $form->create('Deal');?>
<?php
echo $cksource->create();
echo $form->input('title');
echo $form->input('price');
echo $form->input('market_price');
echo $form->input('discount');
echo $form->input('buy_link');
echo $form->input('image');
$config['toolbar'] = array(
array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', '-','BulletedList' ),
array( 'Image', 'Link', 'Unlink', 'Anchor' )
);
?><label>Deal Highlights</label><?php
echo $cksource->ckeditor('highlights', array('config'=>$config));
?><label>Deal Fine Print</label><?php
echo $cksource->ckeditor('fine_print', array('config'=>$config));
echo $form->input('description');
?><hr />
<h3>Partners Details<?php
echo $form->input('partner_id');
echo $form->input('Partner.name');
echo $form->input('Partner.address1');
echo $form->input('Partner.city');
echo $form->input('Partner.county');
echo $form->input('Partner.postcode');
echo $form->input('city_id');
?><hr />
<h3>Schedule Deal<?php
echo $form->input('start');
echo $form->input('end');
echo $cksource->end();
?>
<?php echo $form->end(__('Submit', true));?>
</div>
Deal Controller
function admin_add() {
if (!empty($this->data['Partner']['name'])) {
$this->data['Deal']['partner_id'] = "";
if ($this->Deal->Partner->saveAll($this->data)) {
$this->Session->setFlash(__('The deal has been saved', true));
$this->redirect(array('controller'=>'deals', 'action' => 'add'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.', true));
}
} else {
if ($this->Deal->saveAll($this->data)) {
$this->Session->setFlash(__('The deal has been saved', true));
$this->redirect(array('controller'=>'deals', 'action' => 'add'));
}
}
$partners = $this->Deal->Partner->find('list');
$cities = $this->Deal->City->find('list');
$this->set(compact('partners', 'cities'));
}
Any Ideas guys? If you know a better way to do it would be happy to hear it,
Thanks
Dave
in controller before saving, check if the partner form is not empty, then empty out the $this->data['Deal']['partner_id'];