<commit_before>//
//  FreeFormTextFieldCell.swift
//  FreeForm
//
//  Created by <NAME> on 11/27/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import Validator

public class FreeFormTextFieldRow: FreeFormRow {
    
    public var validationRuleSet: ValidationRuleSet<String>? = ValidationRuleSet<String>() {
        didSet {
            self.validationErrors = [String]()
        }
    }
    public var validationErrors: [String]?
    
    override public init(tag: String, title: String, value: AnyObject?) {
        super.init(tag: tag, title: title, value: value)
        self.cellType = String(describing: FreeFormTextFieldCell.self)
    }
    
    public func updateValidationState(text: String?, result: ValidationResult) -> Bool {
        if self.isOptional == true {
            if let content = text {
                if content.characters.count > 0 {
                    switch result {
                    case .invalid(let failures):
                        self.validated = false
                        self.validationErrors = failures.map { $0.localizedDescription }
                        return false
                    case .valid:
                        self.validated = true
                        self.validationErrors?.removeAll()
                        return true
                    }
                }else {
                    return true
                }
            }else {
                return true
            }
        }else {
            switch result {
            case .invalid(let failures):
                self.validated = false
                self.validationErrors = failures.map { $0.localizedDescription }
                return false
            case .valid:
                self.validated = true
                self.validationErrors?.removeAll()
                return true
            }
        }
    }
    
    public func clearRules() {
        self.validationRuleSet = ValidationRuleSet<String>()
    }
}

public class FreeFormTextFieldCell: FreeFormCell {

    @IBOutlet weak public var textField: UITextField!
    @IBOutlet weak public var titleLabel: UILabel!
    @IBOutlet weak public var errorLabel: UILabel!
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        self.textField.delegate = self
        self.textField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    }
    
    override public func update() {
        super.update()
        self.clearError()
        guard let textfieldRow = self.row as? FreeFormTextFieldRow else { return }
        
        titleLabel.text = textfieldRow.title
        self.textField.validationRules = textfieldRow.validationRuleSet
        self.textField.validateOnInputChange(enabled: true)
        self.textField.validationHandler = { result in
            if textfieldRow.updateValidationState(text: self.textField.text, result: result) {
                self.clearError()
            }else {
                if let message = textfieldRow.validationErrors?.joined(separator: "/") {
                    self.showError(message: message)
                    self.clearError()
                }
            }
        }
        
        guard let value = textfieldRow.value as? String else {
            textField.text = ""
            return
        }
        
        textField.text = value
    }
    
    public func validateTextField() {
        guard let textfieldRow = self.row as? FreeFormTextFieldRow else { return }
        guard let rules = textfieldRow.validationRuleSet else { return }
        let result = self.textField.validate(rules: rules)
        if textfieldRow.updateValidationState(text: self.textField.text, result: result) {
            self.clearError()
        }else {
            if let message = textfieldRow.validationErrors?.joined(separator: "/") {
                self.showError(message: message)
                self.clearError()
            }
        }
    }
    
    public func showError(message: String) {
        self.errorLabel.text = message
    }
    <commit_msg>Fix error append<commit_after>//
//  FreeFormTextFieldCell.swift
//  FreeForm
//
//  Created by <NAME> on 11/27/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import Validator

public class FreeFormTextFieldRow: FreeFormRow {
    
    public var validationRuleSet: ValidationRuleSet<String>? = ValidationRuleSet<String>() {
        didSet {
            self.validationErrors = [String]()
        }
    }
    public var validationErrors: [String]?
    
    override public init(tag: String, title: String, value: AnyObject?) {
        super.init(tag: tag, title: title, value: value)
        self.cellType = String(describing: FreeFormTextFieldCell.self)
    }
    
    public func updateValidationState(text: String?, result: ValidationResult) -> Bool {
        if self.isOptional == true {
            if let content = text {
                if content.characters.count > 0 {
                    switch result {
                    case .invalid(let failures):
                        self.validated = false
                        self.validationErrors = failures.map { $0.localizedDescription }
                        return false
                    case .valid:
                        self.validated = true
                        self.validationErrors?.removeAll()
                        return true
                    }
                }else {
                    return true
                }
            }else {
                return true
            }
        }else {
            switch result {
            case .invalid(let failures):
                self.validated = false
                self.validationErrors = failures.map { $0.localizedDescription }
                return false
            case .valid:
                self.validated = true
                self.validationErrors?.removeAll()
                return true
            }
        }
    }
    
    public func clearRules() {
        self.validationRuleSet = ValidationRuleSet<String>()
    }
}

public class FreeFormTextFieldCell: FreeFormCell {

    @IBOutlet weak public var textField: UITextField!
    @IBOutlet weak public var titleLabel: UILabel!
    @IBOutlet weak public var errorLabel: UILabel!
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        self.textField.delegate = self
        self.textField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    }
    
    override public func update() {
        super.update()
        self.clearError()
        guard let textfieldRow = self.row as? FreeFormTextFieldRow else { return }
        
        titleLabel.text = textfieldRow.title
        self.textField.validationRules = textfieldRow.validationRuleSet
        self.textField.validateOnInputChange(enabled: true)
        self.textField.validationHandler = { result in
            
            textfieldRow.validationErrors?.removeAll()
            
            if textfieldRow.updateValidationState(text: self.textField.text, result: result) {
                self.clearError()
            }else {
                if let message = textfieldRow.validationErrors?.joined(separator: "/") {
                    self.showError(message: message)
                }
            }
            
        }
        
        guard let value = textfieldRow.value as? String else {
            textField.text = ""
            return
        }
        
        textField.text = value
    }
    
    public func validateTextField() {
        guard let textfieldRow = self.row as? FreeFormTextFieldRow else { return }
        guard let rules = textfieldRow.validationRuleSet else { return }
        let result = self.textField.validate(rules: rules)
        if textfieldRow.updateValidationState(text: self.textField.text, result: result) {
            self.clearError()
        }else {
            if let message = textfieldRow.validationErrors?.joined(separator: "/") {
                self.showError(message: message)
            }
        }
    }
    
    public func showError(message: String) {
        self.errorLabel.text = message
    }
    
{'fraction_non_alphanumeric': 0.08221334301973604, 'fraction_numerical': 0.003148080881462647, 'mean_word_length': 1.6722743448722097, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '242679', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2028, 'n_words': 485}