Validate custom field before saving ticket

68 views
Skip to first unread message

pineapplehandler

unread,
Mar 30, 2015, 2:15:22 PM3/30/15
to trac-...@googlegroups.com
We need to force users to make selections from two customized drop-down fields (previously names Component and Type - renamed with BlackMagicTicketTweaks) before saving a new ticket. We'd like it to operate similar to the way Trac forces entering a ticket summary when clicking preview:
Warning: Tickets must contain a summary.       

I've looked at Ticket Validator (which might work if I can figure out how to code it for our custom fields) and to a lesser extent Trac Ticket Conditional Validate Fields Plugin, which doesn't seem compatible with ver. 0.12.5.

It was suggested that I might ask here to see if there any other options we might want to check out.

Thanks,
Mike



pineapplehandler

unread,
Mar 31, 2015, 5:38:30 PM3/31/15
to trac-...@googlegroups.com
Say I want to use the Ticket Validator plugin, and I want to test the Component and Type fields for specific values. Is this the correct format to enter the field names in the .ini? I'm totally guessing on the 'str' part here:
[fieldscheck]
type
= str
component
= str

And does this look right for the next section?
[ticketvalidator]
new.required = type,component


Ryan Ollos

unread,
Apr 2, 2015, 12:38:53 AM4/2/15
to trac-...@googlegroups.com
On Tue, Mar 31, 2015 at 2:38 PM, pineapplehandler <mbhu...@iastate.edu> wrote:
Say I want to use the Ticket Validator plugin, and I want to test the Component and Type fields for specific values. Is this the correct format to enter the field names in the .ini? I'm totally guessing on the 'str' part here:
[fieldscheck]
type
= str
component
= str

I'm not sure which plugin the [fieldscheck] section applies to. It isn't defined by TicketValidatorPlugin:
 
And does this look right for the next section?
[ticketvalidator]
new.required = type,component

Neither type nor component can have an empty value in Trac < 1.1.2. The [ticket] optional_fields option was added in Trac 1.1.2:

I tested with 0.2dev-r14386 of TicketValidatorPlugin. Using two fields that allow an empty value, I can confirm that the plugin works correctly with Trac 1.0-stable.

[ticketvalidator]
new.required = version, milestone

Inline image 1


As for using it with your custom field, I can confirm the following works:

[ticket-custom]
the_custom = select
the_custom.options = |one|two|thr|fou

[ticketvalidator]
new.required = the_custom

Inline image 2

pineapplehandler

unread,
Apr 2, 2015, 9:38:13 AM4/2/15
to trac-...@googlegroups.com, ryan.j...@gmail.com
On Wednesday, April 1, 2015 at 11:38:53 PM UTC-5, RjOllos wrote:
I'm not sure which plugin the [fieldscheck] section applies to. It isn't defined by TicketValidatorPlugin:

I was trying to incorporate the changes made by Dan Winslow in this comment. The end-goal is to trigger the error when the field hasn't been moved from the default value. The fields are pre-populated with a dropdown list:
# trac-admin /var/www/projects/svn-OurProject ticket_type list

Possible Values              
-----------------------------
--- Please choose a state ---
01 Alabama                  
04 Arizona                  
05 Arkansas                  
06 California                
08 Colorado                  
...

So we'd like to query to be sure the user moved the default from "--- Please choose a state ---" 
 
As for using it with your custom field, I can confirm the following works:

[ticket-custom]
the_custom = select
the_custom.options = |one|two|thr|fou

[ticketvalidator]
new.required = the_custom

Inline image 2

I guess I went a different route. Is it possible to replace the text "--- Please choose a state ---" with null? Seems like that should resolve this. I've tried several methods but it doesn't seem legal. Do we need to define the values in the ini file like you did above rather than populating them via trac-admin?

pineapplehandler

unread,
Apr 3, 2015, 9:14:48 AM4/3/15
to trac-...@googlegroups.com, ryan.j...@gmail.com


On Thursday, April 2, 2015 at 8:38:13 AM UTC-5, pineapplehandler wrote:
I was trying to incorporate the changes made by Dan Winslow in this comment. The end-goal is to trigger the error when the field hasn't been moved from the default value. The fields are pre-populated with a dropdown list...So we'd like to query to be sure the user moved the default from "--- Please choose a state ---" 

We ended up pushing this through by modifying the code within core.py for  the Ticket Validator plugin. To correct the error message we had to create field_name_alias to feed the updated field names into the error function. Here's the code we changed in /usr/local/lib/python2.7/dist-packages/ticketvalidator/core.py:
         field_name_alias = {}                            

       
for field_name_temp in required_fields:                
           
if (field_name_temp == "type"):                
                field_name_alias
[field_name_temp] = "State"
           
elif field_name_temp == "component":            
                field_name_alias
[field_name_temp] = "Table"
           
else:                                    
                field_name_alias
[field_name_temp] = field_name_temp

        errors
= [(field_name_alias[field_name], '%s is required' % field_name_alias[field_name])
                 
for field_name in required_fields
                 
if (not ticket[field_name]) or (ticket[field_name] == '--- Please choose a state ---') or (ticket[field_name] == '--- Please select a table ---')]

       
return errors

Added the fields to /etc/trac/svn-MyProject.ini:
[ticketvalidator]
new.required = type,component

In hindsight it would have been cleaner and easier to simply hide the Type and Component fields and created our own custom fields in the trac.ini file.

RjOllos

unread,
Apr 12, 2015, 4:52:56 PM4/12/15
to trac-...@googlegroups.com, ryan.j...@gmail.com
It sounds like your use case is similar to the request in #11666. Rather than validating a field to be non-empty, we could validate the field to not be some specified value.

Another approach would be to have TicketValidatorPlugin add a label on an empty field, and an empty field in the case that the field doesn't allow the empty value. This would avoid creating dummy values like your Component "--- Please choose a state ---"

- Ryan 

Steffen Hoffmann

unread,
Apr 13, 2015, 5:31:13 PM4/13/15
to trac-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 12.04.2015 22:52, RjOllos wrote:
> Another approach would be to have TicketValidatorPlugin add a label on
> an empty field, and an empty field in the case that the field doesn't
> allow the empty value. This would avoid creating dummy values like your
> Component "--- Please choose a state ---"

Nice idea. I've often seen trailing (red) star behind a required input
field label as visual sign for required input. If others agree, that
this is sufficiently common, it could become a Trac default visual style
too.

Steffen Hoffmann
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlUsNZEACgkQ31DJeiZFuHf+sgCg0bUNtKvgtIhLgbGbljbg5yV0
5h4An20zyk+wYO7KJN76Mahww4Gdpjbw
=Bnz0
-----END PGP SIGNATURE-----

RjOllos

unread,
Apr 14, 2015, 5:33:07 PM4/14/15
to trac-...@googlegroups.com
On Monday, April 13, 2015 at 2:31:13 PM UTC-7, hasienda wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 12.04.2015 22:52, RjOllos wrote:
> Another approach would be to have TicketValidatorPlugin add a label on
> an empty field, and an empty field in the case that the field doesn't
> allow the empty value. This would avoid creating dummy values like your
> Component "--- Please choose a state ---"

Nice idea. I've often seen trailing (red) star behind a required input
field label as visual sign for required input. If others agree, that
this is sufficiently common, it could become a Trac default visual style
too.

Steffen Hoffmann

That sounds good. The BatchModifyPlugin had that behavior:
Reply all
Reply to author
Forward
0 new messages