is there a way to turn off form params passing to a controller in grails? - grails-2.2

I have this simple two methods in PaymentController
def checkout3(){
}
def receipt3() {
render params
return
}
The checkout3.gsp is a simple form as follows
<g:form name="payform" action="receipt3">
<g:textField name="cardNumber"/>
<g:submitButton name="submit" value="submit"></g:submitButton>
</g:form>
Now i go to checkout3 url and in the form enter something in cardNumber field and click submit
The output of receipt3 is as follows: You can see that cardNumber form param is missing. I have tried with other fields and no fields values are passed to the controller.
['action':'receipt3', 'controller':'payment']
The same example works on other controllers. i.e created the same above methods and views in a new test controller called TestController and it prints the cardNumber as follows where i had entered "testing" in cardNumber field
['submit':'submit', 'cardNumber':'testing', 'action':'receipt2', 'controller':'test']
So my question is whether there is a way to disable form params passing to a particular controller? Please note that the first example wasn't written by me so i am investigating what is the reason for params not showing form params in the first case. I appreciate any help! Thanks!

Related

Can I generate a form depending on the results of a view?

I need to create a form which depends on the results of a view. I can generate it within the view and/or create a view template in order to do this....But how do I process it?
How do I connect my form/template generated form to a form_submission function?
When this custom/dynamic form is submitted I need to call some drupal functions to create some content in the site.
I did not find any way to do it in a view, but what I did was:
- Create a form programatically, making only a hidden field for NIDS
- Place my view next to that form using AJAX
- When the view is updated I copy the results of the view to the NIDS field and submit the form (This reloads the page)
- Form submission has 2 behaviours, 1 to re-generate the form when provided with NIDs, and other one for the actual form created by the NIDS result of my view.
Just in case this helps someone

CFWheels: Display form errors on redirectto instead of renderpage

I have a form which I am validating using CFWheels model validation and form helpers.
My code for index() Action/View in controller:
public function index()
{
title = "Home";
forms = model("forms");
allforms = model("forms").findAll(order="id ASC");
}
#startFormTag(controller="form", action="init_form")#
<select class="form-control">
<option value="">Please select Form</option>
<cfloop query="allforms">
<option value="#allforms.id#">#allforms.name#</option>
</cfloop>
</select>
<input type="text" name="forms[name]" value="#forms.name#">
#errorMessageOn(objectName="forms", property="name")#
<button type="submit">Submit</button>
#endFormTag()#
This form is submitted to init_form() action and the code is :
public function init_form()
{
title = "Home";
forms = get_forms(params.forms);
if(isPost())
{
if(forms.hasErrors())
{
// don't want to retype allforms here ! but index page needs it
allforms = model(tables.forms).findAll(order="id ASC");
renderPage(action="index");
//redirectTo(action="index");
}
}
}
As you can see from the above code I am validating the value of form field and if any errors it is send to the original index page. My problem is that since I am rendering page, I also have to retype the other variables that page need such as "allforms" in this case for the drop down.
Is there a way not to type such variables? And if instead of renderPage() I use redirectTo(), then the errors don't show? Why is that?
Just to be clear, I want to send/redirect the page to original form and display error messages but I don't want to type other variables that are required to render that page? Is there are way.
Please let me know if you need more clarification.
This may seem a little off topic, but my guess is that this is an issue with the form being rendered using one controller (new) and processed using another (create) or in the case of updating, render using edit handle form using update.
I would argue, IMHO, etc... that the way that cfWheels routes are done leaves some room for improvement. You see in many of the various framework's routing components you can designate a different controller function for POST than your would use for GET. With cfWheels, all calls are handled based on the url, so a GET and a POST would be handled by the same controller if you use the same url (like when a form action is left blank).
This is the interaction as cfwheels does it:
While it is possible to change the way it does it, the documentation and tutorials you'll find seem to prefer this way of doing it.
TL; DR;
The workaround that is available, is to have the form be render (GET:new,edit) and processing (POST:create,update) handled by the same controller function (route). Within the function...
check if the user submitted using POST
if it is POST, run a private function (i.e. handle_create()) that handles the form
within the handle_create() function you can set up all your error checking and create the errors
if the function has no errors, create (or update) the model and optionally redirect to a success page
otherwise return an object/array of errors
make the result error object/array available to view
handle the form creation
In the view, if the errors are present, show them in the form or up top somewhere. Make sure that the form action either points to self or is empty. Giving the submit button a name and value can also help in determining whether a form was submitted.
This "pattern" works pretty well without sessions.
Otherwise you can use the Flash, as that is what it was created for, but you do need to have Sessions working. their use is described here: http://docs.cfwheels.org/docs/using-the-flash and here:http://docs.cfwheels.org/v1.4/docs/flashmessages
but it really is as easy as adding this to your controller
flashInsert(error="This is an error message.");
and this to your view
<cfif flashKeyExists("error")>
<p class="errorMessage">
#flash("error")#
</p>
</cfif>

Flask - how to get query string parameters into the route parameters

Im very much new to Flask, and one of the starting requirements is that i need SEO friendly urls.
I have a route, say
#app.route('/sales/')
#app.route(/sales/<address>)
def get_sales(addr):
# do some magic here
# render template of sales
and a simple GET form that submits an address.
<form action={{ url_for('get_sales') }}>
<input type='text' name='address'>
<input type=submit>
</form>
On form submission, the request goes to /sales/?address=somevalue and not to the standard route. What options do I have to have that form submit to /sales/somevalue ?
I feel like I'm missing something very basic.
You would need to use JavaScript to achieve this so your template would become:
<input type='text' id='address'>
<button onclick="sendUrl();">submit</button>
<script>
function sendUrl(){
window.location.assign("/sales/"+document.getElementById("address").value);
}
</script>
and your routes similar to before:
#app.route('/sales/')
#app.route('/sales/<address>')
def get_sales(address="Nowhere"):
# do some magic here
# render template of sales
return "The address is "+address
However, this is not the best way of doing this kind of thing. An alternative approach is to have flask serve data and use a single-page-application framework in javascript to deal with the routes from a user interface perspective.
There is a difference between the request made when the form is submitted and the response returned. Leave the query string as is, as that is the normal way to interact with a form. When you get a query, process it then redirect to the url you want to display to the user.
#app.route('/sales')
#app.route('/sales/<address>')
def sales(address=None):
if 'address' in request.args:
# process the address
return redirect(url_for('sales', address=address_url_value)
# address wasn't submitted, show form and address details
I'm not sure there's a way to access the query string like that. The route decorators only work on the base url (minus the query string)
If you want the address in your route handler then you can access it like this:
request.args.get('address', None)
and your route handler will look more like:
#pp.route('/sales')
def get_sales():
address = request.args.get('address', None)
But if I were to add my 2 cents, you may want to use POST as the method for your form posting. It makes it easier to semantically separate getting data from the Web server (GET) and sending data to the webserver (POST) :)

Adding inline formsets without javascript

I am using this post as a reference: Django: Adding inline formset rows without javascript
if request.method=='POST':
PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
if 'add' in request.POST:
cp = request.POST.copy()
cp['prim-TOTAL_FORMS'] = int(cp['prim-TOTAL_FORMS'])+ 1
prims = PrimaryFunctionFormSet(cp,prefix='prim')
I am trying to add inlines rows without javascript and came across a few things I didn't understand in the implementation referenced above.
How do you get a button called 'add' in your form or view or template?
'prims' was defined in the form but 'prim-TOTAL_FORMS' was not. Do you need to define 'prim-TOTAL_FORMS' somewhere?
How can you call 'prim-TOTAL_FORMS' before you defined 'prim'?
Is this all writtein in the views.py?
Thanks for the help, and sorry if the questions are novice!
a) Add in a button with a name and value you wish, and it will be submitted as POST data, with the key in the POST being the name, and the value the value.
<input type="submit" value="true" name="add">
Read up on HTTPRequest objects in Django here.
b) No. The example uses an inlineformset_factory. This sets the TOTAL_FORMS value for you on POSTing, using the given prefix 'prim'. They are taking a copy of the current TOTAL_FORMS count, and adding 1, then returning a new formset.
c) prim is defined in the initial formset, and you are ensuring you are returning a new one with the same prefix. This code is if the form is submitted, so you know 'prim' has been set to the prefix.
d) Yes!

Unit Testing a Django Form Containing Multiple Submit Buttons

I am writing unit tests for a page that uses several Submit buttons to control logical flow through my Django application.
Unfortunately, I can't figure out how to get the response to return the submit values in the unit testing framework. The Django unit testing documentation for post indicates its form is the following:
post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)
In the case of a Delete button of the form:
<input type="submit" name="delete" value="Delete" />
I've tried placing the Delete value in as data, i.e.:
response = self.client.post(url, {'name':'delete'}, follow=True)
but that doesn't seem to work. I need to have the name values in order to exercise the code paths that they trigger. In the views, the logic takes the form of:
if 'delete' in request.POST:
<do something>
I'm assuming that I make use of **extra somehow to get these values but I haven't had much luck with it either.
Any suggestions?
The data dictionary should map input names to values. In your case, the name is delete, and the value is Delete. So the dictionary should be:
{'delete': 'Delete'}