I want NOT to refresh the page after add action in CakePHP3.
Maybe I should use ajax but if I use ajax, I don't know Model Validation of CakePHP works...
If I use ajax, will Model Validation of CakePHP work?
Or if the better way other than ajax exists, please teach me!
You can send data with ajax. An action method handles the ajax request. When you convert request data into entity (newEntity/patchEntity), the data will be validated. The ajax request and response is different from post, but coverting data to entity, validate and save are the same.
Related
Is there a tool in CakePHP to send POST data before a redirect and to be valid only one request? Something similar like in Laravel's Flashdata.
I'm trying to redirect back and keeping the form with the previous data.
I know there is way using session, but it requires extra work, like removing the data after the submit is completed
It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.
I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).
As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.
public $enableCsrfValidation = false;
But no matter what, data was not being added to the request/post data inside Yii2.
The following Image, explains the problem you will find there:
The Axisos method that sends the post with test data.
The Yii2 Action stpoed at the place, I should be able to see data.
The capture of the xdebug variables and data for the request.
The capture of Chrome where you can check the payload is sent.
The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.
After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.
Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.
There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.
This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.
So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:
$raw_data = Yii::$app->request->getRawBody();
Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.
$object= json_decode($raw_data );
And finally use the data inside by calling the properties you look for, sent on the pay load:
Json Payload:
{
"msg":"This is my payload",
"id":"11"
}
To use it:
echo $object->{'msg'}; // prints: This is my payload
So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.
I recently started working on Rest Based Web services. Here I have a requirement where I need to validate the contents of the request based on a parameter of request.
My question is What will be the best approach to do this validation.
I have two different JSON requests hitting my webservice.
Validate in the interceptor. This will need me to cast the request object back to its actual Type.
Validating the request in Controller.
Also I would like to know if filters can be used in this scenario and what benefit will it give me.
Thanks.
Sorry if this seems like a silly question, but assuming one implements a HTML form with an action triggering a post call to a server, is the server supposed to return the 'next' page or can the post call be twisted to return only a JSON for processing by the same HTML page too?
Is there a standard defining what can or should be done after a HTML post call is performed or is everyone free to do what they want?
is the server supposed to return the 'next' page
In general, yes, although you can return a 204 No Content response.
can the post call be twisted to return only a JSON for processing by the same HTML page too?
It could return JSON, but the page couldn't process it because there is no standard way to process JSON.
If you want to deal with JSON, then you need to make the request with JavaScript instead of a regular form submission.
The browser will render the response returned by the server.
If you want to handle the response in code, use AJAX instead.
I'm using a servlet to access the MySQL data. I already have the code for sending the file from the servlet to the response.
What I don't know is how I'm supossed to catch it with javascript, because I'm not making this call by submit. I don't want to reload the hole page.
I tried making a window.open to the servlet, sending all the params by URL, but it only opens a blank page.
Sounds like you need to look into AJAX. jquery has some stuff to make that easier. I have done some ajax, but that was jquery-less so it is probably better if I point you in the right direction rather than post reams of code.
http://api.jquery.com/jQuery.ajax/
http://articles.sitepoint.com/article/ajax-jquery
Your servlet response type needs to be:
response.setContentType("text/xml");
Hope this helps.