PDA

View Full Version : Rules Source


ulil
Oct 8th, 2004, 05:46 AM
I've make rules for input validation :


public class TextValidation implements RulesProvider
{
private RulesSource rulesSource;

public TextValidation()
{
DefaultRulesSource rulesSource = new DefaultRulesSource();
rulesSource.addRules(getRules());
this.rulesSource = rulesSource;

}

protected Rules getRules()
{
Rules rules = Rules.createRules(getClass());
Constraints c = Constraints.instance();
rules.add("inputString", c.all(new Constraint[] { c.required(),
c.maxLength(8) }));
return rules;
}

public PropertyConstraint getRules(String property)
{
return rulesSource.getRules(getClass(), property);
}

}


The form :

public class Text extends JFrame implements ActionListener
{
private JTextField textUserID = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("Ok");
private JButton cancel = new JButton("Cancel");
private TextValidation validation = new TextValidation();

public Text()
{
ok.addActionListener(this);
FormLayout layout = new FormLayout("r:50dlu,3dlu,80dlu","");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();

builder.append("User ID", textUserID);
builder.nextLine();
builder.append("Password",password);
builder.nextLine();
builder.append(ButtonBarFactory.buildRightAlignedB ar
(ok,cancel),3);
getContentPane().add(builder.getPanel());
pack();
show();
}

public static void main(String args[])
{
Text text = new Text();
}

public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Ok"))
{
//here i want to validate value of user ID (textUserID)
}
}
}


How I can register my textUserID ?

Thx before


Ulil

pdbruycker
Oct 8th, 2004, 10:09 AM
You need to use a Form implementation to use the validation. You have two options:
1. provide an application wide RulesSource
2. make your form model object implement RulesProvider

More info on validation: http://opensource.atlassian.com/confluence/spring/display/RCP/Forms#Forms-Validation

When you want to work without a form (as you show in your code), you will have to register the appropriate listeners to your input fields, and call the validation framework from their methods.

Greetz,

Peter