输入的通用验证 [英] Generic validation on input

查看:111
本文介绍了输入的通用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个通用的方法来验证所有的UIControl如NSTextField,NSTextView等。如果任何必填字段为空,合并的一个警报应该显示与第一个控制作为集中/第一响应。

I want to write a generic method to validate all the UIControls like NSTextField, NSTextView, etc. If any mandatory field is empty, a consolidated one alert should be shown with the first control as focused/first responder.

我实现了这样:

-(NSInteger)lengthAfterTrimmingSpaces:(NSString *)string{
    return [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length];
}


-(NSDictionary *)createWarningMessageForMandatoryFields:(NSArray *)uiObjects{

    NSMutableString *warningString=[NSMutableString stringWithString:@"Enter "];
    id firstInvalidatedControl=nil;

    for (NSDictionary *object in uiObjects) {
        NSString *key=[object allKeys][0];
        id control=object[key];
        if ([control isKindOfClass:[NSTextField class]]) {
            if ([self lengthAfterTrimmingSpaces:[control stringValue]]==0){
                [warningString appendString:key];
                [warningString appendString:@", "];

                if (firstInvalidatedControl==nil) {
                    firstInvalidatedControl=control;
                }
            }
        }
        else if ([control isKindOfClass:[NSTextView class]]) {
            if ([self lengthAfterTrimmingSpaces:[control string]]==0){
                [warningString appendString:key];
                [warningString appendString:@", "];
                if (firstInvalidatedControl==nil) {
                    firstInvalidatedControl=control;
                }
            }
        }
    }

    if (firstInvalidatedControl==nil) {
        return @{@"warningString":@"Success"};
    }
    else{

        warningString =[[warningString substringToIndex:[warningString length] - 2]mutableCopy];
        return @{@"warningString":warningString, @"control":firstInvalidatedControl};
    }
}


-(BOOL)validateMandatoryFields{
    NSMutableArray *uiObjects=[NSMutableArray array];

    [uiObjects addObject:@{@"Segment Name":self.segmentName}];
    [uiObjects addObject:@{@"Code":self.code}];
    [uiObjects addObject:@{@"Desciption":self.description}];

    NSDictionary *warningAndControl=[self createWarningMessageForMandatoryFields:uiObjects];
    if ([warningAndControl[@"warningString"] isEqualToString:@"Success"]) {
        return YES;
    }
    else{
        [[self window] makeFirstResponder:warningAndControl[@"control"]];
        NSRunAlertPanel(@"Warning", warningAndControl[@"warningString"], @"OK", nil, nil);
        return NO;
    }
}

- (IBAction)save:(id)sender {
    NSLog(@"%d",[self validateMandatoryFields]);
}

现在我想让它更通用,做类自检,然后检索值(stringValue / string等)。

Now I want to make it even more generic, as for each kind I am doing class introspection and then then retrieving the value ( stringValue/string etc).

还有任何其他的建议。

推荐答案

您将基于逻辑(决定是否有错误)对将呈现给用户的字符串的内容。你不应该。相反,将逻辑放在键( control )上,因为它们是不可变的。与此同时,您应该使用 NSLocalizedString 来证明代码可以重用。

You're basing logic (deciding if there was an error) on the content of strings that will be presented to the user. You shouldn't. Instead, base the logic on the keys (control) as they are immutable. Together with this you should use NSLocalizedString to future proof the code for reuse.

对于内省,考虑将这些方法放入实用程序类中,并将一些类别添加到该类中。类别将为每个 NSControl 子类添加一个方法,目的是允许您始终获取它们的 stringValue 调用具有相同名称的方法。对于 NSTextField ,您不需要一个类别。 NSTextView 类别将是:

For the introspection, I'd consider putting these methods into a utility class and adding a number of categories into that class also. The categories would add a single method to each NSControl subclass with the aim being to allow you to get their stringValue by always calling a method with the same name. For NSTextField you wouldn't need a category. For NSTextView the category would be:

@implementation NSTextView (MyStringValue)

- (NSString *)stringValue
{
    return [self string];
}

@end

您的代码仍应验证该类响应 stringValue 选择器,如果不能,它不能被验证。

Your code should still verify that the class responds to the stringValue selector, if it doesn't it can't be validated.

这篇关于输入的通用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆