Eureka: Adding rules to MultivaluedSection

Created on 19 Dec 2017  路  3Comments  路  Source: xmartlabs/Eureka

Is it possible to add rules to a whole MultivaluedSections?

My intention is to count the number of DateRows added in a MultivaluedSection and check if there is at least one DateRow in that section to enable a save button.

One way could be to overwrite methods like rowsHaveBeenAdded and rowsHaveBeenRemoved.

However, the easiest way would be to add RuleRequired to the whole section if possible.

MultivaluedSection question

Most helpful comment

Hi @calli23, right now there is no way to add rules to an entire section even if it is a MultivaluedSection.

You can try next approach:

  1. Add a disable rule to your "save" button to check if there is any value already added to the multivalued section.

    +++ Section("save")
     <<< ButtonRow("save") { [weak self] in
         $0.title = "Save"
         $0.disabled = Condition.function([]) { form in
             let section = form.sectionBy(tag: "textfields") as! MultivaluedSection
             return section.values().isEmpty
         }
     }
    
  2. Change how rows are added to the section, implement onChange callback to re-evaluate the save button's disable condition

    form +++
    MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
                      header: "Multivalued TextField",
                      footer: ".Insert multivaluedOption adds the 'Add New Tag' button row as last cell.") {
     // Section settings
     $0.multivaluedRowToInsertAt = { index in
         return NameRow() {
             $0.placeholder = "Tag Name"
         }.onChange { [weak self] _ in
             // Re-evaluate save button's disabled condition
             self?.form.rowBy(tag: "save")?.evaluateDisabled()
         }
      }
    }
    
  3. Override rowsHaveBeenAdded and rowsHaveBeenRemoved to re-evaluate save button's disable condition.

    override func rowsHaveBeenAdded(_ rows: [BaseRow], at indexes: [IndexPath]) {
       super.rowsHaveBeenAdded(rows, at: indexes)
       // For the moment, we need to use `async` due to this function is called before the multivalued
       // section's `values` property is updated.
       DispatchQueue.main.async { [weak self] in
           self?.form.rowBy(tag: "save")?.evaluateDisabled()
       }
    }
    
    override func rowsHaveBeenRemoved(_ rows: [BaseRow], at indexes: [IndexPath]) {
       super.rowsHaveBeenRemoved(rows, at: indexes)
       // For the moment, we need to use `async` due to this function is called before the multivalued
       // section's `values` property is updated.
       DispatchQueue.main.async { [weak self] in
           self?.form.rowBy(tag: "save")?.evaluateDisabled()
       }
    }
    

Note: I've used the example provided as base code for this, check on the ViewController.swift class if you need more context.

All 3 comments

Hi @calli23, right now there is no way to add rules to an entire section even if it is a MultivaluedSection.

You can try next approach:

  1. Add a disable rule to your "save" button to check if there is any value already added to the multivalued section.

    +++ Section("save")
     <<< ButtonRow("save") { [weak self] in
         $0.title = "Save"
         $0.disabled = Condition.function([]) { form in
             let section = form.sectionBy(tag: "textfields") as! MultivaluedSection
             return section.values().isEmpty
         }
     }
    
  2. Change how rows are added to the section, implement onChange callback to re-evaluate the save button's disable condition

    form +++
    MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
                      header: "Multivalued TextField",
                      footer: ".Insert multivaluedOption adds the 'Add New Tag' button row as last cell.") {
     // Section settings
     $0.multivaluedRowToInsertAt = { index in
         return NameRow() {
             $0.placeholder = "Tag Name"
         }.onChange { [weak self] _ in
             // Re-evaluate save button's disabled condition
             self?.form.rowBy(tag: "save")?.evaluateDisabled()
         }
      }
    }
    
  3. Override rowsHaveBeenAdded and rowsHaveBeenRemoved to re-evaluate save button's disable condition.

    override func rowsHaveBeenAdded(_ rows: [BaseRow], at indexes: [IndexPath]) {
       super.rowsHaveBeenAdded(rows, at: indexes)
       // For the moment, we need to use `async` due to this function is called before the multivalued
       // section's `values` property is updated.
       DispatchQueue.main.async { [weak self] in
           self?.form.rowBy(tag: "save")?.evaluateDisabled()
       }
    }
    
    override func rowsHaveBeenRemoved(_ rows: [BaseRow], at indexes: [IndexPath]) {
       super.rowsHaveBeenRemoved(rows, at: indexes)
       // For the moment, we need to use `async` due to this function is called before the multivalued
       // section's `values` property is updated.
       DispatchQueue.main.async { [weak self] in
           self?.form.rowBy(tag: "save")?.evaluateDisabled()
       }
    }
    

Note: I've used the example provided as base code for this, check on the ViewController.swift class if you need more context.

Hi @calli23, as it is pointed out in the Contributing guidelines we don't use Github issues for general library support. In such cases, the best option is posting questions on StackOverflow with tag eureka-forms.

This will help us to better manage real bugs and feature requests and also we think you'll get better and faster support for this kind of questions from community through StackOverflow.

I'm closing this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fpena picture fpena  路  3Comments

thlbaut picture thlbaut  路  3Comments

abbasmousavi picture abbasmousavi  路  3Comments

Tomas1405 picture Tomas1405  路  3Comments

sedwo picture sedwo  路  3Comments