Commit d660624b authored by Etroid's avatar Etroid Committed by nikunj
Browse files

Issue #2966510 by Etroid, nikunjkotecha, sgohil1: Clientside jQuery validation...

Issue #2966510 by Etroid, nikunjkotecha, sgohil1: Clientside jQuery validation not throwing error if element has #pattern (RegExp)
parent abd86e8b
......@@ -108,6 +108,16 @@ class ClientsideValidationDemoForm extends FormBase {
'#required' => FALSE,
];
$form['phone_1'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number'),
'#size' => 60,
'#maxlength' => 14,
'#pattern' => "[789][0-9]{9}",
'#required' => TRUE,
'#placeholder' => t('Enter Phone Number - [789][0-9]{9}'),
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
......
<?php
namespace Drupal\clientside_validation_jquery\Plugin\CvValidator;
use Drupal\clientside_validation\CvValidatorBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'pattern' validator.
*
* @CvValidator(
* id = "pattern",
* name = @Translation("Pattern"),
* supports = {
* "attributes" = {"pattern"}
* },
* attachments = {
* "library" = {"clientside_validation_jquery/jquery.validate.additional"}
* }
* )
*/
class Pattern extends CvValidatorBase {
/**
* {@inheritdoc}
*/
protected function getRules($element, FormStateInterface $form_state) {
return [
'messages' => [
'pattern' => isset($element['#pattern_error']) ? $element['#pattern_error'] : $this->t('@title does not meet the requirements.', ['@title' => $this->getElementTitle($element)]),
],
];
}
}
......@@ -107,6 +107,10 @@ class ClientsideValidationTest extends JavascriptTestBase {
$this->page->findById('edit-number-5')->setValue('101');
$assertions['edit-number-5'] = 'The value in Number 5 has to be greater than 100 by steps of 5.';
// Set number breaking step rule in number field 5.
$this->page->findById('edit-phone-1')->setValue('abc');
$assertions['edit-phone-1'] = 'Phone Number does not meet the requirements.';
$this->_validateAssertions($assertions);
// Set all valid values.
......@@ -121,6 +125,7 @@ class ClientsideValidationTest extends JavascriptTestBase {
$this->page->findById('edit-number-4')->setValue('100');
$this->page->findById('edit-number-5')->setValue('100');
$this->page->findById('edit-url')->setValue('http://example.com');
$this->page->findById('edit-phone-1')->setValue('9999999999');
// Trigger click of submit button.
$this->page->findButton('Submit')->click();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment