Why can't I pass a form field of type file to a CFFUNCTION using structure syntax? - forms

I'm trying to pass a form field of type "file" to a CFFUNCTION. The argument type is "any". Here is the syntax I am trying to use (pseudocode):
<cfloop from="1" to="5" index="i">
<cfset fieldname = "attachment" & i />
<cfinvoke component="myComponent" method="attachFile">
<cfinvokeargument name="attachment" value="#FORM[fieldname]#" />
</cfinvoke>
</cfloop>
The loop is being done because there are five form fields named "attachment1", "attachment2", et al.
This throws an exception in the function:
coldfusion.tagext.io.FileTag$FormFileNotFoundException: The form field C:\ColdFusion8\...\neotmp25080.tmp did not contain a file.
However, this syntax DOES work:
<cfloop from="1" to="5" index="i">
<cfinvoke component="myComponent" method="attachFile">
<cfinvokeargument name="attachment" value="FORM.attachment#i#" />
</cfinvoke>
</cfloop>
I don't like writing code like that in the second example. It just seems like bad practice to me.
So, can anyone tell me how to use structure syntax to properly pass a file type form field to a CFFUNCTION??

In your first codesnippet the value #FORM[fieldname]# evaluates to the name of the file uploaded. So you are sending the filename to your function instead of the name of the field containing the filename.
If you want to stick with the structure notation you might use
<cfinvokeargument name="attachment" value="FORM['#fieldname#']" />
or
<cfinvokeargument name="attachment" value="FORM.#fieldname#" />
instead. I also don't think that there is anything wrong with your (working) second code example.
Edit:
It seems as if <cffile> can not evaluate the filefield if you pass the field using the struct notation, due to some auto evaluation magic of the parameter. After some further investigations I found out that passing only the name of the formfield without the form prefix would also work.
<cfinvokeargument name="attachment" value="#fieldname#" />
The filefield parameter is documented as string, containing the name of the formfield without prefix. My last approach seems more "right" to me. It would even hide the implementation a little bit more. I'm also not so much about composing scope/struct var/keys outside of a component or function and then passing it into the function. This should better be done in the function itself.

Related

TYPO3 Femanager Passwort additionalAttributes

I want to extend the additionalAttributes with a second Attribute. In the original Partials it looks like this:
<f:form.password
id="femanager_field_password_repeat"
name="password_repeat"
class="input-block-level"
value=""
additionalAttributes="{femanager:Validation.FormValidationData(settings:settings,fieldName:'password_repeat')}" />
I try this:
additionalAttributes="{femanager:Validation.FormValidationData(settings:settings,fieldName:'password'),placeholder: '{password_repeat}'}" />
With several Verions of Escaping the femanager:Validation..... Got this Error:
The argument "additionalAttributes" was registered with type "array",
but is of type "string" in view helper
"TYPO3\CMS\Fluid\ViewHelpers\Form\PasswordViewHelper“
Any Ideas?
I think
Validation.FormValidationData()
is a viewhelper that returns a whole array which is expected for the attribute "additionalAttributes".
Because of that it's difficult to extend the array at this place.
But as far as I know the femanager-viewhelper itself offers the possibility to extend the final array, all to do is to give your array as a further argument which is called 'additionalAttributes' as well.
A short example:
<f:form.password
property="password"
additionalAttributes="{
femanager:Validation.FormValidationData(settings:settings,
fieldName:'password',
additionalAttributes:'{required:\'required\',pattern:\'.{8,}\'}')}"
}" />
Notice the array of 2 values (required and pattern).
I also recommend to have a look at the viewhelper on github:
https://github.com/TYPO3-extensions/femanager/blob/master/Classes/ViewHelpers/Validation/FormValidationDataViewHelper.php

JSF - Passing parameter between two RequestScoped Managed beans in JSF page [duplicate]

I am using JSF 2.2 on Glassfish 4.1.
I am trying to pass in a query parameter to as an action method argument as follows:
// Example 1. This does not work.
// at url http://localhost:8080/app/order.xhtml?email=test#email.com
<p:commandButton value="Place order" action="#{orderManager.placeOrder(param['email'])}" />
(Know that param is an implicit EL object.)
In the server log I have configured it to print the method parameter, but I can see that an empty string was passed-in, not "test#email.com" as I expected.
I have confirmed that my overall configuration is working. If I replace the above snippet with the following, then "test#email.com" is output in the server log:
// Example 2. This works.
<p:commandButton value="Place order" action="#{orderManager.placeOrder('test#email.com')}" />
I have also confirmed that my use of EL implicit objects is feasible. The following snippet works if I retrieve the parameter from the FacesContext (after removing the email parameter from placeOrder's signature, of course):
// Example 3. This works.
<p:commandButton value="Place order" action="#{orderManager.placeOrder()}" >
<f:param name="email" value="#{param['email']}"/>
</p:commandButton>
And here is a final mystery, one that truly confuses me, if I use the following snippet, I can retrieve the "email" parameter from both the method parameter and the FacesContext, but recall that the method parameter wasn't retrievable in Example 1!
// Example 4. This works, and BOTH parameters are retrievable!
<p:commandButton value="Place order" action="#{orderManager.placeOrder(param['email'])}" >
<f:param name="email" value="#{param['email']}"/>
</p:commandButton>
Can I pass in an implicit JSF EL object as an action method parameter?
And do you have an explanation for why it seems to work in Example 4, but not Example 1?
The action attribute is evaluated during apply request values phase of the HTTP request triggered by the form submit, which is thus a different HTTP request than the one which produced the HTML output with therin the form (and having the email parameter present in the request).
The <f:param> tag is evaluated during render response phase of the HTTP request which needs to produce the HTML output with therein the form. This thus ends up "hardcoded" in the generated HTML output (on contrary to the EL method arguments in the action attribute!). When the user submits the form, this just gets passed back to the server as a plain vanilla String request parameter (which you would need to convert back if it was originally a complex type).
This has got nothing to do with whether the value is an implicit EL object or not.
That said, there are 2 other ways:
Pass it as hidden input (no, not with <h:inputHidden>).
<h:form>
<input type="hidden" name="email" value="#{param.email}" />
...
</h:form>
Set it as property of a view scoped bean, it'll stay in bean as long as the view lives.
<f:metadata>
<f:viewParam name="email" value="#{viewScopedBean.email}" />
</f:metadata>

HTML5 multi="true" tag doesn't work

i use Eclipse kepler
in dynamic web project
i create html(HTML5) file and use code for Multi select file upload system
<input type="file" multiple="true" />
but can't select multiple with ctrl+click
and my eclipse has warning
Multiple annotations found at this line :
-Undefined attribute value(true)
-Undefined attribute value(true)
any suggestion?
You need to use the attribute's name as its value:
If the attribute is present, its value must either be the empty string
or a value that is an ASCII case-insensitive match for the attribute's
canonical name, with no leading or trailing whitespace.
(http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes)
In other words, just use:
<input type="file" multiple>
or
<input type="file" multiple="multiple">
If it does not work, then the reason is that you are using a browser that does not support the multiple attribute, such as IE 9. To deal with such browsers, you can add some JavaScript that tests whether the input element has the multiple property and if it does not, creates some additional file input elements (possibly in a loop that lets the user specify any number of files).
The warning should really be an error message, since true is not a valid value for the multiple attribute. As #IlmoEuro explains, the value should be empty or multiple. However, the value has in practice no impact; browsers recognize just the attribute name and ignore the value (even if you write multiple="false" for example).

Form param for objects

I have taken over a legacy site, and my spidey sense code smell is going crazy over the form paramming used throughout the site.
For example we have a form that allows you to add a new contact, or edit an existing one dependant on the iContactId being present in the URL. So the code in the top part of the cfm file is.
<cfparam name="form.name" default="">
<cfparam name="form.age" default="">
<cfparam name="form.surname" default="">
<--- More cfparams for every form field--->
<cfif isDefined("URL.iContactId")>
<cfset VARIABLES.contact = contactService.getContact("URL.iContactId")/>
<cfset FORM.name = contact.getName() />
<cfset FORM.age= contact.getAge() />
<cfset FORM.surname = contact.getSurname() />
</cfif>
So essentially we are defaulting all form fields to be empty and then populating them if the iContactId is in the URL. The form at the bottom part of the cfm file uses these variables like this.
<form>
<input name="name" value="#FORM.name#">
Now, for me, the issue with these technique, is that there are around 30 form fields on this page, so I am parramming all form fields, and then setting 30 form fields to the contact objects values if the URL.iContactId variable exists. Am trying to think of a way to remove this duplication - maybe by mapping the form directly to the object?
Any ideas?
Thanks
Don't see why not.
Get getContact() to return an "empty" object for contact ID 0 and populate the form from the empty contact object. By "empty" I mean an object with default values set for each field.
eg.
if (!StructKeyExists(URL,"iContactID")) URL.iContactID = 0;
variables.contact = contactService.getContact(URL.iContactID);
and then in the form.
<input name="name" value="#contact.getName()#">
Only thing to be aware of; If you are doing server-side validation that returns you to this form then you will need to manage making sure that you have a contact object with the valid data in it to populate your form, so that the invalid fields can be corrected.

Html.Hidden() inserting wrong value

When I use a form html helper method in one of my views like <%=Html.Hidden("id", "some id text") %> it creates a hidden input field for me but it puts the wrong value in there.
Instead of getting
<input name="id" type="hidden" value="some id text"/>
I get
<input name="id" type="hidden" value="11000"/>
So the value is being found from somewhere else. In this case it's the primary id of the parent record. So it is an id, it's just the wrong id.
Does anyone have any ideas? I'm pretty sure this didn't happen in MVC1
Model binding always takes precedence. The model binder doesn't know of if a field is hidden. See http://forums.asp.net/t/1559541.aspx and
http://forums.asp.net/t/1703334.aspx
I can thing about following options
Value you are passing to the view is wrong, eg. data passed to view(you can check debugging in controller to see what are you passing into it
You are using ViewData and TempData data with different values which are overridden
else please put here your code so we all can see what is wrong