Hello,
Im using the validation form fields but for some reason my state is not being updated after I enter valid data, on form submission the inputs are all null even thought I had entered valid data. some help would be greatly appreciated. Part of the code is below
const data = {
manufacturer_id: parseInt(manufacturerId)||null,
comp_model: compModel||null,
comp_type_id: parseInt(compTypeId)||null,
comp_nickname: compNickname||null,
comp_serial_no: compSerialNo||null,
comp_pressure: parseFloat(compPressure)||null,
comp_pressure_unit: compPressureUnit.id||null,
comp_regulation: compRegulation.id||null,
comp_cooling: compCooling.id||null,
comp_year: compYear.text||null,
comp_power_supply: compPowerSupply.id||null,
comp_power_supply_unit: null,
comp_motor_power: parseInt(compMotorPower)||null,
comp_motor_power_unit: compMotorPowerUnit.id||null,
comp_output: parseFloat(compOutput)||null,
comp_output_unit: compOutputUnit.id||null,
real_comp_data_id: idCreatedCompressor
}
/**
* Function to save Compressor Data.
* Error message will save in the 'message' state.
*/
const postCompressorSave = async (e) => {
debugger;
const isEmpty = Object.values(data).every(x => x === null || x === '');
if(isEmpty) {
return;
}
if(idCreatedCompressor !== "") {
postCompressorUpdate();
return;
}
const dataWithStatus = {...data, data_status_flow_id:status.IN_PROGRESS}
try{
const result = await authAxios.post(`/compressors`, dataWithStatus)
setIdCreatedCompressor(result.data)
console.log(idCreatedCompressor);
}catch(err){
console.log("AXIOS ERROR", err.message);
}
}
constNicknameInput = (fieldRenderProps) => {
const { validationMessage, visited, ...others } = fieldRenderProps;
return (
<div>
<label for="nickname-name" className="text-info">Nickname</label>
<Input type="text" name="nickname" placeholder=""
style={{height: "30px"}}
onChange={e=>setCompNickname(e.target.value)}
{...others}/>
{visited && validationMessage && <Error>{validationMessage}</Error>}
</div>
);
};
const SerialNoInput = (fieldRenderProps) => {
const { validationMessage, visited, ...others } = fieldRenderProps;
return (
<div>
<label for="serial-no-name" className="text-info">Serial No</label>
<Input type="text" name="serialNo" placeholder=""
style={{height: "30px"}}
onChange={e=>setCompSerialNo(e.target.value)}
{...others}/>
{visited && validationMessage && <Error>{validationMessage}</Error>}
</div>
);
};
const MotorPowerInput = (fieldRenderProps) => {
const { validationMessage, visited, ...others } = fieldRenderProps;
return (
<div>
<label for="motor-power-name" className="text-info">Motor Power</label>
<Input type="text" name="motor-power" placeholder="ex: 75"
style={{height: "30px"}}
onChange={e=>setCompMotorPower(e.target.value)}
{...others}/>
{visited && validationMessage && <Error>{validationMessage}</Error>}
</div>
);
};
const pressureUnitInput = (fieldRenderProps) => {
const { validationMessage, visited, ...others } = fieldRenderProps;
return (
<div>
<label for="rated-pressure-name" className="text-info">Rated Pressure</label>
<Input type="text" name="pressure" placeholder=""
style={{height: "30px"}}
onChange={e=>setCompPressure(e.target.value)}
{...others}/>
<label for="rated-pressure-unit" className="text-info">{compPressureUnit}</label>
{visited && validationMessage && <Error>{validationMessage}</Error>}
</div>
);
};
const OutputInput = (fieldRenderProps) => {
const { validationMessage, visited, ...others } = fieldRenderProps;
return (
<div>
<label for="output-name" className="text-info">Output</label>
<Input type="text" name="motor-power" placeholder="ex: 75"
style={{height: "30px"}}
onChange={e=>setCompOutput(e.target.value)}
{...others}/>
<label for="output-unit" className="text-info">{compOutputUnit}</label>
{visited && validationMessage && <Error>{validationMessage}</Error>}
</div>
);
};
return (
<Form className="mt-3 mb-4 px-0" onSubmit={postCompressorSave}
render={(formRenderProps) => (
<FormElement
style={{
maxWidth: 650,
}}
>
<fieldset className={"k-form-fieldset"}>
<div className="mb-3">
<Field
name={"nickname"}
component={NicknameInput}
validator={inputValidator}
/>
</div>
<div className="mb-3">
<Field
name={"serialNo"}
component={SerialNoInput}
validator={inputValidator}
/>
</div>
<div className="mb-3">
<label for="cooling-name" className="text-info">Cooling</label>
<DropDownList
value={compCooling}
textField="text"
dataItemKey="id"
onChange={(e) => {
setCompCooling(e.target.value);
}}
data={compCoolings}
/>
</div>
<div className="mb-3">
<label for="year-manufactured-name" className="text-info">Year Manufactured</label>
<DropDownList
value={compYear}
textField="text"
dataItemKey="id"
onChange={(e) => {
setCompYear(e.target.value);
}}
data={years}
/>
</div>
<div>
<label for="power-supply-name" className="text-info">Power Supply</label>
<DropDownList
value={compPowerSupply}
textField="text"
dataItemKey="id"
onChange={(e) => {
setCompPowerSupply(e.target.value);
}}
data={compPowerSupplies}
/>
</div>
<div className="mb-3">
<Field
name={"motorPower"}
component={MotorPowerInput}
validator={inputValidator}
/>
</div>
<div className="mb-3">
<Field
name={"ratedPressure"}
component={pressureUnitInput}
validator={inputValidator}
/>
</div>
<div className="mb-3">
<Field
name={"output"}
component={OutputInput}
validator={inputValidator}
/>
</div>
</fieldset>
<div className="k-form-buttons">
<button type={"submit"} className="btn btn-outline-success btn-block mt-1"
style={{height: "30px", fontSize: "16px", paddingTop: "4px"}}>
Save
</button>
</div>
</FormElement>
)}
/>
)
}