Skip to content

[Form] Constraint Assert/Regex does not use HTML5 pattern attribute #3766

Closed
@marcospassos

Description

@marcospassos
#Customer.php

class Customer {
# (...)

   /**
    * @var string $phone
    * @Assert\NotBlank()
    * @Assert\Regex("/^\(\d{2}\) \d{4}-\d{4}$/")
    * @ORM\Column(name="phone", type="string", length=14)
    */
   private $phone;

# (...)
}
#CustomerType.php

class CustomerType extends AbstractType
{
# (...)
   public function buildForm(FormBuilder $builder, array $options)
   {
       $builder
           ->add('phone', null, array('label' => 'Telefone'))
       ;
   }
# (...)
}

HTML Result:

<input type="text" id="paggy_admin_customer_phone" name="paggy_admin_customer[phone]" required="required" maxlength="14" class="input-text">

Workaround: If I add the "pattern" property, duplicating the code of entity, it works as expected:

#CustomerType.php

class CustomerType extends AbstractType
{
# (...)
   public function buildForm(FormBuilder $builder, array $options)
   {
       $builder
           ->->add('phone', 'text', array('label' => 'Telefone', 'pattern' => '^\
(\d{2}\) \d{4}-\d{4}$'))
       ;
   }
# (...)
}

HTML Result:

<input type="text" id="paggy_admin_customer_phone" name="paggy_admin_customer[phone]" required="required" maxlength="14" pattern="^\(\d{2}\) \d{4}-\d{4}$" class="input-text">

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureFormGood first issueIdeal for your first contribution! (some Symfony experience may be required)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions