Passing data via POST from an element to controller/model Cakephp - cakephp

I am pretty new to cakephp, I have an uploader form and would like to pass all of the data using POST to the controller and model so that it will save to the database. So far there are no cakephp errors occuring but no data is being passed.
Controller Code:
<?php class Controller extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file has been liberated :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}
}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="uploader" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}
?>

Please, check input specification you can see there, that there are no "uploader" value for type attribute. Maybe you mean type="file".
Also, if you want to upload files, please, use
echo $this->Form->create('File', array('type' => 'file'));
this will produce correct enctype attribute enctype="multipart/form-data". such enctype needed if you send files by POST.
Also, you use style: style="visibility: hidden;" which should hide file input field, so, maybe you use some flash uploader or ajax, but this is not demonstrated in your code. If you provide more code, we can provide more concrete help to you.
and counter question: how you debug your code?

Related

Sending form errors back to view via redirect

I recently started picking up on CakePHP and now I'm trying to build a contact form for my site.
It seems to work in validating the forms, but there's something I want to do now.
I want it to show any errors that occur (like missing a required field).
However, I want it to show 2 different possible states:
- A user error (user forgot a field or the field doesn't meet the requirements), show this below the field (using Bootstrap 4)
- A server error (mail couldn't send from the server - for example, the SMTP server is down), shown using the Flash
I've been searching far and wide for this, but I couldn't get any further on this without posting a question myself.
Below is all the code that I'm using (running CakePHP 3.6)
src/Template/Pages/contact.ctp (controlled by the PagesController):
<div class="container">
<div class="row no-banner">
<div class="col-md-6">
<h4>Contact Form</h4>
<div style="padding-bottom:25px;">
Got a question? we'd love to hear it from you!<br />
Send us a message and we'll respond as soon as possible!
</div>
<?= $this->Flash->render(); ?>
<?= $this->Form->create("Contact",array("url"=>"/contact","class"=>"contact-form","id"=>"contact-form")); ?>
<?= $this->Form->control("name",array("placeholder"=>"Your Name","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->control("email",array("placeholder"=>"Your Email","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->control("subject",array("placeholder"=>"The Subject","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->textarea("message",array("placeholder"=>"Your Message","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->button('Submit',array("class"=>"btn")); ?>
<?= $this->Form->end(); ?>
</div>
<div class="col-md-6">
<h4>Social Media</h4>
<div style="padding-bottom:25px;">
We are active on a variety of of social media, feel free to like and follow us!
</div>
<i class="fab fa-facebook social-media-icon"></i>
<i class="fab fa-discord social-media-icon"></i>
<?= $this->Form->errors; ?>
</div>
</div>
</div>
src/Controller/ContactController.php:
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController {
public function add() {
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you asap!');
$this->redirect($this->referer());
} else {
$this->Flash->error('There was an issue sending your mail. Please try again later!');
$this->redirect($this->referer());
}
}
$this->set('contact', $contact);
}
}
src/Form/ContactForm.php:
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController {
public function add() {
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you asap!');
$this->redirect(array('controller' => 'Pages','action' => 'display','contact'));
} else {
$this->Flash->error('There was an issue sending your mail. Please try again later!');
$this->redirect($this->referer());
}
}
$this->set('contact', $contact);
}
}
Don't redirect, because you don't need to. Instead of using the Pages controller to display the form put the form in your controllers view: /Contacts/add.ctp. No need to call redirect() then.
The errors are already added to the form and the form helper will find them because you're passing the form object to Form->create().
If you want to make it more complicated than needed stick to your implementation and simply write the errors to the session and in the action that receives it read them from the session and set them to the form object via setErrors().

Passing Data from Dropbox Chooser to controller/model in Cakephp

EDIT:
I am getting the JSON results from the dropbox chooser, but they are not being parsed properly to be put into the database. I can't figure out what I am doing wrong, if I should be doing a JSON decoder in my controller or if it is something else.
Controller Code:
<?php class DropboxfilesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file is now available :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="Dropboxfilechooser" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}?>
The actual problem is looking at the source code, the form action is not going to the proper controller, it is just going to the cake/homes. Just change that and the problem is solved.

CakePHP form input filed empty after get submit

echo $this->Form->create('Driver', array('type' => 'get'));
echo $this->Form->input('name');
echo $this->Form->end('Search');
as result $this->request:
query => array(
'name' => 'some name'
)
Problem is input form is empty after search although $this->request->query['name'] = 'some name'
Everything works as expected when change form back to post
Edit. Included the model and the controller. For testing I use clean install.
Model (Driver.php):
App::uses('AppModel', 'Model');
class Driver extends AppModel {
public $displayField = 'name';
}
Controller (DriversController.php):
App::uses('AppController', 'Controller');
class DriversController extends AppController {
public function index() {
$drivers = $this->Driver->find('all');
$this->set(compact('drivers'));
}
}
In your controller code you do not show us where you are trying to access the submitted form values so I will try and give some general information to get you moving.
To access your form data, you need to cool use request. To see exactly what is going on, enter in your controller one of the below...
print_r($this->request->data);
or
print_r($this->request);
Either of those will show you any data registered with CakePHP.
If you want to save this save using your Models. use...
$this->Driver->save($this->request->data)
You might want to check it is a post first though.. lets complete the code...
public function submit() {
if ($this->request->is('post')) {
$this->Driver->create();
if ($this->Driver->save($this->request->data)) {
$this->Session->setFlash('Saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('FAILED');
}
}
}
The information above can be read in further detail here.
You can set form values by assigning to $this->data.
$this->data = $this->request->query;

Sending string/text to a method/function in cakePHP

Good Day to all. I am currently developing a chat application using cakePHP. It will be a chat application that focuses on answering questions. That means the user will receive an automated response based on his/her question. I am working on the chat interface right now that doesn't require the user to login. The chat application will only be interacting to a database table once the user has sent the question. Now my problem is on how to send the question to a method in the controller where it will be parsed. I tried to do the following in the view file:
<!--View/People/index.ctp-->
<h1>This is the chat interface</h1>
<?php $this->Html->charset(); ?>
<p>
<!--This is the text area where the response will be shown-->
<?php
echo $this->Form->create(null);
echo $this->Form->textarea('responseArea', array('readonly' => true, 'placeholder' =>
'***********************************************************************************
WELCOME! I am SANTI. I will be the one to answer your questions regarding the enrollment process
and other information related to it. ***********************************************************************************', 'class' => 'appRespArea'));
echo $this->Form->end();
?>
</p>
<p>
<!--This is the text area where the user will type his/her question-->
<?php
echo $this->Form->create(null, array('type' => 'get', 'controller' => 'people', 'action' => 'send', ));
echo $this->Form->textarea('userArea', array('placeholder' => 'Please type your question here', 'class' => 'userTextArea'));
echo $this->Form->end('Send');
?>
</p>
This is the controller:
<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
public $helpers = array('Form');
public function index(){
}
public function send(){
//parsing logic goes here
}
}
?>
As you can see, I am telling the form in index.ctp to point the action to the send() method in PeopleController so it can parse the question before interacting with the database. The problem that arises when I click the button is that I am always redirected to /users/login which is not what I want to happen. I just want the application to point itself to /people/send. What seems to be the problem in that case? I have tried to look for answers both in the Internet and in the documentation and then tested them but nothing has resolved the problem so far. Can anyone please help me on this? I've been trying to resolve this for so many days.
I keep on getting this error:
Missing Method in UsersController
Error: The action *login* is not defined in controller *UsersController*
Error: Create *UsersController::login()* in file: app\Controller\UsersController.php.
<?php
class UsersController extends AppController {
public function login() {
}
}
If you are using Auth Component, then you might need to change your PeopleController code:
<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
public $helpers = array('Form');
public beforeFilter()
{
parent:: beforeFilter();
$this->Auth->allow('index', 'send');
}
public function index(){
}
public function send(){
//parsing logic goes here
}
}
?>
This is because of you used people/send as a form action. And the user is not logged in, it means there is no any Auth session has been set. Thats why it always redirect the user to login page, and if there is not login page, then it will show you the error.
So I made the send() method also public, so that anyone can access it.
Hope this concept will help you.

How to use tinymce plugin?

I have tried many times to use this plugin and I failed.
I am following documentation, but it does not work for me.
I am posting the simple code here, to know what wrong I am doing.
1-I put this plugin in this folder app/plugins
2- I add TinyMce helper to articles_controller
<?php
class ArticlesController extends AppController {
// good practice to include the name variable
var $name = 'articles';
// load any helpers used in the views
var $helpers = array('Html', 'Form','TinyMce.TinyMce');
/**
* index()
* main index page of the formats page
* url: /formats/index
*/
function index(){
// get all formats from database where status = 1
$articles = $this->Article->find("all") ;
$this->set('articles', $articles);
}
function admin_add() {
// if the form data is not empty
if (!empty($this->data)) {
// initialise the format model
$this->Article->save($this->data);
// set a flash message
$this->Session->setFlash('The Format has been saved');
// redirect
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The Format could not be saved. Please, try again.','default', array('class' => 'flash_bad'));
}
}
}
?>
3- in the view file articles/admin_add.ctp I added the editor
// i think the problem in this code
<?php $this->TinyMce->editor(array(
'theme' => 'advanced'
)); ?>
<div class="formats form">
<?php echo $form->create('Article');?>
<fieldset>
<legend>Add a article</legend>
<?php
// create the form inputs
echo $this->Form->input('title');
echo $this->Form->input('content'); ?>
</fieldset>
<?php echo $form->end('Add');?>
</div>
<ul class="actions">
<li><?php echo $html->link('List Articles', array('action'=>'index'));?></li>
</ul>
You need to put tinymce files into your js assets
Then you have to add into section of your layout.
Then you'll need to init tinymce according to example provided on tinymce website (ex: full tinymce layout) and configure it according to your requirements.
I'd personally would not rely on such cake plugins, when actions required to get things working are not many and they are simple enough.

Resources