0

With Solspace Rating I'm allowing members to submit ratings in 4 rating fields. However, to some people, not all fields will apply so they can select an "N/A" option for any of the fields as needed. This option has no value ("" not "0") so it doesn't affect rating stats.

I'm using allow_unrated="" to specify that these fields can be submitted with no value. But, I want the fields be be required - meaning they must select a valid rating (1-5) OR the "N/A" option, but they CANNOT select absolutely no option.

The trouble is, the N/A field with null value is equal to selecting nothing, which means Rating's own server-side field validation as well as my client-side form validation script think the field is empty even when it has a valid selection of "N/A".

So as far as I can figure, there's no way to allow unrated fields AND require those fields.

Setting the value of the "N/A" option to 0 is NOT a solution as that is an actual score and therefor pulls down average rating.

Here is some simplified template code for what I'd like to do (this doesn't work):

{exp:rating:form 
    form:class="box write-review validate" 
    form_name="{channel_short_name}" 
    collection="{channel_short_name}" 
    required="rating1|rating2|rating3|rating4" 
    allow_unrated="rating1|rating2|rating3|rating4" 
    allow_duplicates="no" 
    anonymous="no" 
    return="/vendor/rating-thanks/{segment_2}" 
    status="closed" 
}

{select name="rating1"}
{option value=""}-select a rating from 1-5{/option}
{option value="1"}1{/option}
{option value="2"}2{/option}
{option value="3"}3{/option}
{option value="4"}4{/option}
{option value="5"}5{/option}
{option value=""}N/A{/option}
{/select}

{select name="rating2"}
{option value=""}-select a rating from 1-5{/option}
{option value="1"}1{/option}
{option value="2"}2{/option}
{option value="3"}3{/option}
{option value="4"}4{/option}
{option value="5"}5{/option}
{option value=""}N/A{/option}
{/select}

{select name="rating3"}
{option value=""}-select a rating from 1-5{/option}
{option value="1"}1{/option}
{option value="2"}2{/option}
{option value="3"}3{/option}
{option value="4"}4{/option}
{option value="5"}5{/option}
{option value=""}N/A{/option}
{/select}

{select name="rating4"}
{option value=""}-select a rating from 1-5{/option}
{option value="1"}1{/option}
{option value="2"}2{/option}
{option value="3"}3{/option}
{option value="4"}4{/option}
{option value="5"}5{/option}
{option value=""}N/A{/option}
{/select}

{/exp:rating:form}

At present I have to make all the rating fields not required so the form can be submitted with the "N/A" options, but then there's no way to indicate to the member that they have forgotten to select a rating for any of the fields.

2 Answers 2

1

An empty value or the absence of a value will trigger the server-side "required" routine. Both your "select a rating" and "N/A" options have the same value and will trigger the error.

Using the required="" parameter in this case will therefore not work in your situation. Users who select "N/A" would trigger the required error, similarly to those leaving the selection on "select a rating".

You might have better luck with a javascript-based validation approach. For example, add an attribute to your "select a rating" option and check, through javascript, if that option is selected. If it is, throw a "required" js error. There is likely a number of approaches to this, and therefore cannot give any fail-safe examples here. Further, giving js code examples here would be outside of the score of this answer.

1
  • Thank you. This was the conclusion I made and I suspected a javascript validation would be the only way to make it work. I already validate the form with javascript but I need to come with a way to test for N/A values so I can still make them required. Commented Feb 5, 2014 at 3:31
0

Here's what I came up with to solve this dilemma:

  1. I used a "0" value instead of null on the N/A option values so on my client-side form validation the select boxes could easily be detected as required, and correcly identified as filled in even when the N/A option is selected.

  2. Once the form is validated via javascript I ran a function to swap out all "0" values for null before the form is submitted. (I never want to submit a 0 value or it will mess up rating stats/average).

  3. Form is then submitted with null values for any rating fields selected as N/A, and I've set those fields as not required in Rating's own required param so the submission is accepted. This means no server-side validation of those ratings fields but it shouldn't matter as it was already validated via javascript.

The result is a form that allows any of those fields to be unrated but also requires those select boxes to have some value selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.