Symfony Validation error: preg_match(): Compilation failed: range out of order in character class at offset

  php, symfony, symfony4

When I was verifying the operation of the app that was updated from Symfony 2.x to Symfony 4.4, the following error occurred.
The function I was verifying was to enter the id "!" To be validated and verify that the validation works correctly. Half-width alphanumeric characters, underscores (_), hyphens (-), and dots (.) Can be used in the target area.
Is there anything you can think of?
I deleted form.errors, but it didn’t work.

Error

preg_match(): Compilation failed: range out of order in character class at offset 13

Type

                $form->add("loginId", TextType::class, array(
                   "required" => false,
                ));

         $resolver->setDefaults(array(
             'validation_groups' => function (FormInterface $form) {
                 $options = $form->getConfig()->getOptions();
                 $loginStaff = $options["login_staff"];
                 $staff = $form->getData();
                 return $this->getValidationGroups($loginStaff, $staff);
             },
         ));
     private function getValidationGroups($loginStaff, $staff)
     {
         $validationGroups = array();
         if ($loginStaff->isManage()) {
             $validationGroups[] = 'manage';
         }
 
         if ($loginStaff->isHq()) {
             if ($staff->getPassword()) {
                 $validationGroups[] = 'hqEdit';
             } else {
                 $validationGroups[] = 'hqNew';
             }
         } else {
             if ($staff->getPassword()) {
                 $validationGroups[] = 'shopEdit';
             } else {
                 $validationGroups[] = 'shopNew';
             }
         }
 
         return $validationGroups;
     }

validation.yaml

    getters:      
        loginId: 
            - Length: { max: 20, groups: [hqInitialSetting, shopInitialSetting, hqEdit, shopEdit] } 
            - Regex:        
                pattern: "/^[a-zA-Z0-9_-.]+$/" 
                groups: [hqInitialSetting, shopInitialSetting, hqEdit, shopEdit]

Staff.php

use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserAdvancedUserInterface;
use AppBundleModelLibParameters;
use SymfonyComponentSecurityCoreUserEquatableInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
 /**
 *
 * @ORMEntity(repositoryClass="AppBundleModelRepositoryStaffRepository")
 * @ORMTable(name="staff", uniqueConstraints={
 *      @ORMUniqueConstraint(name="idx_staff_unique", columns={"staff_login_id"}),
 *      @ORMUniqueConstraint(name="idx_image_mail_unique", columns={"image_mail"})
 * })
 * @ORMHasLifecycleCallbacks
 */
class Staff implements AdvancedUserInterface, Serializable, EquatableInterface
{
    /**
     * @ORMColumn(name="login_id", type="string", length=20, nullable=true, options={"comment"="ID"})
     */
    protected $loginId;

twig

    {{ form_start(form) }}

        {{ form_errors(form) }}
            <div class="formGroup">
                <div class="formGroup">
                    {{ form_label(form.loginId, 'LOGIN ID') }}
                    {{ form_widget(form.loginId) }}
                    {{ form_errors(form.loginId) }}
                </div>
        </div>
        {{ form_rest(form) }}
    {{ form_end(form) }}

Source: Symfony Questions

LEAVE A COMMENT