-
-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable ability to require tags in profiles #23645
Conversation
(Standard links)
|
@@ -137,6 +137,9 @@ public static function buildQuickForm( | |||
|
|||
if (!empty($tags)) { | |||
$form->add('select2', 'tag', ts('Tag(s)'), $tags, FALSE, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]); | |||
if ($isRequired) { | |||
$form->addRule('tag', ts('Tag is a required field.'), 'required'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just change param 5 from FALSE to $isRequired? I haven't tested but it seems like that would do the same thing as the if
and addRule.
For unit testing you could try using $form = $this->getFormObject('CRM_Profile_Form_Edit', [params])
. I don't know offhand if there's an existing test that is similar - sometimes you have to fiddle a bit with the params and $_GET
to make it work for the given form, but then you can do stuff like call $form->buildForm(), $form->postProcess(), etc.
@demeritcowboy Thanks for the tip on But I'm banging my head against the wall with the test. I've added my work so far, but for some reason I can't seem to get the test for whether a required field is present to trigger. The commented out Any suggestions? |
$form = $this->getFormObject('CRM_Profile_Form_Edit', $formParams); | ||
$form->set('gid', $profileID); | ||
$form->preProcess(); | ||
$form->buildQuickForm(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking maybe adding $errors = $form->validate() here after buildQuickForm might do it but doesn't seem to. Hmm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it. This form handles its own error handling slightly differently than other forms (because it's Civi!) It expects there to be an errorUrl param and then does a redirect. The return value from validate() is just a true/false here, so it actually does work to call $form->validate(), I was just expecting an array of error messages. So you can do $this->assertFalse($form->validate());
after the buildQuickForm(), and don't need the postProcess since then you're done with the test.
d40e6a0
to
12d0654
Compare
Prior to this change, it was not possible to make tags required in a profile.
12d0654
to
e661b0f
Compare
@demeritcowboy Thank you!!! I was tearing my hair out as I spiraled down a recursive grep rabbit hole... |
Overview
This PR enables the ability to make the Tag field required in a profile. For additional discussion, see this stack exchange post.
Before
If you:
When you display the profile in create mode:
After
A red asterisk appears when the form is displayed and you are not able to submit the form without entering at least one tag.
Technical Details
It seems that this has simply been overlooked and nobody noticed or decided it should be fixed.
Comments
I tried to write a test to ensure it continues to work, but alas, the Api V3 profile submit code does, in fact, enforce the tags requirement. But, create mode does not seem to use that Api v3 function. I'm open to suggestions on how to properly test this feature.