+ Start a Discussion
Amey K 10Amey K 10 

Validate design attributes in LWC

I have 3 fields in design attributes for LWC. These fields are objName, field1 and field2. I'm  passing these fields to the apex method using @wire on a function and with $ on the parametes. When these fields have value, the method returns correct result set. However if any of the parameter is empty, the component is unable to invoke the apex. Is there any way to validate the input parameters before being passed to the Apex method.

pseudo code as below

 @api strObject;
 @api fieldColumn1;
 @api fieldColumn2;

 @wire(retriveRecords, { strObject: '$strObject',fieldColumn1: '$fieldColumn1', fieldColumn2: '$fieldColumn2'})
    contacts(result) {
        if (result.data) {
            this.data = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error;
            this.data = undefined;
        }
    }
Soyab HussainSoyab Hussain
Hi Amey,

First Solution:
You can call handleKeyChange on change of your all input change and validate input values.
//call this method on your input onchange
handleKeyChange(event) {
    if(/* validate your value here */) {
        retriveRecords({ strObject: '$strObject',fieldColumn1: '$fieldColumn1', fieldColumn2: '$fieldColumn2'})
        .then(result => {
            this.data = result.data;
            this.error = undefined;
        })
        .catch(error => {
            this.error = result.error;
            this.data = undefined;
        });
    }
}

Second Solution:
You can validate your parameters on you apex controller if any parameter isBlank you can return null data. 

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

If you found it useful please appreciate my efforts and mark it as the best answer.

Regards,
Soyab