Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6751799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:58:10+00:00 2026-05-26T12:58:10+00:00

I’ve got a view with a few different forms in it. The first form

  • 0

I’ve got a view with a few different forms in it. The first form has a search field which populates a multiple choice field, and when the user types in their query in the search field, an AJAX call is sent to bring all records which match the query. The user then selects the choices from the first multiple choice field and clicks “Add” to move them to a different Multi Select Box

This works fine, but when the form submits, I receive an error that reads
“Select a valid choice.1 is not one of the available choices.”.
I have tried setting the choices after receiving arguments in the form init, but that doesn’t seem to work.

My Form:

class SiteCoordinatorForm(forms.ModelForm):
    selected_studies = forms.MultipleChoiceField(required = False)
    site = forms.ChoiceField(required = False)    
    studies = forms.MultipleChoiceField(required = False)
    study_search = forms.CharField(max_length = 50, required = False)

    def __init__(self, *args, **kwargs):
        super(SiteCoordinatorForm, self).__init__(*args, **kwargs)        
        if args:
            study_list = []
            query_dict = args[0]
            self.fields['selected_studies'].choices = [(int(x), x) for x in   query_dict.getlist('selected_studies')]

        self.fields['site'].choices = [(x.pk, "%s (%s)" % (x.primary_name, x.primary_number)) for x in Site.objects.all().order_by('primary_name')]

    class Meta:
        model = SiteCoordinator
        exclude = ('studies', 'site', 'selected_studies')

My AJAX function which populates the box:

def search_studies(request):
    return_data = {}
    studies = []
    make_query = lambda terms, fieldname: reduce(lambda x, y: x & Q(**{fieldname + '__icontains': y}), terms, Q())
    if 'search_text' in request.POST:
        terms = request.POST['search_text'].split()
        for rec in Study.objects.filter(make_query(terms, 'name')):
            studies.append({
                'study': {'id': rec.id, 'name': rec.name, 'number': rec.id}
            })

    response = {'status': 'success', 'count': len(studies), 'studies': studies}
    return HttpResponse(simplejson.dumps(response), mimetype="application/json")

The function to populate the MultiSelectBox:

function show_results(data, status_code, request){
    recs = data.studies;
    var select_box = document.getElementById('id_studies');
    select_box.options.length = 0;

    for (var index = 0; index < recs.length; index ++){
        select_box.options[index] = new Option(recs[index].study.name,
                        recs[index].study.id,
                        false, false);
    }
}

Then the JQuery to move the choices:

$('#add').click(function() {
    $('#id_studies option:selected').remove().appendTo('#id_selected_studies');
    return false;

});

$('#remove').click(function() {  
    $('#id_selected_studies option:selected').remove().appendTo('#id_studies');
    return false;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:58:11+00:00Added an answer on May 26, 2026 at 12:58 pm

    It is clear that we don’t want validations of MultipleChoiceField but do want MultipleSelectWidget so solution is simply to change field to

    studies = forms.CharField(widget=forms.SelectMultiple)
    

    and do your own validations in

    def clean_studies(self):
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.