JIRA - form validation

b April 9, 2012

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?

1 answer

1 accepted

1 vote
Answer accepted
Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2012

Hi,

in action class override doValidation method and fire error into the error collection in service context (or error message).

Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2012

VM template:

#if($action.getHasErrors())
							#set($err = $action.getErrors())
						#end
    
    					#macro( errortablerow $errors $key)
							#if ($errors && $errors.get($key))
								<tr>
                					<td class="formErrors"> </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>

Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2012

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;
	}

}

Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2012

Error key match html element name, so

public static final String ERROR_USERNAME = "userName";

Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2012

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;
	}

b April 9, 2012

Is that possible to get field component inside of class?

satya L June 19, 2012

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.

Suggest an answer

Log in or Sign up to answer