[Vuejs]-How to replace multiple if condition block to validate multiple input fields in a form – vue 3

0👍

I haven’t tested but this should work

const inputValidation = () => {
  for (const tempField of tempFieldArray) {
    if (form1.value[tempField.radio] === tempField.value) {
      validateInputField(tempField.field, formInputs.value[tempField.input]);
    }
  }
}

0👍

asume u can get that trigger field
and change tempArray to tempObject

const tempFieldProp = 
    { 
      field1 : {radio: 'radio3', value: 'Pass', input: 'txtArea1' }, 
      field2 : {radio: 'radio4', value: 'Pass', input: 'txtArea2' }, 
      field3 : {radio: 'radio4', value: 'fail', input: 'txtArea6' }, 
      field4 : {radio: 'radio5', value: 'fail', input: 'txtArea3' }, 
      field5 : {radio: 'radio6', value: 'Pass', input: 'txtArea4' }, 
      field6 : {radio: 'radio6', value: 'fail', input: 'txtArea5' }, 
      field7 : {radio: 'radio7', value: 'fail', input: 'txtArea7' }, 
      field8 : {radio: 'radio8', value: 'fail', input: 'txtArea8' }, 
      field9 : {radio: 'radio9', value: 'fail', input: 'txtArea9' }
    }
  

    const inputValidation = (field) => {
        let props = tempFieldProp[field]
        return formInputs.value[props.radio] == props.value ? ()=> validateInputField(field, formInputs.value[props.input]) : ()=>{}
    }()

return function and use it

or you can’t get trigger field

const tempFieldArray = [
  { radio: 'radio3', value: 'Pass', input: 'txtArea1', field: 'field1' },
  { radio: 'radio4', value: 'Pass', input: 'txtArea2', field: 'field2' },
  { radio: 'radio4', value: 'fail', input: 'txtArea6', field: 'field3' },
  { radio: 'radio5', value: 'fail', input: 'txtArea3', field: 'field4' },
  { radio: 'radio6', value: 'Pass', input: 'txtArea4', field: 'field5' },
  { radio: 'radio6', value: 'fail', input: 'txtArea5', field: 'field6' },
  { radio: 'radio7', value: 'fail', input: 'txtArea7', field: 'field7' },
  { radio: 'radio8', value: 'fail', input: 'txtArea8', field: 'field8' },
  { radio: 'radio9', value: 'fail', input: 'txtArea9', field: 'field9' },
];

tempFieldArray.map((field) => 
 formInputs.value[field.radio] === field.value ? 
       validateInputField(field.field , formInputs.value[field.input]) : else-function
)

Leave a comment