Thursday, February 27, 2020

Re: How to do validation on a form which is opened by clicking a button

If the form is not valid you are creating a new form and returning this to the user.  So you need to remove the line where you create this second form object within the else clause of the view and it will work.

On Thu, Feb 27, 2020 at 9:10 PM Sunil BK <sunil.babu.k@gmail.com> wrote:
Hello community

I am trying to build a simple form which can be opened using a button. It should offer some fields which should be further process after validation. And there is exactly my issue. When I write some data into the fields and click the submit button. the validation error is NOT displayed below the screen like in this screeshot:


but is displayed in the log.

Can somebody give me a hint what I am missing here?
Thanks a lot!!
Sunil


This is the code I am using:

my HTML Code for presenting the form.
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{%block content%}
<br>
<h1 style="color:black">Add System to Organization</h1>
<br>
<div class="col" style="color:black">
<form method="post" action="{% url 'chr_add_system' %}" class="uniForm">
{% csrf_token %}
{{form|crispy}}
<input type="submit" value="Submit"/>
</form>
</div>
<br>
{%endblock content%}

the view:
def chr_add_system_view(request):
if request.method == 'POST':
form = AddChrSystemForm(request.POST)
if form.is_valid():
print('add_system-------->valid')
# TODO: do something
return redirect(chr_systems_view)
else:
print('form.errors ' + str(form.errors))
form = AddChrSystemForm()
context = {'form': form}
return render(request=request, template_name='chr/add_system.html', context=context)

the form:
class AddSystemForm(ModelForm):

def __init__(self, *args, **kwargs):
super(AddSystemForm, self).__init__(*args, **kwargs)
self.fields['profile_name'].widget.attrs['readonly'] = True

class Meta:
model = System
fields = '__all__'

def clean_system_id(self):
print('validating oid of system..')

system_id = self.cleaned_data['system_id']
print('system_id: ' + system_id)

if "2.16" not in system_id:
print('oid of system id is invalid')

raise forms.ValidationError(
"Your submissionset source oid seems to be invalid. ==> it has to be a valid oid and must start with: 2.16")
print('oid of system id is valid')
return system_id

the button where the form is called:
<div class="col system_title">
<font style="color:Black;"><b>SourceId</b></font>
<form method="post" action="{% url 'add_system' %}" class="uniForm">
{% csrf_token %}
<button type='submit' name="add_system"
class="btn btn-warning btn-sm float-right" value="{{item0}}">[WIP] Add System
</button>
</form>
</div>

my class
class System(models.Model):
system_id = models.CharField(max_length=100)
abbreviation = models.CharField(max_length=100, default="SRC-")
software_name = models.CharField(max_length=100, default="Portal")
vendor = models.CharField(max_length=100, default="vendor")
profile_name = models.CharField(max_length=100, default="Source")




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/28c122d0-72b9-4367-9f07-ce1939ad3b78%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJXS7_McJftTiy_2hjJ9oxANXbxisYFUSFmSF2%2B_zr32Bb%2BxcA%40mail.gmail.com.

No comments:

Post a Comment