PDA

View Full Version : extended form validator (enable and disable submit button)



soma13
18 Dec 2010, 3:54 AM
Dear All,

I need a extended form validator for my application.


I have a form with four fields, for example: field1, field2, field3, field4. The normal case is working good:

field1.setAllowBlank(false)
field2.setAllowBlank(false)
field3.setAllowBlank(false)
field4.setAllowBlank(true)

In this case, if field1 and field2 and field3 are not empty then 'submit' button is active.


But i need a complex validator:

if field1 is not empty:
field2: need to fiill
field3: not important
field4: not important

OR

if field3 is not empty:
field1: need to empty
field2: need to empty
field4: not important

Could you give me a nice solution for it?

thank you!

sven
18 Dec 2010, 4:13 AM
Cannot you simple implement the statements as listed here? You can probably copy the FormButtonBInding class and instead if checking for isvalid, you implement the logic you listed here.

You can also use the Validator class and implement the logic there.

soma13
21 Dec 2010, 1:43 AM
Thank you for your answer!

To use the FormButtonBinding class is a good idea!
I made my FormButtonBinding class and it is working wery vell:

public class FromButtonBindingForTariffRecordEditor extends com.extjs.gxt.ui.client.widget.form.FormButtonBinding
{
private FormPanel panel;
private List<Button> buttons;

/**
* default constructor
*
* @param panel
*/
public FromButtonBindingForTariffRecordEditor (FormPanel panel)
{
super(panel);
buttons = new ArrayList<Button>();
this.panel = panel;
}

/**
*
* @param button
*/
@Override
public void addButton(Button button)
{
buttons.add(button);
}

/**
*
* @return
*/
@Override
protected boolean checkPanel()
{
// get reference to fields
DateField fieldX = (DateField) panel.getItemByItemId(RecordEditor.FIELD_ID_VALID_FROM_DATE);
TimeField fieldX= (TimeField) panel.getItemByItemId(RecordEditor.FIELD_ID_VALID_FROM_TIME);
ComboBox fieldX= (ComboBox) panel.getItemByItemId(RecordEditor.FIELD_ID_CURRENCY);
ComboBox fieldX= (ComboBox) panel.getItemByItemId(RecordEditor.FIELD_ID_VAT_RATE);
...

// calculate value of the result variable
boolean result = fieldX .isValid() && fieldX .isValid()...

if (fieldX .getValue() != null)
{
fieldX .setAllowBlank(false);
fieldX .setAllowBlank(false);
fieldX .setAllowBlank(true);
result = result && fieldX .isValid() && fieldX .isValid() && (fieldX .getValue() == null);
}
else if (fieldX .getValue() != null)
{
fieldX .setAllowBlank(true);
fieldX .setAllowBlank(true);
fieldX .setAllowBlank(false);
result = result && (fieldX .getValue() == null) && (fieldX .getValue() == null) && percentagePayoutForOwnerField.isValid();
}
else if (fieldX .getValue() == null && fieldX .getValue() == null && fieldX .getValue() == null)
{
result = false;
}

// show or hide buttons on the form
for (Button button : buttons)
{
button.setEnabled(result);
}

return result;
}
}