Re: Validation on my own

From: Kieran Kelleher (kelleher..mail.com)
Date: Tue May 03 2011 - 12:51:53 UTC

  • Next message: Peter Müller: "Re: Validation on my own"

    You can implement custom validation by simply adding validation methods to any class that implements NSValidation interface.

    You can throw custom 'nice' message from your own Validation method.

    Also, if you use Wonder, the default messages will be 'nice' and Wonder allows for nice localization too.

    There is a bit for you to learn, but it will be worth it.

    So for your user EO, you can add custom validation methods.

    For example for your password logic below, this would normally be added in the EO class as sth like:

       public String validatePassword(Object aValue) throws NSValidation.ValidationException {
            if (aValue == null || aValue.toString().length() < 5) {
                throw new NSValidation.ValidationException("Password must be set and must contain at least 5 characters.");
            }
            return aValue.toString();
        }

    Your validate method gets called automatically in takeValuesFromRequest phase of request handling.

    Your current approach will not work because you are validating in the invokeAction phase which comes after the takeValues phase.

    There is a book written by a bourbon-loving fairly smart Canuck entitled "Practical WebObjects". Chapter 5 explains Validation in very easy to understand terms. If you read that, you will then know enough to be dangerous. ;-)

    Regards, Kieran

    On May 3, 2011, at 8:31 AM, Peter Müller wrote:

    > Hello WO-Community,
    > i've a coding-problem because WO does a job for me, I want to do on my own.
    >
    > I've a modelclass called "User" with a view fields:
    > id, firstname, lastname, email, ...
    >
    > id is primary key, firstname, lastname and a view others are "required" fields.
    >
    > On my component I have a Form to set this fields. The "save"-Button is linked to my method:
    >
    > public WOComponent save() {
    > alertMessage=null;
    > if(selectedUser.lastname()==null) {
    > alertMessage="Lastname of user must be set!";
    > return null;
    > }
    > if(selectedUser.firstname()==null) {
    > alertMessage="Firstname of user must be set!";
    > return null;
    > }
    > if(selectedUser.username()==null) {
    > alertMessage="Username of user must be set!";
    > return null;
    > }
    > if(selectedUser.password()==null || selectedUser.password().length()<5) {
    > alertMessage="Password of user must be set and must contain at least 5 signs.";
    > return null;
    > }
    > session.defaultEditingContext().saveChanges();
    > selectedUser = null;
    > return null;
    > }
    >
    > As you can see, I want to do the validation for the fields on my own (because I want to make a "nice" message to the user, and later also multilingual).
    >
    > But now the problem: If I click "save" on my component, first the form will be set, and WO validates, if all required fields are set. If not, it will print a message in console and "reset" the fields. My validation will be ignored.
    >
    > Have I any chance to validate "required" fields on my own, and ignore WO's validation?
    >
    > Thank you very much,
    > All the best from germany
    > Peter M.
    > --
    > Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
    > belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de



    This archive was generated by hypermail 2.0.0 : Tue May 03 2011 - 12:53:00 UTC