Hi,
I created view .vm that contains html input fields (like textarea, or textbox) inside of form. I serve form by class that extends JiraWebActionSupport. And my question is, how could I validate input fields? Is that possible to get (inside of class) view fields?
I would appreciated for any code examples?
Hi,
in action class override doValidation method and fire error into the error collection in service context (or error message).
VM template:
#if($action.getHasErrors()) #set($err = $action.getErrors()) #end #macro( errortablerow $errors $key) #if ($errors && $errors.get($key)) <tr> <td class="formErrors">&nbsp;</td> <td valign="top" class="formErrors"> <span class="errMsg">$errors.get($key)</span> </td> </tr> #end #end #errortablerow( $err "userName" ) <tr> <td #if($err && $err.get("userName")) class="fieldLabelArea formErrors" #else class="fieldLabelArea" #end> $i18n.getText("up.add.element.user") </td> <td #if($err && $err.get("userName")) class="fieldValueArea formErrors" #else class="fieldValueArea" #end> <select id="userName" name="userName" style="width: 210px" > #foreach ($user in ${action.getAvailableUsers()}) <option value="${user.getName()}" #if($el.getUser().getName() == $user.getName()) selected #end >${user.getDisplayName()}</option> #end </select> </td> </tr>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Example action class:
public class TestAction extends JiraWebActionSupport { private static final String ERROR_INVALID_USER = "up.web.action.error.invalid.user"; private String userName; public TestAction() { } @Override protected void doValidation() { User u = UserUtils.getUser(userName); if (u == null) { getJiraServiceContext().getErrorCollection().addError(Constants.ERROR_USERNAME, getJiraServiceContext().getI18nBean().getText(ERROR_INVALID_USER)); } // Validation continue } @Override protected String doExecute() throws Exception { if (hasAnyErrors()) return ERROR; // Some logic code return INPUT; } public Collection<User> getAvailableUsers() { return UserUtils.getAllUsers(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Error key match html element name, so
public static final String ERROR_USERNAME = "userName";
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can use pass class into the action constructor. In example below MyService is my own component class implementation.
private final WebResourceManager webResourceManager; private final MyService service; public TestAction(WebResourceManager webResourceManager, MyService service) { this.webResourceManager = webResourceManager; this.service = service; }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
But how i can validate input field of type "file" (for ex: <input type="file" name="excelFile" id="excelFile" value="$!excelFile") in 'doValidation()' of my Action class.
I have only this single fileld in my .VM file.
please share if any code example available.
Regards,
Satya.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.