how to return url in form file in strut - struts2

hai all,
i have one problem regarding to return the form file value is validation fail. i have one form that have a few field to be fullfil by the user and one attachment field.if one of the field is blank the system will give the error when submit the form..my problem is, all the field will have the value that we entered before this but for form file it disappear.

Let's see if I understand your issue: You have a form with several input fields, and among them a <input type=file> for uploading some file content. In case of validation error, you return to that page (with struts2 result = INPUT, I guess) and the content of the previously filled fields appears, except for the FILE field.
This makes sense, if you understand how the other fields are "refilled" in Struts2 in this scenario (just from the action, which has typically mapped the parameters to properties). The server doesnt know the (clientside) fullpath of the file, it is not sent along the http request (it would be a privacy issue), it's the content of the file what is uploaded (and perhaps the filename without path). You can't escape that.
Anyway, I think this a case in which validation should signal an error early (in javascript, in the client side). Think about it: the user is uploading a file (potentially large) together with other info, and AFTER uploading it the server checks the fields and instructs the user to refill the fields and retry (including the upload). This can be unacceptable. My advise is, then, to include client-side validation (and if it pass, and the server validation fails, then the user must resign to refill the FILE field). If the file is large, one could split the input form in two pages, a first one to fill the several fields, and afterwards another to upload the file.

Related

mvc file upload and database insert

I'm just getting my head wrapped around MVC in .net using VS 2013.
I need a little direction in regards to uploading a file (which is easy) but also inserting data about that image into a database. Specifically I want to allow the end user to enter a description, title etc. about the file being uploaded. On the back-end I want to also add to the meta data a 'Date Created', 'Path to the file', 'Category', and the File Name and a couple other pieces of data that will help with presenting files in the views. I don't want to insert the files in the DB but just use the path in the generated HTML to point to the physical file so the end user can view or download it.
Multiple file types are being used, Audio, Video, Documents, Images.
I can get the file upload to work but writing the controller to accept a file, and end user input, then also add the other fields I need into the database that the user never sees is where I'm stuck. Blending the file upload with the user fields and beack end data is confusing me on how to get all the pieces to work together.
So in short getting File Upload + User Input + non-User Input values all in the same View, Controller, and Model is what I need direction on.
You have to upload your image plus data in a multi-part form.
In your view you will create a POST form that uses "multipart/form-data" encoding, you can then include inputs for model data and your file upload control within the body of the form. When it submits will will create a multi-part form, one part will contain the binary file and another part will contain your data.
On the controller action side you will receive the data with an action akin to
public ActionResult PostFile(MyModel model, HttpPostedFileBase file) {...}
There are numerous posts on SO with more details so I won't go into that.

Hybrid Model/Controller Validation?

I am looking to have a user enter in a URL to an image on a form. I already have this built. I would like for two different things to happen, but I was wondering if I could somehow combine them.
When the user submits the form, I would like the URL to be validated, not only that it takes on a valid url format, but that it actually exists.
I would also like to set an integer in the database depending on the type of document the URL points to. If it is an image, I would like an integer field on the record (let's call it 'file_type') to be set to 1. If it is not an image, I would like the validation to fail.
Is there any way to validate the URL while also collecting data for the record? I ask this because I believe it makes much more sense for the server to make the request once rather than twice, once for validation, and once for storing file type.
Is this possible? What would you suggest?
Thanks!

Can someone explain Rails behavior for multiple fields with identical names and ids?

I needed to create a input mask for a jQuery datepicker in my Rails app, where the first form field uses m/d/yy format and the datepicker populates a hidden input with the proper database format.
I was using SimpleForm, and so I extended my own input so that the input is preceded by the mask.
I got everything set up and when checking out the browser, it all just worked well before I thought I would be done.
The form ends up with two inputs for the same attribute, each with the same id and name. I never thought this would work. Checking the development log I only see one date getting submitted, the second of the two which is the one that has the proper format for the database.
Is this all okay? Should I take some extra steps even though this appears to work fine, and more importantly, can someone explain what's going on under the hood that results in this behavior?
Thanks!!
Rails uses the Hash params to store fields submitted. When you declare two or more inputs using the same name, it happens the same as if you do something like
h=Hash.new
h[:name]="foo"
h[:name]="bar"
Result is bar because the foo was overwritten. So the "winner" is always the field value which the browser last appended to postdata.
But if I where you, I would not rely on the browser that the second field gets appended at last.

Symfony will not populate a form with large string after bind()

I am having issues trying to override the value of a form field after submission. Currently the form includes a textarea and file upload input, but only one is used. If the user uploads a file it is parsed into text. I want to use the data that is parsed from the file as the value for the textare when the page reloads, rather than what was in the text box (empty). The content can not be determined until after the bind.
What I did was just bind the data again to manually set the value of the textarea after the document was parse. It works great if I hard code a value for the value, however when I use the full contents of the file, nothing is put in the textarea. There isn't some sort of length limit or something that could interfere with the population is there? I have tried short strings and they work fine, but these files are about 4k in length and won't populate in the text area.
Works
$this->form->bind(array('text'=>'1234'), $request->getFiles('profile_generate') );
Does not work, even though the value text is actually being set.
$this->form->bind(array('text'=>$largeString), $request->getFiles('profile_generate') );
FIX:
I suppose whatever function that Symfony uses to populate the fields from the bind() values has a problem with non-standard characters. Unfortunately it does not complain about it or let on to what the issue really is. After cleaning up the data, it worked fine.
$largeString = preg_replace('/[^(\x20-\x7F)\x0A]+/',' ', $largeString);
$this->form->bind(array('text'=>$largeString), $request->getFiles('profile_generate') );

How do i filter and validate form fields in symfony 1.4?

Im trying to integrate a content filtering API. My plan was to use pre/post validators but I've lost may way somehow.
What i need to do is send the values to the content filtering service. If the response comes back that the content has been filtered it will also return a modified value for the field (basic profanity filtering... matches are replace with asterisks). Thats all well and good i can throw validation errors no problem - simple stuff.
However i dont want just throw errors. What needs to happen is that validation errors are thrown as normal, but the values are modified in the form for re-display.
Basically if someone posts something naughty i want them to get a validation error saying their post has been modified, they can re-submit the now "clean" post, or they can go about editing it to make it clean without the word replacements.
But do clean on a validator either throws an error OR returns cleaned values, not both. How can i go about implementing both? This will be used on many different forms with many different field names, so modifying methods on the form or a form base class isnt really an option - it needs to happen in the validation sub-framework somehow.
You can adjust this plugin for your needs http://www.symfony-project.org/plugins/WebPurifyPlugin

Resources