Remove Illuminate\Http\UploadedFile from Request object after asynchronous file upload - laravel-5

I have a VueJs form that uploads files. Validation is being handled by the Laravel back-end. The problem I have is that the Illuminate\Http\UploadedFile is not being removed from the request object for subsequent requests, and as such the validation for files is not working.
I am trying to reset the request parameters with $request->replace([]) but it does not work for the uploaded file.
app('log')->debug('Before replace');
app('log')->debug($request);
$request->replace([]);
app('log')->debug('After replace');
app('log')->debug($request);
Successful upload:
[2019-07-23 11:01:06] local.DEBUG: Before replace
[2019-07-23 11:01:06] local.DEBUG: array (
'name' => 'a',
'description' => 'a',
'resourceType' => 'sample',
'applicationTypes' =>
array (
0 => '1',
),
'authorities' =>
array (
0 => '1',
),
'file' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'blueprint.pdf',
'mimeType' => 'application/pdf',
'error' => 0,
'hashName' => NULL,
)),
)
When no file has been selected in subsequent requests, the file object is still present.
[2019-07-23 11:01:06] local.DEBUG: After replace
[2019-07-23 11:01:06] local.DEBUG: array (
'file' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'blueprint.pdf',
'mimeType' => 'application/pdf',
'error' => 0,
'hashName' => NULL,
)),
)
How can I remove the file for subsequent uploads?

Turned out the file was not being cleared in the Vue component after submission.

Related

Where is the file after create by "$service->spreadsheets->create($requestBody)"

Thank you very much for reading this, its quite long!
I run my function:
public function createSpreadSheet()
{
$client = $this->getClient();
$service = new \Google_Service_Sheets($client);
// TODO: Assign values to desired properties of `requestBody`:
$requestBody = new \Google_Service_Sheets_Spreadsheet();
$response = $service->spreadsheets->create($requestBody);
echo '<pre>', var_export($response, true), '</pre>', "\n";
}
I got a result:
Google_Service_Sheets_Spreadsheet::__set_state(array(
'collection_key' => 'sheets',
'developerMetadataType' => 'Google_Service_Sheets_DeveloperMetadata',
'developerMetadataDataType' => 'array',
'namedRangesType' => 'Google_Service_Sheets_NamedRange',
'namedRangesDataType' => 'array',
'propertiesType' => 'Google_Service_Sheets_SpreadsheetProperties',
'propertiesDataType' => '',
'sheetsType' => 'Google_Service_Sheets_Sheet',
'sheetsDataType' => 'array',
'spreadsheetId' => '1QlBQo_YHQpiiMBWn6b6wSMVWkiFRx4grJhPParXUUSU',
'spreadsheetUrl' => 'https://docs.google.com/spreadsheets/d/1QlBQo_YHQpiiMBWn6b6wSMVWkiFRx4grJhPParXUUSU/edit',
'internal_gapi_mappings' =>
array (
),
modelData' =>
array (
),
'processed' =>
array (
),
'properties' =>
Google_Service_Sheets_SpreadsheetProperties::__set_state(array(
'autoRecalc' => 'ON_CHANGE',
'defaultFormatType' => 'Google_Service_Sheets_CellFormat',
'defaultFormatDataType' => '',
'iterativeCalculationSettingsType' => 'Google_Service_Sheets_IterativeCalculationSettings',
'iterativeCalculationSettingsDataType' => '',
'locale' => 'en_US',
'timeZone' => 'Etc/GMT',
'title' => 'Untitled spreadsheet',
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
),
'processed' =>
array (
),
'defaultFormat' =>
Google_Service_Sheets_CellFormat::__set_state(array(
'backgroundColorType' => 'Google_Service_Sheets_Color',
'backgroundColorDataType' => '',
'bordersType' => 'Google_Service_Sheets_Borders',
'bordersDataType' => '',
'horizontalAlignment' => NULL,
'hyperlinkDisplayType' => NULL,
'numberFormatType' => 'Google_Service_Sheets_NumberFormat',
'numberFormatDataType' => '',
.........
.........
))
I access to the link it returned:
'https://docs.google.com/spreadsheets/d/1QlBQo_YHQpiiMBWn6b6wSMVWkiFRx4grJhPParXUUSU/edit'
it gives me this 'request access' page, then I press the request access
I open my email and get this:
Your message wasn't delivered to xxxxxx#xxxxxxxx.iam.gserviceaccount.com because the domain admanager-1x3x71x4x27x4.iam.gserviceaccount.com couldn't be found. Check for typos or unnecessary spaces and try again.
Is there something wrong and what should I do? I just want to open the file and check where it is.
Thank you very much!

Laracasts/Flash not creating html for flash

I have installed Laracasts/Flash package and have been using it successfully for a while. I recently made some changes to my code and added another service provider and now, suddenly Laracasts/Flash is not magically creating the flash message pop-up modal. On my local dev environment it does, but not in production :(. In one of my controllers I run this code:
flash()->overlay("Your content may take a few minutes to be processed.", "Thanks for your submission!");
Log::info( $request->session()->all());
and I checked my logs to find the session data:
[2017-05-18 02:07:08] local.INFO: array (
'_token' => 'somestring',
'backUrl' => 'https://example.com/posts',
'_previous' =>
array (
'url' => 'https://example.com/posts/create',
),
'flash' =>
array (
'old' =>
array (
),
'new' =>
array (
0 => 'flash_notification',
),
),
'login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d' => 1,
'flash_notification' =>
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
0 =>
Laracasts\Flash\OverlayMessage::__set_state(array(
'title' => 'Thanks for your submission!',
'overlay' => true,
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'important' => false,
)),
),
)),
)
although in the environment where it works it looks like this:
'flash_notification' =>
array (
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'overlay' => true,
'title' => 'Thanks for your submission!',
),
So Laracasts/Flash is creating the session data, but then in the returned view there is no flash message. Meaning when I check the source of the web page that is returned I do not see <div id="flash-overlay-modal" class="modal fade flash-modal"><div class="modal-dialog"><div class="modal-content"><div class="modal-header">... that I do when Laracasts/Flash normally creates a flash message. Anyone have any ideas about what the problem could be?
I am using Laravel 5.2.45.
Also I am including the flash view by #include('flash::message')
I have been digging around. I published the flash vendor files so that now I can edit the flash::message view and I put some test text at the top of the file and it shows up, however the first if statment in the view #if (Session::has('flash_notification.message')) never passes.
In my local env when I do Log::info($request->session()->get('flash_notification')); I get this output:
[2017-05-18 05:30:00] local.INFO: array (
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'overlay' => true,
'title' => 'Thanks for your submission!',
)
in my production I get this output:
[2017-05-18 17:42:21] local.INFO: [{"title":"Thanks for your submission!","overlay":true,"message":"Your content may take a few minutes to be processed.","level":"info","important":false}]

Store session in MemCache instead of default session storage in Yii 1.x

This is the code, that I've added to config/main.php in my Yii 1.x application:
'mCache' => array(
'class' => 'system.caching.CMemCache',
'useMemcached'=>true,
'keyPrefix'=>'',
'hashKey'=>false,
'serializer'=>false,
'servers' => array(
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 10000)
),
),
'session' => array(
'sessionName' => 'PHPSESSID',
'class' => 'CCacheHttpSession',
'autoStart' => true,
'cacheID' => 'mCache',
'cookieMode' => 'only',
'timeout' => 1200
),
What should I do next, to force Yii to use CMemCache, instead of default session storage?
I know this answer is old, but this configuration works
'memcacheConn'=>array(
'class'=>'CMemCache',
'servers'=>array(
array(
'host'=>'172.17.0.1',
'port'=>11211,
//'weight'=>60,
),
),
),
'session' => array(
'class' => 'CCacheHttpSession',
'autoStart' => true,
'cacheID' => 'memcacheConn',
'cookieMode' => 'allow',
'sessionName' => 'MYSSIONNAME',
),
Did you read introduction to CMemCache in Yii 1.x API documentation? I think you didn't. In first paragraphs of this document, you have an example, how to use CMemCache in Yii 1.x.
Change 'class'=>'CCacheHttpSession' into 'class'=>'CMemCache' in your session key of configuration file. And you don't have to register CMemCache as separate component, like you did in your example (mCache). You can configure it directly in session configuration key.
An example from Yii 1.x API documentation:
array
(
'components'=>array
(
'cache'=>array
(
'class'=>'CMemCache',
'servers'=>array
(
array
(
'host'=>'server1',
'port'=>11211,
'weight'=>60,
),
array
(
'host'=>'server2',
'port'=>11211,
'weight'=>40,
)
)
)
)
)

Cakephp How to Make Request data contain only the selected field values

Iam Using Cakephp 2.4.6 version. Currently I am facing a problem with some fields in the forms. I have a group of horizontal controls in which the first control is a checkbox which hold the id value.if id is selected then only i want to get all other controls in that row.
is there any way other than getting all values into ajax and send thorough ajax.
TFmPlanProgram' => array(
(int) 0 => array(
't_fm_program_id' => '42',
'number_of_time' => '10'
),
(int) 1 => array(
't_fm_program_id' => '43',
'number_of_time' => '10'
),
(int) 2 => array(
't_fm_program_id' => '44',
'number_of_time' => '15'
),
(int) 3 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
(int) 4 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
This is my array. in that you can see that 3 and 4 having no id value. but it is passed to the server. i want to pass only the selected ID's.
You can try this before calling $this->TFmPlanProgram->save();
foreach($this->request->data['TFmPlanProgram'] as $key => $value){
if(empty($value['number_of_time'])){
unset($this->request->data['TFmPlanProgram'][$key]);
}
}

Setting Validation Rules at Runtime in CakePHP

My problem right now is that a Model has a set of Validation rules like so:
var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'uri' => array(
'slugged' => array(
'rule' => '/^[a-z0-9-_]+$/i',
'message' => 'This field should only contain characters, numbers, dashes and underscores'
),
'uniqueUrl' => array(
'rule' => array('uniqueUrl'),
'message' => 'A page has already acquired this url'
)
),
'meta_keywords' => array(
'rule' => 'notEmpty'
),
'meta_description' => array(
'rule' => 'notEmpty'
),
'layout' => array(
'rule' => 'notEmpty'
)
);
The problem is that in another model that has hasOne relationship its controller also inserts data into it. I want to NOT require the title, uri and layout from that page. How do I do it?
I have a Post Model and I set Page values from there.
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
)
)
I do this from the controller to set info for the Page model
$this->data['Page']['title'] = $this->data['Post']['title'];
It turns to be like this:
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
[title] => data[Post][title]
)
)
My problem is that I do not require a Page field when saving. Post belongsTo Page.
I don't require [Page][layout] when saving a Post as a Post uses the default view of the method in the Post Controller. A page uses static pages and require them when creating a Page, not when creating a Post.
You're preprocessing the data before it goes for validation, so you're taking some control away from validation. As you seem to be making the decision (in code) whether or not certain fields need to be artificially populated you are rendering those parts of the validation redundant and you should remove them. If you have multiple validations in php, you're going to end up confused.
Where to do this or how to do it more cleanly? WellbeforeValidate might be the 'correct' place to do this, but I would do it wherever it best fits with the logic of your application. Function should come before elegance.

Resources