How to pass empty value from front to backend

How to pass an empty value from the front so that it comes empty on the backend. What is it for? For example, to remove from a data field.

For example, when passing an empty string ” “the backend may return an error if the incoming data is validated for int or none.

To pass an empty value, we reduce it to null

At the front, if an empty value is passed, then turn it into null:

<input class=rub edit type=“text” bind:value=“{b.rub}” on:change={saveUsersData(b)}>

function saveUsersData(b) {
if (b.rub === ) {
b.rub = null
}

let data = {
‘rub’: b.rub,
}

patchFetch(‘balance/update/’, data);
}

On the backend, check this value for a number through pydantic:

sum_in_rub: optional[int]

We see that only int will pass and to an empty value ” “ will return an error.

And then we store either the value that is there or the value that is not:

def update_balance(user_id, rub):
if rub or rub is None:
db.session.query(Balance).filter_by(user_id=user_id, id=id_).update(
{‘rub’: rub}
)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *