Add validation constraints to contact.form.*

Created on 8 May 2024, 11 months ago

Problem/Motivation

Contact form entity is not yet fully validatable:

.vendor/bin/drush config:inspect --filter-keys=contact.form.feedback --detail --list-constraints
➜  🤖 Analyzing…

 Legend for Data: 
  ✅❓  → Correct primitive type, detailed validation impossible.
  ✅✅  → Correct primitive type, passed all validation constraints.
 -------------------------------------------------- --------- ------------- ------ --------------------------------------------------------------------------------------------- 
  Key                                                Status    Validatable   Data   Validation constraints                                                                       
 -------------------------------------------------- --------- ------------- ------ --------------------------------------------------------------------------------------------- 
  contact.form.feedback                              Correct   87%           ✅❓   ValidKeys: '<infer>'                                                                         
   contact.form.feedback:                            Correct   Validatable   ✅✅   ValidKeys: '<infer>'                                                                         
   contact.form.feedback:_core                       Correct   Validatable   ✅✅   ValidKeys:                                                                                   
                                                                                      - default_config_hash                                                                      
   contact.form.feedback:_core.default_config_hash   Correct   Validatable   ✅✅   NotNull: {  }                                                                                
                                                                                    Regex: '/^[a-zA-Z0-9\-_]+$/'                                                                 
                                                                                    Length: 43                                                                                   
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:dependencies                Correct   Validatable   ✅✅   ValidKeys: '<infer>'                                                                         
   contact.form.feedback:id                          Correct   Validatable   ✅✅   Length:                                                                                      
                                                                                      max: 32                                                                                    
                                                                                    ↣ PrimitiveType: {  }                                                                        
                                                                                    ↣ Regex:                                                                                     
                                                                                      pattern: '/^[a-z0-9_]+$/'                                                                  
                                                                                      message: 'The %value machine name is not valid.'                                           
   contact.form.feedback:label                       Correct   Validatable   ✅✅   Regex:                                                                                       
                                                                                      pattern: '/([^\PC])/u'                                                                     
                                                                                      match: false                                                                               
                                                                                      message: 'Labels are not allowed to span multiple lines or contain control characters.'    
                                                                                    NotBlank: {  }                                                                               
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:langcode                    Correct   Validatable   ✅✅   NotNull: {  }                                                                                
                                                                                    Choice:                                                                                      
                                                                                      callback: 'Drupal\Core\TypedData\Plugin\DataType\LanguageReference::getAllValidLangcodes'  
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:message                     Correct   Validatable   ✅✅   Regex:                                                                                       
                                                                                      pattern: '/([^\PC\x09\x0a\x0d])/u'                                                         
                                                                                      match: false                                                                               
                                                                                      message: 'Text is not allowed to contain control characters, only visible characters.'     
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:recipients                  Correct   NOT           ✅❓   ⚠️  @todo Add validation constraints to config entity type: contact.form.*                   
   contact.form.feedback:recipients.0                Correct   Validatable   ✅✅   Email:                                                                                       
                                                                                      message: '%value is not a valid email address.'                                            
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:redirect                    Correct   NOT           ✅❓   ⚠️  @todo Add validation constraints to config entity type: contact.form.*                   
   contact.form.feedback:reply                       Correct   Validatable   ✅✅   Regex:                                                                                       
                                                                                      pattern: '/([^\PC\x09\x0a\x0d])/u'                                                         
                                                                                      match: false                                                                               
                                                                                      message: 'Text is not allowed to contain control characters, only visible characters.'     
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:status                      Correct   Validatable   ✅✅   ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:uuid                        Correct   Validatable   ✅✅   Uuid: {  }                                                                                   
                                                                                    ↣ PrimitiveType: {  }                                                                        
   contact.form.feedback:weight                      Correct   Validatable   ✅✅   Range:                                                                                       
                                                                                      min: -2147483648                                                                           
                                                                                      max: 2147483647                                                                            
                                                                                    FullyValidatable: null                                                                       
                                                                                    ↣ PrimitiveType: {  }                                                                        
 -------------------------------------------------- --------- ------------- ------ --------------------------------------------------------------------------------------------- 

Steps to reproduce

  1. Get a local git clone of Drupal core 11.x.
  2. composer require drupal/config_inspector — or manually install https://www.drupal.org/project/config_inspector/releases/2.1.5 or newer (which supports Drupal 11!)
  3. composer require drush/drush
  4. vendor/bin/drush config:inspect --filter-keys=contact.form.feedback --detail --list-constraints

Proposed resolution

Add validation constraints to missing properties.

This requires looking at the existing code and admin UI (if any) to understand which values could be considered valid. Eventually this needs to be reviewed by the relevant subsystem maintainer.

For examples, search *.schema.yml files for the string constraints: 😊

Reach out to @borisson_ or @wimleers in the #distributions-and-recipes.

Remaining tasks

MR

User interface changes

None.

API changes

None.

Data model changes

More validation 🚀

Release notes snippet

None.

📌 Task
Status

RTBC

Version

11.0 🔥

Component
Contact 

Last updated 1 day ago

Created by

🇮🇳India narendraR Jaipur, India

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @narendraR
  • Status changed to Needs work 11 months ago
  • 🇮🇳India narendraR Jaipur, India

    😬 Forgot to change status while cloning

  • Merge request !7962constraint added → (Open) created by narendraR
  • Pipeline finished with Failed
    11 months ago
    Total: 705s
    #167399
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    #2: Hah! :D

    This is a good start, but there's a bunch left to do.

  • First commit to issue fork.
  • Pipeline finished with Failed
    11 months ago
    Total: 518s
    #168155
  • 🇫🇷France andypost

    I see no reason to allow weight to be nullable

  • Pipeline finished with Failed
    11 months ago
    Total: 544s
    #171419
  • Pipeline finished with Failed
    11 months ago
    Total: 582s
    #171558
  • Pipeline finished with Failed
    11 months ago
    #171677
  • Pipeline finished with Failed
    11 months ago
    Total: 603s
    #172161
  • Pipeline finished with Success
    11 months ago
    Total: 697s
    #172367
  • Pipeline finished with Failed
    11 months ago
    Total: 700s
    #172647
  • Pipeline finished with Canceled
    11 months ago
    Total: 8s
    #173028
  • Pipeline finished with Success
    11 months ago
    Total: 574s
    #173029
  • Status changed to Needs review 11 months ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to Needs work 11 months ago
  • 🇫🇷France andypost

    Left review on MR - we can add proper types with deprecation message as 11.x going beta

  • Pipeline finished with Canceled
    11 months ago
    Total: 77s
    #176820
  • Pipeline finished with Success
    11 months ago
    Total: 577s
    #176822
  • Pipeline finished with Failed
    11 months ago
    Total: 607s
    #176844
  • 🇮🇳India narendraR Jaipur, India

    I tried to add deprecation message in previous commit, not sure if this is the correct way or not. But due to this change tests started failing. https://git.drupalcode.org/issue/drupal-3445976/-/jobs/1632410#L2068

  • Pipeline finished with Failed
    11 months ago
    Total: 652s
    #178386
  • Pipeline finished with Success
    11 months ago
    Total: 2539s
    #178955
  • Status changed to Needs review 11 months ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to RTBC 11 months ago
  • 🇮🇳India srishtiiee

    The MR looks good, I think it just needs a CR for the deprecation. RTBC'd otherwise 👍🏼

  • Status changed to Needs work 11 months ago
  • 🇮🇳India narendraR Jaipur, India

    Moved to NW for fixing deprecation errors in better way

  • 🇮🇳India narendraR Jaipur, India

    I attempted to remove the @legacy from the tests, which I added in previous commit, but it seems something was done incorrectly in the MR, resulting in deprecation errors. I would appreciate any assistance.

  • First commit to issue fork.
  • Pipeline finished with Success
    11 months ago
    Total: 538s
    #185128
  • Pipeline finished with Failed
    11 months ago
    Total: 780s
    #185749
  • Pipeline finished with Success
    11 months ago
    Total: 670s
    #185793
  • Status changed to Needs review 11 months ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to Needs work 11 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    I noticed a few more things but this is looking very close to ready. It does still need a change record.

  • Pipeline finished with Canceled
    11 months ago
    Total: 139s
    #189487
  • Pipeline finished with Failed
    11 months ago
    Total: 613s
    #189488
  • Pipeline finished with Success
    10 months ago
    Total: 537s
    #191695
  • Status changed to Needs review 10 months ago
  • 🇮🇳India narendraR Jaipur, India
  • 🇺🇸United States mtift Minnesota, USA

    This one makes sense to me. All of the reply & redirect issues have been addressed per https://www.drupal.org/node/3452650 . The deprecation messages and tests make sense. The feedback has been addressed.

    However, I applied the MR on a standard install for 11.x with config inspector and got all green checks, but it shows "1 errors" and I'm not quite sure what that is referring to:

  • 🇺🇸United States phenaproxima Massachusetts

    I know nothing about config inspector, but I assume that it's flagging an error in the data in that config object, not any problem with the validation constraints themselves.

  • Status changed to RTBC 10 months ago
  • 🇺🇸United States smustgrave

    Regarding #18 did you run updb? When I applied the MR, before running updb, I saw the error in configuration inspector.

    On a standard 11.x profile install
    Installed configuration inspector
    Applied the MR
    Ran drush updb

    Reviewing the code thanks @phenaproxima for pointing out scope issue.

    Searched for .feedback.yml files and all appear to be updated.

    Believe this one is good to go.

  • 🇺🇸United States mtift Minnesota, USA

    That was it. Thank you @smustgrave!

    FWIW, this looks good to me, too.

  • Status changed to Needs work 10 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    This MR no prevents me from removing the message from a contact form. If I installed standard with this MR applied and go to admin/structure/contact/manage/feedback and remove all the text in the message field and press save the empty message field is not saved. If I remove the MR and a tried again I can empty the field and saves as expected.

  • Pipeline finished with Success
    10 months ago
    Total: 519s
    #213816
  • Pipeline finished with Success
    10 months ago
    Total: 629s
    #214632
  • Status changed to Needs review 10 months ago
  • 🇮🇳India narendraR Jaipur, India

    Re #22, I updated the code to set values to NULL.
    Reverting changes related to message(message: '') will fail ContactFormValidationTest.

  • Pipeline finished with Success
    10 months ago
    Total: 514s
    #214688
  • Status changed to Needs work 9 months ago
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    I missed this issue when it was originally active, we should start by rerolling this.
    Next we should look at https://git.drupalcode.org/project/drupal/-/merge_requests/7962#note_333039, to figure out how the schema should look.

  • First commit to issue fork.
  • 🇳🇱Netherlands bbrala Netherlands

    Merged 11.x into this, lets see what happens.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 205s
    #432037
  • Pipeline finished with Canceled
    about 2 months ago
    Total: 80s
    #432040
  • Pipeline finished with Failed
    about 2 months ago
    Total: 246s
    #432041
  • Pipeline finished with Failed
    about 2 months ago
    Total: 1054s
    #432446
  • Pipeline finished with Failed
    about 2 months ago
    Total: 376s
    #432460
  • 🇳🇱Netherlands bbrala Netherlands

    Well, 2 test failures:

    1. core/recipes/feedback_contact_form/recipe.yml -> The receipient input is not set when installing in core/tests/Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php, so it fails since ${resipient} is not a vaild email adress.
    2. core/tests/Drupal/FunctionalTests/Core/Recipe/StandardRecipeInstallTest.phphas the same issue, no recipient set.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 143s
    #432521
  • Pipeline finished with Failed
    about 2 months ago
    Total: 386s
    #432572
  • Pipeline finished with Failed
    about 2 months ago
    Total: 364s
    #432581
  • Pipeline finished with Failed
    about 2 months ago
    Total: 407s
    #432588
  • Pipeline finished with Failed
    about 2 months ago
    Total: 440s
    #432610
  • 🇳🇱Netherlands bbrala Netherlands

    Opened 📌 #3303126 was fixed, but a todo was not removed. Active since that should fix one of the tests. The references issue in the failing test tells us it was fixed and some code could be remove.d.

  • Pipeline finished with Failed
    18 days ago
    Total: 180s
    #459797
  • 🇳🇱Netherlands bbrala Netherlands

    Some more digging into the feedback form recipe.

    It seems the actions are applied, BUT InputConfigurator::collectAll is never called, so while installing it seems like it doesn't really collect anything from config, so the placeholder ${recipient} is never replaced, therefor we get install errors.

    FOund when debuging the failing test, stepping through RecipeRunner and seeing the $config_action_manager->applyAction($action_id, $config_name, static::replaceInputValues($data, $replace)); didnt really have any input values.

    So basically this part of therecipe:

    input:
      recipient:
        data_type: email
        description: 'The email address that should receive submissions from the feedback form.'
        constraints:
          NotBlank: []
        prompt:
          method: ask
          arguments:
            question: 'What email address should receive website feedback?'
        form:
          '#type': email
          '#title': 'Feedback form email address'
        default:
          source: config
          config: ['system.site', 'mail']
    

    Is never looking up the default value, therefor we enter a invalid value when the config is installed.

  • 🇳🇱Netherlands bbrala Netherlands

    Some discussion on the issue with recipes here: https://drupal.slack.com/archives/C2THUBAVA/p1743182816947909

    In order to load default values for recipes when quickstart is used we need to make sure default values that use config are actually found. We do this using a lookup array for the config path to $install_state.

    Unfortunately the default email address for an install needed changing also since drupal@localhost is not a valid email address. I changes this to @drupal.local

  • Pipeline finished with Failed
    18 days ago
    Total: 761s
    #459909
  • 🇳🇱Netherlands bbrala Netherlands

    Still failting on the standard install. Which has pretty much the. same problem, but unfortunately, does not configure system.site.mail at all.
    In the test:

    
        // Standard expects to set the contact form's recipient email to the
        // system's email address, but our feedback_contact_form recipe hard-codes
        // it to another value.
        // @todo This can be removed after https://drupal.org/i/3303126, which
        //   should make it possible for a recipe to reuse an already-set config
        //   value.
        ContactForm::load('feedback')?->setRecipients(['simpletest@example.com'])
          ->save();
    
    

    Which i alerady opened an issue for earlier. 📌 #3303126 was fixed, but a todo was not removed. Active . So guessing this might need to use another angle. Not sure what.

  • 🇳🇱Netherlands bbrala Netherlands

    Somewhat related: 📌 Add validation constraints to system.site Needs work

  • 🇳🇱Netherlands bbrala Netherlands
  • 🇳🇱Netherlands bbrala Netherlands
Production build 0.71.5 2024