click-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Carlos Lobato <lobat...@gmail.com>
Subject Re: NumberField validation
Date Thu, 05 Jan 2012 15:02:59 GMT
Hi Arne, I'm not sure this class is compliant with best practices, but here
it is:

public class LengthNumberField extends NumberField {

    private static final long serialVersionUID = 1L;

    public LengthNumberField(String name) {
        super(name);
        setAttribute("onkeypress", "javascript:return
doubleFilter(event);");
        setTextAlign("right");
    }

    /**
     * Construct a LengthNumberField with the given name and label.
     *
     * @param name the name of the field
     * @param label the label of the field
     */
    public LengthNumberField(String name, String label) {
        super(name, label);
        setAttribute("onkeypress", "javascript:return
doubleFilter(event);");
        setTextAlign("right");
    }

    /**
     * Construct a LengthNumberField with the given name and required
status.
     *
     * @param name the name of the field
     * @param required the field required status
     */
    public LengthNumberField(String name, boolean required) {
        this(name);
        setRequired(required);
    }

    /**
     * Construct a LengthNumberField with the given name, label and
required status.
     *
     * @param name the name of the field
     * @param label the label of the field
     * @param required the field required status
     */
    public LengthNumberField(String name, String label, boolean required) {
        this(name, label);
        setRequired(required);
    }

    /**
     * Construct the LengthNumberField with the given name, label and size.
     *
     * @param name the name of the field
     * @param label the label of the field
     * @param size the size of the field
     */
    public LengthNumberField(String name, String label, int size) {
        this(name, label);
        setSize(size);
    }

    /**
     * Construct the LengthNumberField with the given name, label, size and
     * required status.
     *
     * @param name the name of the field
     * @param label the label of the field
     * @param size the size of the field
     * @param required the field required status
     */
    public LengthNumberField(String name, String label, int size, boolean
required) {
        this(name, label, required);
        setSize(size);
    }

    /**
     * Create a LengthNumberField with no name defined.
     * <p/>
     * <b>Please note</b> the control's name must be defined before it is
valid.
     */
    public LengthNumberField() {
        setAttribute("onkeypress", "javascript:return
doubleFilter(event);");
        setTextAlign("right");
    }

    /**
     * Validate the LengthNumberField request submission.
     * <p/>
     * A field error message is displayed if a validation error occurs.
     * These messages are defined in the resource bundle: <blockquote>
     * <pre>org.apache.click.control.MessageProperties</pre></blockquote>
     * <p/>
     * Error message bundle key names include: <blockquote><ul>
     * <li>field-maxlength-error</li>
     * <li>field-minlength-error</li>
     * <li>field-required-error</li>
     * </ul></blockquote>
     */
    @Override
    public void validate() {
        setError(null);

        String value = getValue();

        int length = value.length();
        if (length > 0) {
            if (getMinLength() > 0 && length < getMinLength()) {
                setErrorMessage("field-minlength-error", getMinLength());
                return;
            } else {
                try {
                    NumberFormat format = getNumberFormat();
                    Number number = format.parse(value);

                    double doubleValue = number.doubleValue();

                    if (doubleValue > maxvalue) {
                        setErrorMessage("number-maxvalue-error",
                                getNumberFormat().format(maxvalue));

                    } else if (doubleValue < minvalue) {
                        setErrorMessage("number-minvalue-error",
                                getNumberFormat().format(minvalue));

                    } else {
                        String formattedValue = format.format(number);
                        setValue(formattedValue);
                    }

                } catch (ParseException pe) {
                    setError(getMessage("number-format-error",
getErrorLabel()));
                }
            }

            if (getMaxLength() > 0 && length > getMaxLength()) {
                setErrorMessage("field-maxlength-error", getMaxLength());
                return;
            } else {
                try {
                    NumberFormat format = getNumberFormat();
                    Number number = format.parse(value);

                    double doubleValue = number.doubleValue();

                    if (doubleValue > maxvalue) {
                        setErrorMessage("number-maxvalue-error",
                                getNumberFormat().format(maxvalue));

                    } else if (doubleValue < minvalue) {
                        setErrorMessage("number-minvalue-error",
                                getNumberFormat().format(minvalue));

                    } else {
                        String formattedValue = format.format(number);
                        setValue(formattedValue);
                    }

                } catch (ParseException pe) {
                    setError(getMessage("number-format-error",
getErrorLabel()));
                }
            }

        } else {
            if (isRequired()) {
                setErrorMessage("field-required-error");
            }
        }
    }
}

Best,
-Carlos



On Thu, Jan 5, 2012 at 2:18 AM, <Arne.Bachmann@dlr.de> wrote:

>  Hi Carlos,****
>
> ** **
>
> I would be very much interested in the code!****
>
> Best regards,****
>
> ** **
>
> Arne****
>
> ** **
>
> ** **
>
> *Von:* Carlos Lobato [mailto:lobato87@gmail.com]
> *Gesendet:* Mittwoch, 4. Januar 2012 19:18
> *An:* user@click.apache.org
> *Betreff:* Re: NumberField validation****
>
> ** **
>
> Hi,
>
> Upon comparing the validate method of NumberField, which subclasses
> TextField, I realized NumberField's validate overrides TextField's; so I
> ended up subclassing NumberField to "LengthNumberField" and merging NF and
> TF validate methods in it. In the end, LengthNumberField validates max, min
> value and max, min length. If any of you guys want to implement it you can
> reply and I'll send you the code (:
>
> Thanks to Bob and everybody else for devoting so much of your time in
> answering questions.
>
> Regards,
> -Carlos****
>
> ** **
>
> On Tue, Jan 3, 2012 at 11:38 PM, Bob Schellink <sabob1@gmail.com> wrote:**
> **
>
> Hi,****
>
>
> On Wed, Jan 4, 2012 at 12:49 AM, Carlos Lobato <lobato87@gmail.com> wrote:
> > Hi apache click folks,
> >
> > I have been trying to make my app take a NumberField only if 5 digits are
> > input. With controls like TelephoneField all I have to do is set its
> minimum
> > length to whatever I want it to and the app will display an error if
> > minLength or maxLength aren't met. However, NumberField seems to ignore
> > minimum length, even though the documentation states that there are error
> > messages mapped for NumberFields for min or max length errors.****
>
> NumberField support min and max value, but not min and max length. So
> you can do:
>
> field.setMaxValue(99);****
>
>
>
> > would also like to know what is the proper way to override its validation
> > and do my own thing, because that seems to be another way to accomplish
> > custom validations, right now this field is only validated when it gets
> no
> > input (setRequired).****
>
> You can override the validate() method to do your own validation.
>
> regards
>
> Bob****
>
> ** **
>

Mime
View raw message