Re: EOF objects and NON NULL validation
Serge Cohen <[email protected]> Fri, 7 Nov 2003 11:28:07 +0100
| Newsgroups | gmane.comp.web.webobjects.eof,gmane.comp.web.webobjects.devel |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Chuck, Arturo; Thanks for the comment, indeed I've been reading this article (together with http://www.stepwise.com/Articles/Technical/2003-09-29.01.html), which are both very interesting. Having you tell me to re-read it was nice because I realised now that the author 'confirm' my understanding of the problem; quoting the article: "For Enterprise Objects, these methods are called after any constraints in the EOModel have been checked. If an attribute is a String, for example, and the field in the database only allows 10 characters, and a user enters 11, the value will be immediately rejected before any validation methods are called. The same is true if an attribute does not allow null values and the user submits an empty field. If an attribute does allow nulls, however, in your validation method you should check for nulls prior to sending messages to values. " The solution they provide is to handle this kind of validation in the component (if the constraints of the EOModel are not satisfied get the exception in the validationFailedWithException of the WOComponent) (as Arturo Pérez pointed as well). Unfortunately, I was trying to avoid that... Let me elaborate my design (I'd be glad to get some better idea/tips on the design): I have a form, where user can input data for a few different object (indeed, 5 to 10) of 4 different classes. For each of these entry, I'd like to be able to conditionaly display an error message and ask user to correct there previous entry. To keep the entry of the user when a validation fail I copied the idea of the second stepwise article (Keeping Invalid Data with WebObjects Components). Now the 'problem' is to get the proper conditional remarks to the user, for that after each entry field I have a WOConditional including a single WOString. My idea was that each object has also two dictionary using entry key as keys and one called isInvalid, the other validationString... then after a WOTextField bound to aFoo.label the conditional would be bound to aFoo.isInvalid.label and the WOString bound to aFoo.validationString.label. The nice thing about this design is that each entity is responsible to generate its own message for the validation of it's attributes, so I don't have to duplicate when I use the entity again in another component. The second advantage (even more important indeed ?) is that when I have a repetition of object in a WORepetition (all of the same class, with the same keyPath), each object is responsible for its own validation (it would be more complicated if the isInvalid dictionary was in the component, and I use isInvalid.aFoo.label for the conditional and validationString.aFoo.label for the message... in which case all the objects in the repetition would get exactly the same validation message). Unfortunately, for this design to work, I need the validationKey method to be called even if the EOModel constraints are not satisfied... which is not the case by default. So my next question is: Should I better change my design principle (handling validation error messages in each EO entity classes), or override the default behaviour of EOCustomObjects.validateTakeValueForKeyPath() method? If I'm going for the override, I'd still like the default behaviour to be used within any EOEditiongContext.insertObject or saveChange... methods! As previously, thanks for any comments/idea/tips/remarks. Serge. Le vendredi, 7 nov 2003, à 01:16 Europe/Amsterdam, Chuck Hill a écrit : > You kind of on the wrong track. Try reading this: > http://www.stepwise.com/Articles/Technical/2001-06-13.01.html > > Chuck > > > At 07:20 PM 06/11/2003 +0100, Serge Cohen wrote: >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> Hello List; >> >> I'm developing a Web-Application, using WebObject and EOF together. >> In my data model, one of the attributes (lets call it Label, for the >> entity Foo) should never be empty, so in EOModeler I've set the >> attribute so that it does not allow nulls. >> >> I have a web form to generates Foo entities, and there is a >> WOTextField >> used to get the content of 'Label'. >> If the user let the Label text field empty, I get an exception thrown, >> because foo.label is not allowed to be null. This is normal; that's >> what I expect from a non null attribute, but I'd like to get a bit of >> control over, so that I can take my own action in such a case. >> >> Reading the manual, I've found (not too hard), that each class can >> have >> validateKey methods, used to validate attributes BEFORE they are set, >> seems what I was looking for... >> >> I have written a validateLabel method for the Foo class. I've checked >> that this method is called anytime before the setLabel method is >> called, and it's fine... EXCEPT when the value to which Label is set >> is >> null... To be exact, if the textfield is directly connected to the >> aFoo.label path, then the 'aFoo.setLabel(value)' is not called, still >> I >> get an exception thrown that the Label can not be null. >> >> To be even more sure, I added a couple of println statement in my >> validateLabel() and setLabel() method: >> - - -When the value is not null, I see that validateLabel() is first >> called, then it exits, then setLabel() is called and exits. >> - - -When the value is null, I see that the exception is thrown BEFORE >> any of this line are printed : these methods (validateLabel, setLabel) >> does not even have the chance to modify the value for it to be non >> null... >> >> From the stack trace I can see that the exception is raised in: >> at >> com.webobjects.eoaccess.EOAttribute.validateValue(EOAttribute.java:231 >> 5) >> at >> com.webobjects.eoaccess.EOEntityClassDescription.validateValueForKey(E >> OE >> ntityClassDescription.java:439) >> at >> com.webobjects.eocontrol.EOCustomObject.validateValueForKey(EOCustomOb >> je >> ct.java:1332) >> >> EOCustomObject from which my object inherit (through EOGenericRecord). >> >> Is there anyway to avoid having to override >> EOCustomObject.validateValueForKey, still have the validateLabel >> called >> before the generic validateValue method (the one checking it's not >> null)? I have the feeling it can be quiet tricky to override this >> method and avoiding getting things wrong... I've not found any >> doc/example about overriding this method. >> >> >> I've searched the doc to get a better grasp of the validation/setting >> cycle after a user submit a form, but I've found no precise >> description. >> >> >> Thanks for any tips, info, or pointer to resources!! >> (it's also likely that I've been too far in the analysis... and that I >> just missed the simple+proper solution!!) >> >> Serge. >> >> - ---------------------------------------------------- >> Serge Cohen >> >> GPG Key ID: 9CBB58FB >> - ---------------------------------------------------- >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v1.2.1 (Darwin) >> >> iD8DBQE/qpBb5EPeG5y7WPsRAk9GAJ9DDJUyMpjkSVCUM2Ye9u362IdlpgCfbXBU >> huuGkksVUd+U3o6ziNE6pgU= >> =FCaZ >> -----END PGP SIGNATURE----- >> >> >> >> _______________________________________________ >> EOF mailing list >> [email protected] >> http://www.omnigroup.com/mailman/listinfo/eof >> >> > _______________________________________________ > EOF mailing list > [email protected] > http://www.omnigroup.com/mailman/listinfo/eof > > - ---------------------------------------------------- Serge Cohen GPG Key ID: 9CBB58FB - ---------------------------------------------------- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (Darwin) iD8DBQE/q3O85EPeG5y7WPsRAnmIAKDCZ4qq3ge3AbXAaMV+TLKQnGoTCQCgu56C 20a7cQsQmGUKsSsd/0Km20o= =Apom -----END PGP SIGNATURE-----