Cannot submit form to controller zend framework - model-view-controller

I create a from statically in HTML file in view project which is inside the module visit.
I want the controller to handle the request occur when submitting the form
<form action="addVisit" method="post">
<input type="text" name="number"/>
<input type="submit" name="save"/>
The structure of the project is
module visits
controller Visits and it has action addVisit
when submitting the form an error occur, the url becomes like this when submitting the form
http://localhost/zendApps/InspectionSys/public/visits/visits/VisitsController/addVisit
in the controller there is a function action
public function addVisitAction()
{
echo 'here';
}
what should I do ?

First you might reconsider camel caseing your actions addVisitAction() because if you do then you have to deal with view file names like add-visit.phtml I think it will affect urls as well, I find it simpler just to leave action names as lowercase addvisitAction(), this might be part of what's amiss.
Next identify your form actions as /module/controller/action (if you are not using modules you can omit that param), so your action should at least be /visits/visits/addvisit or /visits/visits/add-visit (not quite sure which will work properly) .
also when using html forms that were not generated by Zend_Form you can access the values using $this->getParams() or $this->getParam('paramName') instead of $this->getValues().

Related

How to get a Sinatra erb to report back its referring page in params

For learning purposes, I am developing a Ruby/Sinatra "to do list" program. In views, I have the usual index page, which lists uncompleted tasks, and also a completed page. On both pages (and on many future user-created categories pages), there is a similar table, with similar methods for moving and deleting items. These call the same methods, or I'd like them to. But if they call the same methods, at the end of those methods the user is redirected to the index view. But if users are on the completed page, they want to stay on that page after deleting an item (or whatever).
To solve this problem, I think what I need to do is pass a variable from the erb file to the route block, reporting what view (erb file) the user was most recently on. I thought there would be some built-in Sinatra methods to report this, and I looked hard (e.g., I tried all plausible-sounding ways of accessing the request object), but I couldn't find any. Then I tried using <input type="hidden" name="pg_type" value="completed"> but for reasons totally mysterious to me, this doesn't work (maybe because Sinatra uses type="hidden" to route PUT and DELETE requests?). When I inspect the parameters with p params, I see that the pg_type param was created, but its content is an empty string ("").
An example form from (this is a template used by the other views) task_table.erb is this:
<form method="post" action="/delete/<%= task.id %>">
<label for="delete_button">
<input type="hidden" name="pg_type" value="<% #pg_type %>">
<button type="submit" name="delete">Delete</button></label>
</form>
Here is the route block for that method:
post('/delete/:id') do
store.delete(params[:id].to_i)
session[:message] << " " + "Deleted task!"
redirect "/" if params[:pg_type] == "index"
redirect params[:pg_type] # But params[:pg_type] always == "" :-(
end
So how can a pass info about the referring page from my erb pages to my route blocks? Or, if my strategy for solving the problem of returning the user to the correct page is wrong or could be improved (I wouldn't be surprised), please explain.
I'd love to have any more detailed feedback on this program. Other problems I'm trying to solve are how to add unit tests of put and delete methods and how to add user accounts (not started with that though).
In your code you forgot a '=' before #pg_type:
<input type="hidden" name="pg_type" value="<%= #pg_type %>">

How to recognaize which ajax form it is in Django?

I have view which takes care of all the Ajax submits from the client side. And to differentiate them by I uses different submit button names such as this one
<input type="submit" value="Send" name="send_message">
Suggested from this question.
The only problem is that from the view side it doesn't seems to carry the name to the server side so I cannot use the following if-statement
if 'send_message' in request.POST:
It works if I send it normally with page fresh. But I want to use it with Ajax.
I came up with a hack that you can add this name with jQuery. Simply by after serializing() your data you then concatenate the name attribute by data += "&send_message"
Then the if statement will work. But it doesn't seems so clean. So I wonder if there's a better way to handle this? Or should I make different views to handle the different Ajax calls I have?
You really should post each form to a different URL.
If not, you could add a hidden input with the name of the form as the value.
<input name="form_name" type="hidden" value="form_1" />
views.py:
form_name = request.POST['form_name']

Html.RenderAction cannot find my controller

I have a razor masterpage (_Layout.cshtml) where I layout a 3 column website. In one of the side columns I want to display a "Login Control"
From my readings, I can use Html.RenderAction to call my LoginController and it will display the login view in the side column.
But, when I run it and point it to a Controller/View to fill the RenderBody(), the call to Html.RenderAction("Index", "LoginController") fails with this error.
"The controller for path '/[insert path to a Controller/View to fill the
RenderBody()]' was not found or does not implement IController. "
So, what am I doing wrong?
My code really is as simple as:
<div id="Navigation">#{ Html.RenderPartial("Test"); }</div>
<div id="Main">#RenderBody()</div>
<div id="Misc">#{ Html.RenderAction("Index", "LoginController");}</div>
And in my controllers folder, I have the controller for the RenderBody and the LoginController.
When specifying controller names by convention in MVC, you don't include the "Controller" part.
Html.RenderAction("Index", "LoginController")
wouldn't work unless you had a controller named "LoginControllerController"
try
<div id="Misc">#{ Html.RenderAction("Index", "Login");}</div>

Requests with AJAX in a portlet (Liferay)

I have an issue with my portlet and I don't know exactly how to solve it.
My portlet adds or retrieves info from liferay's DB by inserting a name in 2 text fields.
After pressing the submit button, I see the response from the server, a JSON response like this:
{"id":301,"name":"Pepo"}
If a user correctly inserted or if the search throws a good result. I have to go back in the browser to see the portal again.
How can I use AJAX to pass the following URL dynamically from the portlet to the server without refreshing the page afterwards?
http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName=findByName&servletContextName=TrabajadorPlugin-portlet&serviceParameters=[param1]&param1=NameInsertedByUser
Now I'm using the <form> tag like this:
<%
//Shows "New Employee" in the text field when portlet is rendered, or gets the user input and pass it as a param to the URL
PortletPreferences prefs = renderRequest.getPreferences();
String employee = (String)prefs.getValue("name", "New Employee");
%>
<form id="postForm" method="post" action="http://localhost:8080/c/portal/json_service">
<input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil" />
<input name="serviceMethodName" type="hidden" value="create" />
<input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet" />
<input name="serviceParameters" type="hidden" value="[param]" />
<input name="param" type="text" value="<%=employee%>" />
<input type="submit" value="Submit"/>
</form>
I understand how AJAX works, but I need some help to create my function in order to achieve the URL to be correctly sent to the server for both GET and POST requests. This is my first try with AJAX.
Thank you very much, hope somebody understands my problem and could help me.
First of all, I see no point at all to use JSON services here. Just write ordinary portlet with MVC Controller, and in controller write action handling for corresponding actions (storing data, searching etc).
In controller you can directly call static methods like create or findByName from java class com.liferay.test.service.TrabajadorServiceUtil (or TrabajadorLocalServiceUtil) - that's how people usually do it.
If for some reason you really must use JSON, you should of course do these actions with AJAX calls - and render results using JavaScript.
Updating after question update:
The easiest and most correct way to send AJAX requests in Liferay would be to use AlloyUI JS framework that's a part of Liferay. You can read more on how to send AJAX requests with it here: http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/4733559
In order to accomplish your goal I'd suggest implementing processAction(ActionRequest actRequest, ActionResponse actResponse) method in your controller/portlet.
In order to actually send data to it you'll have to have actionURL, which you can create using for example portlet:actionURL tag:
<portlet:actionURL /> or with Java code PortletURL actionUrl = portletResponse.createActionURL();
Then just submit your form using POST to this URL, and in actionRequest you'll have your parameters.

ASP.NET MVC 3.0 Partial inside and outside the form with unobtrusive data validation attributes issue

I have from with conditional content, according with conditions using jQuery i am including or excluding this parts from the form elements.
Here is my functions:
function MoveInsideForm(id) {
$("#" + id).insertAfter("#myForm")
}
function MoveOutsideForm() {
$("#myPartial1").insertAfter("#element-outside-from");
$("#myPartial2").insertAfter("##element-outside-from");
}
The problem is with insertAfter() it is does not getting copied HTML 5 custom attributes
For example i have an element like that
<input data-val="true" data-val-required="*" id="MyInput" name="MyInput" type="text" value="" class="input-validation-error"/>
But insertAfter() copying it like that:
<input id="MyInput" name="MyInput" type="text" value=""/>
Is there any way i can say to insertAfter() to copy HTML 5 attributes as well?
My jQuery version is 1.6.1.
UPDATE:
Thanks guys for comments.
Here is the thing, when i am rendering my partials inside the form the inputs getting generated with unobtrusive data attributes, but if i am rendering my partials outside the form, the unobtrusive data attributes initially not getting included in to inputs.
So when i am rendered the partials outside the form they initially does not contain data attributes.
So it is not issue with jQuery insertAfter(), is it the nature of unobtrusive data validation attributes generation?
In order to generate unobtrusive validation, MVC must have a FormContext. When you place an HtmlHelper outside of Html.BeginForm or Ajax.BeginForm, the HtmlHelpers will not have unobtrusive validation attributes, unless you manually instantiate a FormContext. You can manually instantiate a FormContext by inserting the following code before your helpers:
this.ViewContext.FormContext = new FormContext();
If you are placing your helpers before a BeginForm(), then be sure to clear the FormContext after your helpers and before beginning the form.

Resources