161 lines
6.8 KiB
Objective-C
161 lines
6.8 KiB
Objective-C
//
|
|
// ValidationExamplesFormViewController.m
|
|
// XLForm ( https://github.com/xmartlabs/XLForm )
|
|
//
|
|
// Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
|
|
//
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
#import "XLForm.h"
|
|
#import "ValidationExamplesFormViewController.h"
|
|
|
|
@implementation ValidationExamplesFormViewController
|
|
|
|
NSString * const kValidationName = @"kName";
|
|
NSString * const kValidationEmail = @"kEmail";
|
|
NSString * const kValidationPassword = @"kPassword";
|
|
NSString * const kValidationInteger = @"kInteger";
|
|
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder
|
|
{
|
|
self = [super initWithCoder:coder];
|
|
if (self) {
|
|
[self initializeForm];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
|
|
-(void)initializeForm
|
|
{
|
|
XLFormDescriptor * formDescriptor = [XLFormDescriptor formDescriptorWithTitle:@"Text Fields"];
|
|
XLFormSectionDescriptor * section;
|
|
XLFormRowDescriptor * row;
|
|
|
|
// Name Section
|
|
section = [XLFormSectionDescriptor formSectionWithTitle:@"Validation Required"];
|
|
[formDescriptor addFormSection:section];
|
|
|
|
// Name
|
|
row = [XLFormRowDescriptor formRowDescriptorWithTag:kValidationName rowType:XLFormRowDescriptorTypeText title:@"Name"];
|
|
[row.cellConfigAtConfigure setObject:@"Required..." forKey:@"textField.placeholder"];
|
|
[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
|
|
row.required = YES;
|
|
row.value = @"Martin";
|
|
[section addFormRow:row];
|
|
|
|
// Email Section
|
|
section = [XLFormSectionDescriptor formSectionWithTitle:@"Validation Email"];
|
|
[formDescriptor addFormSection:section];
|
|
|
|
// Email
|
|
row = [XLFormRowDescriptor formRowDescriptorWithTag:kValidationEmail rowType:XLFormRowDescriptorTypeText title:@"Email"];
|
|
[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
|
|
row.required = NO;
|
|
row.value = @"not valid email";
|
|
[row addValidator:[XLFormValidator emailValidator]];
|
|
[section addFormRow:row];
|
|
|
|
// password Section
|
|
section = [XLFormSectionDescriptor formSectionWithTitle:@"Validation Password"];
|
|
section.footerTitle = @"between 6 and 32 charachers, 1 alphanumeric and 1 numeric";
|
|
[formDescriptor addFormSection:section];
|
|
|
|
// Password
|
|
row = [XLFormRowDescriptor formRowDescriptorWithTag:kValidationPassword rowType:XLFormRowDescriptorTypePassword title:@"Password"];
|
|
[row.cellConfigAtConfigure setObject:@"Required..." forKey:@"textField.placeholder"];
|
|
[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
|
|
row.required = YES;
|
|
[row addValidator:[XLFormRegexValidator formRegexValidatorWithMsg:@"At least 6, max 32 characters" regex:@"^(?=.*\\d)(?=.*[A-Za-z]).{6,32}$"]];
|
|
[section addFormRow:row];
|
|
|
|
// number Section
|
|
section = [XLFormSectionDescriptor formSectionWithTitle:@"Validation Numbers"];
|
|
section.footerTitle = @"greater than 50 and less than 100";
|
|
[formDescriptor addFormSection:section];
|
|
|
|
// Integer
|
|
row = [XLFormRowDescriptor formRowDescriptorWithTag:kValidationInteger rowType:XLFormRowDescriptorTypeInteger title:@"Integer"];
|
|
[row.cellConfigAtConfigure setObject:@"Required..." forKey:@"textField.placeholder"];
|
|
[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
|
|
row.required = YES;
|
|
[row addValidator:[XLFormRegexValidator formRegexValidatorWithMsg:@"greater than 50 and less than 100" regex:@"^([5-9][0-9]|100)$"]];
|
|
[section addFormRow:row];
|
|
|
|
self.form = formDescriptor;
|
|
}
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
// Do any additional setup after loading the view.
|
|
[self.navigationItem.rightBarButtonItem setTarget:self];
|
|
[self.navigationItem.rightBarButtonItem setAction:@selector(validateForm:)];
|
|
}
|
|
|
|
#pragma mark - actions
|
|
|
|
-(void)validateForm:(UIBarButtonItem *)buttonItem
|
|
{
|
|
NSArray * array = [self formValidationErrors];
|
|
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
XLFormValidationStatus * validationStatus = [[obj userInfo] objectForKey:XLValidationStatusErrorKey];
|
|
if ([validationStatus.rowDescriptor.tag isEqualToString:kValidationName]){
|
|
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[self.form indexPathOfFormRow:validationStatus.rowDescriptor]];
|
|
cell.backgroundColor = [UIColor orangeColor];
|
|
[UIView animateWithDuration:0.3 animations:^{
|
|
cell.backgroundColor = [UIColor whiteColor];
|
|
}];
|
|
}
|
|
else if ([validationStatus.rowDescriptor.tag isEqualToString:kValidationEmail]){
|
|
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[self.form indexPathOfFormRow:validationStatus.rowDescriptor]];
|
|
[self animateCell:cell];
|
|
}
|
|
else if ([validationStatus.rowDescriptor.tag isEqualToString:kValidationPassword]){
|
|
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[self.form indexPathOfFormRow:validationStatus.rowDescriptor]];
|
|
[self animateCell:cell];
|
|
}
|
|
else if ([validationStatus.rowDescriptor.tag isEqualToString:kValidationInteger]){
|
|
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[self.form indexPathOfFormRow:validationStatus.rowDescriptor]];
|
|
[self animateCell:cell];
|
|
}
|
|
}];
|
|
}
|
|
|
|
|
|
#pragma mark - Helper
|
|
|
|
-(void)animateCell:(UITableViewCell *)cell
|
|
{
|
|
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
|
|
animation.keyPath = @"position.x";
|
|
animation.values = @[ @0, @20, @-20, @10, @0];
|
|
animation.keyTimes = @[@0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1];
|
|
animation.duration = 0.3;
|
|
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
|
|
animation.additive = YES;
|
|
|
|
[cell.layer addAnimation:animation forKey:@"shake"];
|
|
}
|
|
|
|
|
|
@end
|