Struts 1 form validation - struts-1

I'm using struts 1 in legacy project. I want to validate form. I have read good article about that. But I don't understand what struts do if form's validate method returns non-empty ActionErrors. I have specified validate="true" and input="somepage.jsp" in action, but I get a blank page if my form's validation doesn't pass. As I understnad it has to stay at page specified in input? Am I right?
edit
<action path="/struts/BlaBlaAction"
type="com.example.BlaBlaAction"
name="BlaBlaForm"
validate="true"
scope="request"
input="/struts/blablainput.jsp">
<forward name="someaction" path="/struts/AnotherAction.do"/>
<forward name="error" path="/error.html" redirect="true"/>
</action>
Folder struts resides in the root of my app

You also need to display the validation errors using <html:errors/> tag, in somepage.jsp
If your form's validate method returns non-empty ActionErrors, Struts would redirect to the JSP you have defined as input. And then if you have <html:errors> in the input JSP, all the ActionErrors are iterated and displayed.

You may have forgot the "failure" forward in your struts XML configuration. Look at this example:
<action path="/LogonSubmit" type="app.jcj.LogonAction" name="logonForm"
scope="request" validate="true" input="/pages/Logon.jsp">
<forward name="success" path="/pages/Welcome.jsp"/>
<forward name="failure" path="/pages/Logon.jsp"/>
</action>

Related

Validation with one action and multiple JSP pages in Struts 2

I have a form with a section where I submit a comment separate from updating the entire form.
To keep my JSP manageable I use the following Struts 2 action:
<action name="maininfo" class="MainInfo">
<result name="success" type="tiles">maininfo</result>
<result name="addFundingComment" type="tiles">addFundingComment</result>
<result name="input" type="tiles">maininfo</result>
</action>
The maininfo tile displays the main form JSP page. The addFundingComment tile displays the form to submit the comment.
My issue is if validation fails the "input" result has to go to either the maininfo tile or the addFundingComment tile but I need it to go to the tile corresponding to the form the validation failed for.
I can get around this by putting the addFundingComment JSP code in the maininfo JSP code and using a <s:if> to display the form I want but I think having two separate JSP files makes each one easier to manage and debug.
I want to use one Action to make it easier to keep all of the maininfo field changes when the user submits a comment.
Is there a way to have one Action with <results> which go to different JSP pages and have validation failures return to the corresponding JSP page?
You can use dynamic result configuration to return the corresponding view. The dynamic property is evaluated via OGNL, so you have to create a getter method to return the location for the input result.
<result name="input" type="tiles">${inputName}</result>
In the action class
public String getInputName() {
String inputName = "maininfo";
if (actionName.equals("addFundingComment")
inputName = "addFundingComment";
return inputName;
}

Magento layout XML custom helper

I'm trying to pull in a system configuration value to a layout file. I figured the only way to do this was to create a function in my Data.php helper and then use the function in the layout xml. For some reason, it doesn't work.
public function getStoreVar() {
return Mage::getStoreConfig('path/to/var');
}
Then in the xml I have something like
<block type="my/module" template="path/to/template.phtml">
<config_id helper="module/getStoreVar" />
</block>
I have a Mage::log on the getStoreVar function and can see that it isn't even being called. What am I missing?
Just figured out what I was doing wrong. Realized the code I was referencing to do this, addLink, had the helper inside an action tag with the helper on an argument for the action method. Using a method from my block class fixed it for me. For example:
<action method="setStorVar">
<var helper="module/getStoreVar" />
</action>

Remove top navigation using ifconfig in magento

I'm working on a module in which we have a configuration as "advanemenu/general/enabled"
By using this config I am being able to add items conditionally to my magento frontend.
Ex.
<reference name="head">
<action method="addItem" ifconfig="advancemenu/general/enabled"><type>skin_css</type><name>css/advancemenu.css</name></action>
</reference>
Now similarly I want to remove the top navigation if the config value is enabled.
I tried the following but without any result...
<remove ifconfig="advancemenu/general/enabled" name="catalog.topnav" />
Incase the ifconfig works with <action> then is there any way to remove top navigation using this method.
Please help me if anyone knows how to do this.
(Thnx in Advance)
IfConfig only work with action method. When you call action in a xml layout this parse in a call to funcion in the block instance.
you can see this in:
file: app/code/core/Mage/core/Model/layout.php around line 289
protected function _generateAction($node, $parent)
{
if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
if (!Mage::getStoreConfigFlag($configPath)) {
return $this;
}
}
but a posible solution for this is add a template only in case of true value. For example
<reference name="head">
<action method="setTemplate" ifconfig="advancemenu/general/enabled">
<template>route/to/template</template>
</action>
</reference>
then, only when you have enable your module, you have template associate to this block, in another case, your block don´t have template, then don´t load.
You can remove any block using the unsetchild method.
For the above case
<reference name="top.menu">
<action method="unsetChild" ifconfig="advancemenu/general/enabled">
<name>catalog.topnav</name>
</action>
</reference>
It will help for the conditional remove statement.

Magento - custom module that captures user e-mail as soon as it is entered

In a custom extension, I'm thinking I might like to capture the user's e-mail address as they enter it in the various places that it can be entered (Checkout, Create Account, Log In). I might not, but if I did...
I think the best way to do this would be to inject a piece of JavaScript, but I'm not sure how to do that. I guess I need to specify a block in a layout file? So maybe,
app/design/frontend/base/default/layout/mymodule.xml
<default>
<reference name="before_body_ends">
<block type="mymodule/myblock" name="not sure" as="not sure" />
</reference>
</default>
and in the block class, the _toHtml() method could return some JavaScript that added an event handler [onchange] to the email address field, and the implementation of the event handler made an AJAX call to a controller passing the e-mail address entered.
I think the above layout class would result in JavaScript being injected in every page. I would only want it on the ones that contain an email address entry field. What layout file should I create to do that (location, name, and contents).
I went through the demo site and viewed the source of the pages where an e-mail address is entered. The id of the email address field is different in each case, so the JavaScript would have to account for that.
Many Thanks for any help.
If all you want to do is to include a .js file on every page you should add it to page.xml:
<layout>
<default>
<reference name="root">
<action method="addJs"><script>relative/path/from/js/folder</script></action>
</reference>
</default>
</layout>
Wait for the dom to load and setup your javascript observers.

struts2 validation result input cannot be an action

I have an action Registration.java which is used for users to create an account.
this action class has two methord: doList and execute.
doList gets data from the database and renders the initial jsp page with some s:select tags.
execute do the actual business logics.
in the struts.xml:
<action name="InitList" method="list" class="......Registration" >
<result name="success">/..../...../Registration.jsp</result>
<action name="Registration" class="......Registration">
**<result name="input" >InitList.action</result>**
<result name="next" type="redirect">InitListReg.action</result>
</action>
I also have a validation config file: RegistrationAction-Registration-validation.xml
when i created some validation error and the intial page was not displayed with the error: InitList.action is not available. It seems strut2s did not recognized the action InitList. When i change the result input like this:
<action name="Registration" class="......Registration">
**<result name="input" type="redirect">InitList.action</result>**
<result name="next" type="redirect">InitListReg.action</result>
</action>
the initial page was displayed successfully, but the validation error messages were lost and not displayed because of "redirect".
So i wonder if input can be an action or only support jsps. Or how can i fix my problem?
When you use the Struts2 Validations togehther with redirect you need the MessageStoreInterceptor.

Resources