I have a virtual field called test in a Doctype. As you know, virtual fields aren’t visible unless they have a value. What I’m trying to do is transform this virtual field into a hidden input virtual field that allows users to enter data, which I can then use elsewhere without saving it to the database.
I attempted to implement this in my Doctype’s JavaScript file, but it’s not working as expected. Does anyone have suggestions or ideas on how I can achieve this?
onload: function(frm) {
// Show the virtual field as an input
frm.fields_dict['test'].df.hidden = 0; // Ensure it's not hidden
frm.fields_dict['test'].df.read_only = 0; // Make it editable
frm.refresh_field('test');
}
As far as I know, virtual fields are used to hold computed data based on other fields. (ex., calculates ‘age’ based on date of birth).
If you want to take a user’s input then use a data field to store their entry in the database, otherwise, where else would you want to store that input?
it would be better if you share an example of what you are trying to do or give a scenario,
virtual fields are hidden by default, you can use sessionStorage instead
The scenario is as follows: I have two doctypes, DT1 and DT2. I want the user to fill out data in DT1, and when they are done and hit the save button, I will create an instance of DT2 and populate it with data from a section in DT1. This data will then be inserted into DT2.
Here’s an example from dt1.py:
def after_insert(self):
if self.mode_of_payment == "DT2":
try:
# Create DT2 document
dt2 = frappe.new_doc("DT2")
dt2.bank_account_no = self.cheque_bank_account_no
# Link to Payment Entry
dt2.append("dt1", {
"dt1": self.name
})
# Save the DT2 document
dt2.insert()
except Exception as e:
frappe.msgprint(str(e))
else:
pass
This works fine when using normal fields. However, I now want to use virtual fields because I don’t want to store this data in the DT1 table. Instead, I want it to be stored only in DT2, and I haven’t found any field type that allows data usage without storing it in the table except for virtual fields.
but when I tried it I faced the problem that I talked about previously, which is the virtual fields are hidden and read only.
if there is any ideas on how to deal with virtual fields or how to handle my case in a different way be very grateful
is dialog can be used in this case, I can’t get your point about cutting off the reference of the payment method from the DT1 while DT2 was created based on a Field in DT1.
may this will help assume you want to add this field after a field named test
in the client script in the after_save method add this code
frappe.ui.form.make_control({
parent: frm.fields_dict.test.parent, // where your field will be rendered
df: {
label: 'Hold Virtual value',
fieldname: ''virtual_value',
fieldtype: 'Data',
options: ''' // the option will be based on the field type
},
render_input: true
})
this field will be in the fly if you getout of the form it will not be there. until you handle the cases when need it