How to remove/reset ember validation in form - ember.js

I have a form inside a modal, that form allows user to update each model feild and I'm also using ember-validations to validate them, but then after successful saving or when a user closes/cancel the modal the form element still contain the class has-success, has-error, has-feedback classes, so how can I reset the form fields after every successful saving/cancel?

It's common practice create new Form instance instead of cleaning errors. For each opening form/modal you should create new form
form: computed(function() {
return Form.create({ });
}),
actions: {
onShow() {
this.notifyPropertyChange('form');
}
}

Related

How to Reset form field after submit in HTMX

I'm tried to reset form field after submit the data in HTMX.
Please guide me how to do.
You can do with with htmx beforeswap https://htmx.org/docs/#modifying_swapping_behavior_with_events
To make the example below work django view must return HttpResponse(status=204)
htmx.on("htmx:beforeSwap", (e) => {
if (!e.detail.xhr.response) {
var title = $('#id_title');
if (title) {
title.val('');
}
}
})
Tutorial how to work with django forms and htmx https://blog.benoitblanchon.fr/django-htmx-modal-form/

How do I redirect a Django form submit to a new tab after form validation?

I have a Django form with two dropdown integer fields and a submit button.
A user selects values from these dropdown fields and click on submit button.
The Django template performs a form 'post' and sends these two dropdown values to my Django views.
Django views performs the form validation by validating these two dropdown fields values.
If the form is not valid, it will send the error message to the Django template and I can display the error message on the same form page above the form.
If the form is valid, then it will redirect to a new URL.
Issue: How do I tell my Django view to redirect the URL to a new tab only after the form is valid?
I'm assuming that you're already familiar with Djangos Form View. Within the form_valid(self, form) method of your class based view simply replace return super().form_valid(form) with redirect(reverse('YOUR_TARGET_VIEW')). If you have other mixins within your view that rely on form_valid you may also call super().form_valid(form) prior to returning the redirect reponse but without returning its returned data further.
As of displaying error messages you may refer to the django message framework.
EDIT: A cleaner way to do this, may be to overwrite success_url within your processing but this may not be appropriate to all use cases as you need to provide usefull defaults within each request path.

Django wizardview: how to set up step data without validating the form

What I am trying to do I have a view which has a shortcut form which will ask a user to pre-fill some fields before a user is redirected to wizard forms.
For example:
class PreFillView():
def post(self,request):
# get the data from the form and save into request.session
# Then http redirect to the wizard view
Then from this view, I redirect it to a WizardView. In the wizard view, I catch all the information passed in from the previous view in dispatch function:
class MyWizardView(NamedUrlSessionWizardView):
def dispatch(self,request, *args, **kwargs):
#parse data from request.session
#set step data using these data
# Note these data fields only partially covered the form in the wizardview, there is still a couple of fields needed to be filled in the wizard view.
This almost works fine but the only problem is that it validates the form and pop up field error for the fields which are not pre-populated. I tried, if I only redirect to the wizard view without setting the step data, it is fine. It won't validate the form, so no field errors will be displayed.
I am pretty new to Django and not sure if I am doing the right thing and if yes, how can I avoid form to be validated after I set the step data for the current step? Any help will be appreciated.
Implement the WizardView.get_form_initial(step) method in you wizard view class.
This method gets step number as parameters and it should return dict for initial data for the form for that step.

Validation of modelformsets from modelforms

One view I have is using a modelform with custom field cleaning. One type of cleaning is checking if the user is trying to submit a change to a field that is already set to the value, and it works exactly how I want it to work by throwing a ValidationError. The problem of course is that I can only submit one form at a time, so I'd like to use a modelformset to submit multiple forms.
I know it is possible to override the modelformset's clean method, but I'm asking if it's possible to use the modelform's field cleaning methods on the modelformset?. Currently when I submit the modelformset with empty fields the is_valid() passes which seems strange to me...
I also would like to know "typically" where the custom modelformset validation code would go? I was thinking with the forms.py.
*Edit -- with answer. My httpResponseRedirect was allowing the form to be submitted without validation.
def mass_check_in(request):
# queryset
qs = Part.objects.none()
errlst=[]
c = {}
c.update(csrf(request))
# Creating a model_formset out of PartForm
PartFormSetFactory = modelformset_factory(model=Part,
form=PartForm,
formset=BasePartFormSet,
extra=2)
if request.method == 'POST':
PartFormSet = PartFormSetFactory(request.POST)
if PartFormSet.is_valid():
PartFormSet.save()
return http.HttpResponseRedirect('/current_count/')
else:
PartFormSet = PartFormSetFactory(queryset=qs, initial=[
{'serial_number':'placeholder',
},
{'serial_number':'placeholder'
}])
return render(request,'mass_check_in.html',{
'title':'Add Item',
'formset': PartFormSet,
'formset_errors': PartFormSet.non_form_errors(),
})
If you don't enter any data at all in one of the modelforms in your model formset, it will skip validation for that form; from the docs:
The formset is smart enough to ignore extra forms that were not changed.
You can actually disable this functionality though by forcing empty_permitted=False on the forms; see the accepted answer to this question for more: Django formsets: make first required?
Formsets do have their own clean method, but it's used to validate information between two or more forms in the formset, not for validating the individual forms themselves (which should be taken care of in the forms clean method - as you are doing now.
A formset has a clean method similar to the one on a Form class. This is where you define your own validation that works at the formset level:
Here's another similar question:
Django formset doesn't validate

Django: Form in base template

Hi Stackoverflow people,
In my Django project I created a form to register users. This forms can be called through a specific url -> view method. Once the user has filled in the form, presses submit, the same view method will be called and if form.is_valid() is true, then ... simply a standard form, nothing special.
Now, I would like to integrate this little form on every page, and therefore I would like to add it to the base template. I have read that I could populate the form variable through a context_processor, but could I define the process after the submission of the form?
I have created the context_processor.py (as below), added the context_processor to the TEMPLATE_CONTEXT_PROCESSOR dir in the settings (as described here):
from app.forms import Form
def registration_form(request):
return {
registration_form : Form()
}
First of all, the form variable won't be displayed.
And secondly, how do I manipulate the form submission?
I think I misunderstanding the context_processor of Django and would be more than happy about comments on the overall process.
Thank you!
how are you trying to access to form in your template? you probably don't want to use a function name as your dictionary key, maybe you want
return {
'registration_form': Form(),
}
not sure what you mean by manipulate the form submission, but i guess you'd need all the form processing logic in your context processor
if request.POST:
form = Form(request.POST)
# validate etc
instead of creating context processor, create template tag for the purpose and place the tag in base.html
for form submission and displaying errors use ajax, and front-end validations.