n How to validate url parameter using regex in drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

The routing system that drupal 8 offers is flexible in terms of validation. It is possible to validate your route against some typical regex lines. 

Example 1

{id} placeholder should be a numeric character. 

/path/test/1 will be valid
/path/test/test will be 'page not found'

mymodule.controller:
  path: '/path/{method}/{id}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\mymoduleSaveController::Render'
    _title_callback: '\Drupal\mymodule\Controller\mymoduleSaveController::getTitle'
  requirements:
    _access: 'TRUE'
    id: '\d+'

Example 2 

{name} placeholder should string and contains only uppercase and lowercase alphabet.

/example/test will be valid
/example/100 will be 'page not found'

example.user
  path: '/example/{name}'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
  requirements:
    _permission: 'access content'
    name: '[a-zA-Z]+'

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook