Eureka: Proposal: Validations

Created on 12 Jul 2016  路  8Comments  路  Source: xmartlabs/Eureka

We plan to add native validations to Eureka.
We also want to separate the logical validation part and the UI part of showing errors the most we can.

Use cases

Logical part

  • Validate rows: We want to be able to validate the values of each of the rows of our forms.
  • Validate forms: We want to validate the whole form to see if the user can submit it or not.
  • Validate list sections
  • Reuse validations: We want to be able to reuse part or the whole of a validation in other validations
  • When validations are shown: To be able to choose when to show the validation's result
  • Execute handler when validation fails/succeeds for row and/or form
  • Strongly typed values: Execute validations over strong-typed values
  • Required rows: Specify if a row is required or not
  • Display errors or not: Specify if the result of the validation should be shown or not. We might want to validate rows silently.

    UI part

  • How validations are shown: To be able to define how a validation result will be shown for each row or type of cell.

  • Form validations: FormViewController should be able to show if a form is valid or invalid knowing which rows failed validation.

    Proposed features

  • Define rules of validation that can be reused. This rules should have a name and an associated execution block taking the row to be validated and returning a boolean result.

  • Each row should have an array of rules that are executed in order. Validation will execute all rules in order and return an array of errors (or should we just execute until the first error appears?).
  • The form will be valid if all required rows of that form are valid
  • Each row will have a required variable that determines if an error in this row invalidates the form.
  • Each row will have a showValidation variable that determines if the error should be shown or not
  • Each cell is responsible for showing row validation errors. Each cell should implement a method that shows valid/invalid status. This should be different in each app so that the best way might be to add a callback onValidation similar to 'cellUpdate'.
  • FormViewController is responsible for showing form validation errors. Add delegate methods that get called when valid status changes

Note: List sections will have to support the same behaviour as the rows

Validations 2.0.0-beta.1

Most helpful comment

I am using the new validations feature in my project and it's great! Would love to see support for validating list sections (e.g. at least one row must be selected). Maybe the validation related parts of BaseRow could be split out into a Validatable protocol to which Sections could conform?

All 8 comments

Great, I have been waiting for it for a long time.
I was trying to integrate Eureka with other validation libraries, but I really think that validation should be native.
I love your features list, just some point that I found useful from my experience:

  • required attribute for all kind of rows (not just textFields)
  • Possibility to have custom validation rules.
  • Exclude hidden rows from validation.

Do you have any release date idea for this feature?

No, there is no release date. We want to think a bit more about it and then find some time to implement it.

This is the missing piece of the puzzle for Eureka. Feature list looks good.

I have made a workaround till Eureka comes with proper validation as XLForm.

func validateForm(formValues:[String:Any?], sender:UIViewController) -> Bool {

    for (key,value) in formValues{

        if key.containsString("*") && value == nil {

            formValidationError(key, sender: sender)
            return false
        }
    }

    return true
}

You just need to add * with title while initialising your form like

<<< PickerInlineRow<String>("Method") { row in
                row.title = "Method*"
                row.options = followUpMethod   
            }

And you can validate the whole form before submitting like

if validateForm(form.values(), sender: self) {

}

Cheers for this workaround and open for any other suggestions.

Thanks

This feature has been implemented in Eureka 2.0 beta 1. Take a look at the repository readme for more information.

Any feedback is welcomed.

Thanks for this. When I eventually convert the project to Swift 3 I will definitely make use of this. Something that's not really made clear in the README is whether or not a row that doesn't pass validation has some sort of a "error" displayed on the default rows (TextRow, DateRow etc.). Is this the case?

In order to change how a row looks like depending on validations result you should use row.isValid from cellUpdate callback.

By default Eureka validations doesn't alter the UI. Take a look at the example project to know how to do so.

I am using the new validations feature in my project and it's great! Would love to see support for validating list sections (e.g. at least one row must be selected). Maybe the validation related parts of BaseRow could be split out into a Validatable protocol to which Sections could conform?

Was this page helpful?
0 / 5 - 0 ratings