2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #import <Foundation/NSNumberFormatter.h>
25 #import <Foundation/NSDateFormatter.h>
30 PhoneNumber: WOTextField {
32 formatter = session.phoneNumberFormatter;
33 formatErrorString = errorString;
34 formatFailedAction = "handleFormattingError";
37 The textfield calls the -validationFailed:.. method of the associated
38 component if a formatter could not format a value.
41 @interface WOTextField : WOInput
45 // inherited: disabled
47 WOAssociation *numberformat; // string
48 WOAssociation *dateformat; // string
49 WOAssociation *formatter; // WO4
55 @end /* WOTextField */
57 @interface NSObject(UsedKeyPath)
58 - (NSString *)keyPath;
61 @implementation WOTextField
63 - (id)initWithName:(NSString *)_name
64 associations:(NSDictionary *)_config
65 template:(WOElement *)_root
67 if ((self = [super initWithName:_name associations:_config template:_root])) {
68 self->size = OWGetProperty(_config, @"size");
69 self->formatter = OWGetProperty(_config, @"formatter");
70 self->numberformat = OWGetProperty(_config, @"numberformat");
71 self->dateformat = OWGetProperty(_config, @"dateformat");
73 if (self->formatter == nil) {
74 if ([_config objectForKey:@"formatterClass"]) {
77 className = OWGetProperty(_config, @"formatterClass");
78 className = AUTORELEASE(className);
80 className = [className valueInComponent:nil];
81 className = NSClassFromString(className);
82 className = [[className alloc] init];
84 self->formatter = [WOAssociation associationWithValue:className];
85 self->formatter = RETAIN(self->formatter);
93 if (self->formatter) num++;
94 if (self->numberformat) num++;
95 if (self->dateformat) num++;
97 NSLog(@"WARNING: more than one formats specified in element %@", self);
104 [self->numberformat release];
105 [self->dateformat release];
106 [self->formatter release];
107 [self->size release];
113 static inline NSFormatter *_getFormatter(WOTextField *self, WOContext *_ctx) {
114 NSFormatter *fmt = nil;
116 if (self->numberformat) {
117 fmt = [[[NSNumberFormatter alloc] init] autorelease];
118 [(NSNumberFormatter *)fmt setFormat:
119 [self->numberformat valueInComponent:[_ctx component]]];
121 else if (self->dateformat) {
122 fmt = [[NSDateFormatter alloc]
124 [self->dateformat valueInComponent:
126 allowNaturalLanguage:NO];
127 fmt = [fmt autorelease];
129 else if (self->formatter) {
130 fmt = [self->formatter valueInComponent:[_ctx component]];
136 // ******************** responder ********************
138 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
139 NSFormatter *fmt = nil;
141 fmt = _getFormatter(self, _ctx);
144 NSString *errorText = nil;
147 //fmt = [self->formatter valueInComponent:[_ctx component]];
149 if ([fmt getObjectValue:&object forString:[_value stringValue]
150 errorDescription:&errorText]) {
155 NSException *formatException = nil;
156 NSString *keyPath = nil;
158 if ([self->value respondsToSelector:@selector(keyPath)])
159 keyPath = [(id)self->value keyPath];
161 formatException = [NSException exceptionWithName:@"WOValidationException"
165 [[_ctx component] validationFailedWithException:formatException
172 return [super parseFormValue:_value inContext:_ctx];
175 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
176 NSFormatter *fmt = nil;
180 obj = [self->value valueInComponent:[_ctx component]];
181 s = [self->size unsignedIntValueInComponent:[_ctx component]];
183 fmt = _getFormatter(self, _ctx);
185 NSString *formattedObj = nil;
187 formattedObj = [fmt editingStringForObjectValue:obj];
190 if (formattedObj == nil) {
191 NSLog(@"WARNING: formatter %@ returned nil string for object %@",
198 WOResponse_AddCString(_response, "<input type=\"text\" name=\"");
199 [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
200 WOResponse_AddCString(_response, "\" value=\"");
201 [_response appendContentHTMLAttributeValue:[obj stringValue]];
202 WOResponse_AddChar(_response, '"');
204 WOResponse_AddCString(_response, " size=\"");
205 WOResponse_AddUInt(_response, s);
206 WOResponse_AddChar(_response, '"');
208 [self appendExtraAttributesToResponse:_response inContext:_ctx];
209 if (self->otherTagString) {
210 WOResponse_AddChar(_response, ' ');
211 WOResponse_AddString(_response,
212 [self->otherTagString stringValueInComponent:
215 WOResponse_AddCString(_response, " />");
220 - (NSString *)associationDescription {
221 NSMutableString *str;
223 str = [NSMutableString stringWithCapacity:128];
224 [str appendString:[super associationDescription]];
226 if (self->size) [str appendFormat:@" size=%@", self->size];
227 if (self->formatter) [str appendFormat:@" formatter=%@", self->formatter];
228 if (self->dateformat) [str appendFormat:@" dateformat=%@", self->dateformat];
229 if (self->numberformat)
230 [str appendFormat:@" numberformat=%@", self->numberformat];
235 @end /* WOTextField */