2

I have a SPList, say LIST1, in which I will enter values for 6 columns [single line of text] while adding items. Now, I have a requirement, in which out of 6 columns, 4 columns should not be entered for 2 particular entries. How to prevent the end user entering values for the 2 columns for 2 particular titles?

I mean, for mytitle1, mytitle2, mytitle3 values, I can enter values for all the 6 columns without empty, it's mandatory to enter/fill all the values in all 6 columns.

But for myTitle5, myTitle6 I should not enter values for all the 6 columns, instead I need to fill only 4 columns. Remaining columns must be either greyed out / disabled mode. So column5, column6 must be empty/they should not enter anything in these columns while the end user editing the listitem.

How to achieve this functionality in a SPList? There won't be any change in this list in the future, it's been used as a configuration list. Only the values are going to be changed.

  • Means in NewItem Form, you want disply only four columns to enter data for all users .. right ? – Ronak Patel Apr 7 '17 at 12:31
4

While the other two answers here are correct in that it is fairly straightforward to hide the fields conditionally based on the Title value, what I think is going to be more of a problem for you is that you say that when all 6 fields are shown, they are all required.

This presents a problem, because if you make them required fields in the list, if you hide them using Javascript in the form, they are still required fields and you will not be able to save the item because those fields won't have values (and your users can't enter information because they're hidden).

So in this case, in addition to the Javascript code for showing/hiding the fields, you will also have to make them conditionally required, which means you will have to do your own field validation.

I can see two ways of doing that:

  1. Use PreSaveAction to check if the fields should be required, and if so, check if they have values. PreSaveAction happens before the default SharePoint field validation, so it would be sort of a two step process: you do your custom validation, if everything is OK, then SharePoint does it's validation.
  2. Use Client Side Rendering to register your own custom validation code with SharePoint so that your custom validation runs alongside the default SharePoint validation. Here is a good tutorial on how to add custom validation using CSR.
1

You need JavaScript, attach an onchange eventListener on the Title field,
then hide/show the other fields accordingly

The Show/Hide part has been asked and answered many times before here on SO, use the search

  • Have not worked on onchange event listener, except on the custom forms,visual web parts ,before. Will try and update here. – userAZLogicApps Apr 7 '17 at 16:16
1

You can manage through JavaScript

  • Write a function to hide/show fields you wanted by passing value of Title field. Hide fields on empty or matches to myTitle5, and myTitle6
  • Call this function on a page load.
  • Attach a blur event to the Title field and call the above reusable function.

The above steps works for New Form and Edit form.

Let me know which version of SharePoint you are using If you want code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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