Dexterity Choice Field with a Default Value

Is there a way to add a default value in a Dexterity choice field? I would like to make the field a required field but I would like the default value to be an empty value so that if the user does not make a choice, the form cannot be submitted.

„default“ parameter of the related field definition of your schema?

That is a problem of the select widget which you might use. If the field is required, there is no empty value available. You can try using the radio widget instead. With that, no value will be selected initially.

I think I had a workaround for that in one project. I'll try to find it.

You can set an explicit prompt for the select widget: https://github.com/zopefoundation/z3c.form/blob/master/src/z3c/form/browser/select.txt#L256

As you can see, even though the field is not required, only the explicit prompt is shown. However, the prompt will also be shown if the field is required

You can also customize the prompt message.

yes; check this, for instance:

and this:

1 Like

Thank you all for your quick responses. They are much appreciated.

Does it make a difference if I am using the model_source instead of the schema method? My type.xml looks very different from yours, hvelarde. The field "agency_division" is the field that I would like to have a "Please choose one:" as one of its choices but have an empty value.

This is in my profiles/types/form.xml:
<?xml version='1.0' encoding='utf8'?>
<model xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:indexer="http://namespaces.plone.org/supermodel/indexer" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>

      &lt;field name="agency_division" type="zope.schema.Choice" indexer:searchable="true"&gt;
        &lt;description/&gt;
        &lt;title&gt;Agency/Division&lt;/title&gt;
        &lt;values&gt;
          &lt;element&gt;Aging and Adult Services (DAAS)&lt;/element&gt;
          &lt;element&gt;Budget and Analysis&lt;/element&gt;
          &lt;element&gt;Child Development and Early Education (DCDEE)&lt;/element&gt;
          &lt;element&gt;General Counsel&lt;/element&gt;
          &lt;element&gt;Health Benefits (DHB)&lt;/element&gt;
          &lt;element&gt;Health Service Regulation (DHSR)&lt;/element&gt;
          &lt;element&gt;Human Resources (DHR)&lt;/element&gt;
          &lt;element&gt;Information Technology Division (ITD)&lt;/element&gt;
          &lt;element&gt;Internal Audit&lt;/element&gt;
          &lt;element&gt;Medical Assistance (DMA)&lt;/element&gt;
          &lt;element&gt;Mental Health, Developmental Disabilities, and Substance Abuse Services (DMHDDSAS)&lt;/element&gt;
          &lt;element&gt;Office of Communications&lt;/element&gt;
          &lt;element&gt;Office of Economic Opportunity&lt;/element&gt;
          &lt;element&gt;Office of Education Services&lt;/element&gt;
          &lt;element&gt;Office of Government Affairs&lt;/element&gt;
          &lt;element&gt;Office of NCTracks&lt;/element&gt;
          &lt;element&gt;Office of Procurement and Contract Services (OPC)&lt;/element&gt;
          &lt;element&gt;Office of Rural Health (DORH)&lt;/element&gt;
          &lt;element&gt;Office of the Controller (OOC)&lt;/element&gt;
          &lt;element&gt;Office of the Secretary (OOS)&lt;/element&gt;
          &lt;element&gt;Privacy and Security Office (PSO)&lt;/element&gt;
          &lt;element&gt;Property and Construction&lt;/element&gt;
          &lt;element&gt;Public Health (DPH)&lt;/element&gt;
          &lt;element&gt;Services for the Blind (DSB)&lt;/element&gt;
          &lt;element&gt;Services for the Deaf and Hard of Hearing (DSDHH)&lt;/element&gt;
          &lt;element&gt;Social Services (DSS)&lt;/element&gt;
          &lt;element&gt;State Operated Healthcare Facilities (DSOHF)&lt;/element&gt;
          &lt;element&gt;Vocational Rehabilitation Services (DVRS)&lt;/element&gt;
          &lt;element&gt;-----------------------------------------&lt;/element&gt;
          &lt;element&gt;NC Department of Health and Human Services (NCDHHS)&lt;/element&gt;
        &lt;/values&gt;
      &lt;/field&gt;

      &lt;field name="form_section_number" type="zope.schema.TextLine" indexer:searchable="true"&gt;
        &lt;description/&gt;
        &lt;title&gt;Form Section Number&lt;/title&gt;
      &lt;/field&gt;

      &lt;field name="form_effective_date" type="zope.schema.Date" indexer:searchable="true"&gt;
        &lt;description/&gt;
        &lt;title&gt;Form Effective Date&lt;/title&gt;
      &lt;/field&gt;

      &lt;field name="form_content" type="plone.app.textfield.RichText" indexer:searchable="true"&gt;
        &lt;description/&gt;
        &lt;required&gt;False&lt;/required&gt;
        &lt;title&gt;Form Content&lt;/title&gt;
      &lt;/field&gt;

      &lt;field name="form_file" type="plone.namedfile.field.NamedBlobFile"&gt;
        &lt;description/&gt;
        &lt;required&gt;False&lt;/required&gt;
        &lt;title&gt;Form File&lt;/title&gt;
      &lt;/field&gt;

    &lt;/schema&gt;
  &lt;/model&gt;</property>

You'll want to move your schema and FTI to a new folder called content and create a new xml file your profiles will point to. Working with the escaped xml that's generated by TTW is quite unwieldy.

The article from Plone Trainings I'm referencing explains the how and the why. Hope you find it helpful.

Dexterity: Moving contenttypes into code

Spencer, thanks so much for your help. I clicked on the link you suggested but it goes to a 404 error page:
https://training.plone.org/5/mastering_plone/export_code.html#return-to-dexterity-moving-contenttypes-into-code

Please advise. Thanks.

weird... I was going to post about the code missing (that URL was dead).

Try this Return to Dexterity: Moving contenttypes into Code

This documentation (Mastering Plone Development) is an excellent resource! I've been developing my content type too. Now if I could only get my vocabularies to work...

Cheers!

Aha! Thanks for the link. Yes, I believe I have seen this page before and have worked out the xml part of things. The issue now is trying to figure how to add what hvelarde has suggested into my xml page.

For reference: this is documented here and it is also possible to the parent context in the factory:

https://docs.plone.org/external/plone.app.dexterity/docs/advanced/defaults.html

Thanks MrTango! Cheers!

Plone Foundation Code of Conduct