How to make fields editable only that are selected in configuration(Odoo16)? - javascript

I have a many2many field defined on model "ir.model.fields" set in configuration screen. Where manager can select fields that should be editable by user.
Now , my complete form is readonly but I want to make only that fields editable which are selected in configuration. In short, a user can edit only that fields that are allowed to edit.
How to use this condition in Odoo16 ?
I tried below:
I created manay2many computed field that have all fields that are allowed to edit in employee form and add set attrs in field in xml like this:
editable_fields --> computed many2many field
But still not working!!

Related

(Django) how to submit a form with empty fields?

I have a form with a drop down list with two options ("MCQ" and "SEQ") and some fields. Selecting "MCQ" would hide fields used by "SEQ" and vice versa. I want to submit this form but the "please filled out this field" notice pops up. What should I do to submit this form?
I've searched around Stack overflow before looking possible solutions, some suggested placing "required" or "disabled" beforehand, however I would like a dynamic way of doing if possible but based it on which option the user selects since the user might leave everything empty and just submit if nothing is required.
Additional Information:
I'm using forms.ModelForm, the fields came from there.
I'm using js to hide and show the relevant fields.
I Appreciate any help/suggestions. :)
Your model should not define those fields as required, this way they can be empty when the form is validated:
class MyModel:
my_field = models.CharField(blank=True, null=True, length=500)
...
blank=True will tell your forms not return an error if this field is empty

Under what conditions is an input submitted?

When submitting a form, what inputs are submitted?
As an example:
disabled inputs are not submitted...
inputs wihtout a name attribute are not submitted...
I am looking for a more comprehensive/official document which explains what inputs are submitted?
There's a range of conditions.
The control's form owner must be the form being submitted and none of the following are true:
The field element has a datalist element ancestor.
The field element is disabled.
The field element is a button but it is not submitter.
The field element is an input element whose type attribute is in the Checkbox state and whose checkedness is false.
The field element is an input element whose type attribute is in the Radio Button state and whose checkedness is false.
The field element is an object element that is not using a plugin.
and a name must be established.
Details are in the HTML5 spec at 4.10.21.4 Constructing the entry list
As W3 document (https://www.w3.org/TR/html401/interact/forms.html#successful-controls) said:
A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.
However:
Controls that are disabled cannot be successful.
If a form contains more than one submit button, only the activated submit button is
successful.
All "on" checkboxes may be successful.
For radio buttons that share the same value of the name attribute, only the "on" radio
button may be successful.
For menus, the control name is provided by a SELECT element and values are provided by OPTION elements. Only selected options may be successful. When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted.
The current value of a file select is a list of one or more file names. Upon submission of the form, the contents of each file are submitted with the rest of the form data. The file contents are packaged according to the form's content type.
The current value of an object control is determined by the object's implementation.
More details in the document.

Form validation in javascript when drop down dependency is involved

I have used ng-disabled for my form validation for ADD button.
i.e. ng-disabled="conditionForm.$invalid". But , My form contains two text boxes which are hidden at first , and when a type is selected from drop down , only the respective Text box div should be visible. The Problem i'm facing is ,when the above ng-disabled validation is used , the ADD button is still disabled when one of the text box is selected and an input is provided. After the second input is also selected from the drop-down , then the ADD button is getting enabled.
Can you please provide me with an alternate validation , where the ADD button can get enabled every time a value is selected from drop down and a valid input is provided.
If you're ng-disabled is on a form being valid or invalid it sounds like those may have required inputs set. If that is case look at using ng-required so you can explicitly set required based on expression. You can then control when the form is valid.
The exact answer: Answer

Hidden input fields causing modal form to be invalid

I have an angular js application in which I have a modal form to capture payment details. I am validating the input fields using angular required attribute as below.
<input name="cardNumInput" required type="text" size="20" class="form-control" ng-model="userInput.paymentParams.CardNumber" ng-minlength="13" ng-maxlength="19"/>
Certain fields are visible based on user selections. The requirement I have is that only the visible fields be validated with the required attribute. But it looks like even the hidden inputs are being validated. My submit button is enabled only if the whole form is valid. How do I tell angular to not validate the hidden fields.
http://plnkr.co/edit/8REV5ai52QylxuBbYCrn?p=preview
I also have the problem of not able to indicate the user that my dropdowns are also mandatory - by showing the red border like on input fields.
It doesn't matter for a validation if an input is visible or not. So try use ngIf in stead of ngShow
upd
for the select highlighting you can use the same approach as with inputs (set the class with ngClass directive), but Bootstrap has no error styles for the select element, so you should define your own (or you can just extend it you you are using less)
for example
select.has-error{ border: 1px solid #a94442;}
http://plnkr.co/edit/FMLxDKBkPkuHmtEvzMrL?p=preview

Removing required from a form element via Javascript

I have a form that hides and shows option based on a drop down. If one option is selected there is a form that had required fields (). If the other option is selected I need to disable those required fields or the form won't submit.
Long and short. How do I remove the required for the input element via Javascript?
I was trying document.getItemById('firstName').?
document.getElementById('firstName').removeAttribute('required');

Categories