[英]Symfony Validation error: preg_match(): Compilation failed: range out of order in character class at offset
When I was verifying the operation of the app that was updated from Symfony 2.x to Symfony 4.4, the following error occurred.当我验证从 Symfony 2.x 更新到 Symfony 4.4 的应用程序的操作时,出现以下错误。
The function I was verifying was to enter the id "."我正在验证的 function 是输入 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.我删除了 form.errors,但它没有用。
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验证.yaml
getters:
loginId:
- Length: { max: 20, groups: [hqInitialSetting, shopInitialSetting, hqEdit, shopEdit] }
- Regex:
pattern: "/^[a-zA-Z0-9_-.]+$/"
groups: [hqInitialSetting, shopInitialSetting, hqEdit, shopEdit]
Staff.php职员.php
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use AppBundle\Model\Lib\Parameters;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* @ORM\Entity(repositoryClass="AppBundle\Model\Repository\StaffRepository")
* @ORM\Table(name="staff", uniqueConstraints={
* @ORM\UniqueConstraint(name="idx_staff_unique", columns={"staff_login_id"}),
* @ORM\UniqueConstraint(name="idx_image_mail_unique", columns={"image_mail"})
* })
* @ORM\HasLifecycleCallbacks
*/
class Staff implements AdvancedUserInterface, \Serializable, EquatableInterface
{
/**
* @ORM\Column(name="login_id", type="string", length=20, nullable=true, options={"comment"="ID"})
*/
protected $loginId;
twig 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) }}
I changed the regex and it worked fine.我更改了正则表达式,它工作正常。
The regular expression method may have changed due to the php version upgrade.由于 php 版本升级,正则表达式方法可能发生了变化。
pattern: "/^[a-zA-Z0-9-_.]+$/"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.