Score:0

Conditional form validation by role

us flag

I'm lost on how to do this. I need guest users to have the form fully validated (using the client side validation module) upon creation, but not validating for an authenticated user.

tldr;

The customer (always a guest) will be able to place an order for pickup on a particular date, that date cannot be earlier than 5 days from "today". The customer must always obey the validation rules. Easy, these are set up and working.

When a staff member (always an authenticated user) places a phone order for a customer, they need to be able to override that date, even to one less than 5 days out, skipping the validation.

Similarly, when the staff member edits/updates the webform, they need to be able to do so when it's less than the 5 days. The current client side validation module prohibits anyone from editing the form in less than five days, unless I have the date hidden from them.

I'm struggling to find a process or module that will make this work. Any ideas?

Score:0
cn flag

The simplest thing is to apply the validation as you normally would, and then write a hook_form_alter() that does the following:

  1. Determine if the current user is logged in, or has a particular role.
  2. If logged in, remove the validation pieces from the form render array of that particular field.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.