0

I have a main document with several attached documents. The main document has a start and end date. The attached document has a type, status and date. Status 0 - the document is not needed, 1 - waiting for the document, 2 - the document is attached and has a date. I want to check two options:

  1. If the attached document has a status = 2, it should have a date
  2. The date of the attached document should be in the range between the start and end dates in the main document.

I tried to use Collections validation from https://vuelidate.js.org/#sub-validation-groups

$each: {name: {required}}

My code:

add_docs: {$each: {date: {required}}}  

Works, but requires date for all elements in add_docs

How I can use IF (status===2) then Required?

How can I get the Status value for the current item in $each?

All code

import { required } from 'vuelidate/lib/validators'

export default {
  data() {
   return {
    doc: {
        number: 'A123',
        date_start: '2024-01-01',
        date_finish: '2024-02-01',
        add_docs: [
            {
            type: 'One',
            date: '',
            status: 0 // The document is not needed
            },
            {
            type: 'Two',
            date: '',
            status: 1 // Waiting for the document
            },
            type: 'Three',
            date: '2024-02-10', // This date out of range between date_start and date_finish
            status: 2 // The document is attached and has a date
            },
        ]
    }
  },
  validations: {
    doc {
        number: {required},
        date_start: {required},
        date_finish: {
            check_date: function (value) { // Check date_start > date_finish
                if (!value) {
                    return true;
                }
                return (new Date(value) >= new Date(this.doc.date_start));
            }
        },
        add_docs: {
            $each: {
                date: { required }  // Works, but requires date for all elements in add_docs
            }
        }
    }
  }
}

1
  • There's requiredIf, it can possibly solve this. Or you can use custom validator instead of builtin ones if you need some custom behaviour from it Commented Dec 4 at 15:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.