How to upload an image using Sonata Media Bundle - symfony

I am working on a project using symfony.I want to use Sonata Media bundle,in order to upload image.
Unfortunately I don't knwo how to use it or how to start.
I havethis form :
<form action="" method="POST" class="filePhotoForm form-inline" role="form">
<div class="form-group">
<input type="button" class="btn btn-start-order browse" value="Browse">
<input type="text" class="form-control file-name" readonly="readonly" placeholder="No file selected">
<input type="file" name="filePhoto" id="filePhoto" class="hidden file-upload">
</div><br/><br/>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And the code in controller:
public function changePictureAction(Request $request)
{
return $this->render('MedAppBundle:Profile:change_picture.html.twig');
}
Can you help with the basics of uploading?
Thank you!

i have made a custom function for that
protected function saveImageMediaBundle($file,$context = 'default')
{
$mediaManager = $this->get('sonata.media.manager.media');
$media = new Media();
$media->setContext($context);
$media->setProviderName('sonata.media.provider.image');
$media->setBinaryContent($file);
$mediaManager->save($media);
return $media;
}
and get file by this
$file = $request->files->get('newsImage',null);
$media = $this->saveImageMediaBundle($file,'news');

Related

How to create custom html form with redirect back with success message in silverstripe?

I am new in SilverStripe. I want to create a custom HTML form in SilverStripe.
<form class="form-inline" $HelloForm.FormAttributes>
<p id="{$HelloForm.FormName}_success" class="message" style="">$HelloForm.Message</p>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
<div class="checkbox">
</div>
$HelloForm.fields
<input type="hidden" value="{$AbsoluteLink}" name="redirectURL" class="action" id="{$HelloForm.FormName}_action_doSayHello"/>
And in my controller
public function HelloForm()
{
$form = Form::create(
$this,
'HelloForm'
);
$actions = new FieldList(
FormAction::create('doSayHello', 'Submit')->setAttribute('class', 'btn btn-success')
);
$form = new Form($this, 'HelloForm',$actions);
return $form;
}
public function doSayHello($data,$form)
{
$form->sessionMessage('thanks for contact us','good');
return $this->redirectBack();
//i am not getting success message after submit
}
Can I get a success message after submitting in this case?
When I use standard SilverStripe form it's working but when using custom HTML form like above I am stuck
You could submit the form via ajax, which would have many advantages.
The page does not have to refresh itself
You can animate the form after submitting, by sliding up or some kind of similar animation.
You can handle form states like success or error
Some example code (in jQuery):
let form = $('.form-inline');
$(form).ajaxSubmit({
success: function() {
$(form).slideUp();
$('#form-state').text("Successfully submitted form.");
}
})

Why is my ptzloc1 button below not working?

Sorry if I'm posting this code fence wrong. I am a senior citizen (noob) writing my first html.
I am writing a short script to control a ptz ip cam (192.168.1.247) for my raspberry pi.
The ptzLeftSubmit() and ptzRightSubmit() both work when I hit the buttons that call them.
The ptzloc1 and ptzloc2 do not work when I hit them.
The ptzloc1 and ptzloc2 html param.cgi call works in my browser window but not when I push the button.
Am I not allowed to pass the cmd parameter in a script? What am I doing wrong with ptzloc1 ptzloc2 etc?
function ptzLeftSubmit()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/ytleft.cgi";
form1.submit();
}
function ptzRightSubmit()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/ytright.cgi";
form1.submit();
}
function ptzloc1()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-status=1&-number=0";
form1.submit();
}
function ptzloc2()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-status=1&-number=1";
form1.submit();
#then later I do this
<body onLoad="load()">
<form name="form1" method="get" target="test">
</form>
<div style = "position:absolute; left:80px; top:20px;">
<p>
<input type="button"; value="∧" name="B3" onClick="ptzUpSubmit()">
<input type="button" ; value="∨" name="B2" onClick="ptzDownSubmit()">
<input type="button" ; value="<" name="B1" onClick="ptzLeftSubmit()">
<input type="button" ; value=">" name="B0" onClick="ptzRightSubmit()">
<button type="button" ; name="B4" onClick="location.reload()"><script type="text/javascript">document.write("REFRESH");</script></button>
</p>
</div>
<div style = "position:absolute; left:400px; top:20px;">
<p>
<input type="button"; value="1" name="B4" onClick="ptzloc1()">
<input type="button"; value="2" name="B5" onClick="ptzloc2()">
#etc....

Can I use this Form in symfony 4?

I have added this form in twig, i need to know if this is okay and how can i recupere the input name="comments" in controller
<form action="{{ path('Update') }}" method="POST">
<input class="form-control" type="text" name="comments"
value=""></td>
<td>
<input type="submit" value="Save"/>
</form>
You can take a look at the Symfony Request Object section of the doc:
// retrieves $_GET and $_POST variables respectively
$request->query->get('id');
$request->request->get('category', 'default category');
So you can retrieve in the controller as:
$request->request->get('comments');
In your controller you can use the Request object to get all the parameters of your form, for example:
/**
* #Route("/Update")
*/
public function update(Request $request){
$comments = $request->request->get('comments');
...
}
But I recommend you to use the forms component.

Form validation in meteorjs

I am doing simple validation of inputs in meteorjs, after first tour it works, and every next time it doesn't work (until I reload the page) – it means error messages are not displayed.
//main.js//
Template.addMealForm.events({
'click #submitNewMeal': function (ev) {
ev.preventDefault();
var query = {
name: $("#name").val().trim(),
price: $("#price").val(),
calories: $("#calories").val(),
category: $("#category").val()
};
areInputsValid(query);
}
});
var areInputsValid = function (query) {
if ((query.name.length === 0) || (query.price.length === 0) || (query.calories.length === 0)) {
$("#warningLabel").addClass("di")
$(".warningLabel").text("All fields are required");
}
else if ((isNaN(query.price) === true) || (isNaN(query.calories) === true)) {
$("#warningLabel").addClass("di")
$(".warningLabel").text("To Price and Calories fields please enter a number");
}
else {
console.log('it works');
$('.dn').hide();
}
};
//main.html//
<template name="addMealForm">
<form role="form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control input_form" id="name" placeholder="Name of the meal">
</div>
<div class="form-group">
<label for="price">Price</label>
<input class="form-control input_form" id="price" placeholder="Price">
</div>
<div class="form-group">
<label for="calories">Calories</label>
<input class="form-control input_form" id="calories" placeholder="Calories">
</div>
<div id="warningLabel" class="form-group has-error dn">
<label class="control-label warningLabel"></label>
</div>
<button id="submitNewMeal" type="submit" class="btn btn-rimary">Add</button>
</form>
</template>
The problem is that you are calling $('.dn').hide() in the success case. Because #warningLabel has a class of dn it will not be displayed again on subsequent errors.
One solution is to add $('.dn').show() to the top of areInputsValid.
You already have Tracker as part of Meteor, so I put a little tutorial and JSfiddle together on how to use it to implement a typical form validation scenario.
http://bit.ly/meteor-form-validation-video
http://bit.ly/meteor-form-validation-fiddle

Symfony2 cannot get mp3 or mp4 file from request

I use symfony2 to build a website that is similar to Youtube, but I got failed when I upload .mp3 and .mp4 files into my sever.
In my twig file, it looks like,
<form class="uploadForm" method ="POST" enctype="multipart/form-data" action="{{path('mss_core_media_uploadAudio')}}">
<div class="col-md-6">
<h2>Upload center</h2>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="input-group" style="width: 420px;">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="audioFile"></span>
Remove
</div>
</div><button class="btn btn-primary fileupload-exists" type="submit">Upload</button>
</div>
</form>
my controller code looks like,
$audio = $request->files->getData()->get('audioFile');
$status = 'success';
$uploadedURL = '';
$message = '';
$logger = $this->get('logger');
$logger->debug($audio);
if (($audio instanceof UploadedFile) && ($audio->getError() == '0')) {
But I got $audio is null in my action method of controller, so the rest part of code cannot be performed. I have no idea about this, I was confused that I can successfully upload an image and a pdf file by using the same method, but both audio and video files got failed.

Resources