|
|
@@ -8375,7 +8375,7 @@ class FormHelperTest extends TestCase
|
|
|
'name',
|
|
|
'required' => 'required',
|
|
|
'id' => '0-comments-1-comment',
|
|
|
- 'rows' => 5
|
|
|
+ 'rows' => 5,
|
|
|
],
|
|
|
'/textarea',
|
|
|
'/div'
|
|
|
@@ -8454,6 +8454,165 @@ class FormHelperTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * tests fields that are required use custom validation messages
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testHtml5ErrorMessage()
|
|
|
+ {
|
|
|
+ $this->Form->setConfig('autoSetCustomValidity', true);
|
|
|
+
|
|
|
+ $validator = (new \Cake\Validation\Validator())
|
|
|
+ ->requirePresence('email', true, 'Custom error message')
|
|
|
+ ->requirePresence('password')
|
|
|
+ ->alphaNumeric('password')
|
|
|
+ ->notBlank('phone');
|
|
|
+
|
|
|
+ $table = $this->getTableLocator()->get('Contacts', [
|
|
|
+ 'className' => __NAMESPACE__ . '\ContactsTable'
|
|
|
+ ]);
|
|
|
+ $table->setValidator('default', $validator);
|
|
|
+ $contact = new Entity();
|
|
|
+
|
|
|
+ $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
|
|
|
+ $this->Form->setTemplates(['inputContainer' => '{{content}}']);
|
|
|
+
|
|
|
+ $result = $this->Form->control('password');
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'password'],
|
|
|
+ 'Password',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'password',
|
|
|
+ 'name' => 'password',
|
|
|
+ 'type' => 'password',
|
|
|
+ 'value' => '',
|
|
|
+ 'required' => 'required',
|
|
|
+ 'onvalid' => 'this.setCustomValidity('')',
|
|
|
+ 'oninvalid' => 'this.setCustomValidity('This field is required')',
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $result = $this->Form->control('phone');
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'phone'],
|
|
|
+ 'Phone',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'phone',
|
|
|
+ 'name' => 'phone',
|
|
|
+ 'type' => 'tel',
|
|
|
+ 'value' => '',
|
|
|
+ 'maxlength' => 255,
|
|
|
+ 'required' => 'required',
|
|
|
+ 'onvalid' => 'this.setCustomValidity('')',
|
|
|
+ 'oninvalid' => 'this.setCustomValidity('This field cannot be left empty')',
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $result = $this->Form->control('email');
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'email'],
|
|
|
+ 'Email',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'email',
|
|
|
+ 'name' => 'email',
|
|
|
+ 'type' => 'email',
|
|
|
+ 'value' => '',
|
|
|
+ 'maxlength' => 255,
|
|
|
+ 'required' => 'required',
|
|
|
+ 'onvalid' => 'this.setCustomValidity('')',
|
|
|
+ 'oninvalid' => 'this.setCustomValidity('Custom error message')',
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * tests that custom validation messages are in templateVars
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testHtml5ErrorMessageInTemplateVars()
|
|
|
+ {
|
|
|
+ $validator = (new \Cake\Validation\Validator())
|
|
|
+ ->requirePresence('email', true, 'Custom error "message" & entities')
|
|
|
+ ->requirePresence('password')
|
|
|
+ ->alphaNumeric('password')
|
|
|
+ ->notBlank('phone');
|
|
|
+
|
|
|
+ $table = $this->getTableLocator()->get('Contacts', [
|
|
|
+ 'className' => __NAMESPACE__ . '\ContactsTable'
|
|
|
+ ]);
|
|
|
+ $table->setValidator('default', $validator);
|
|
|
+ $contact = new Entity();
|
|
|
+
|
|
|
+ $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
|
|
|
+ $this->Form->setTemplates([
|
|
|
+ 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
|
|
|
+ 'inputContainer' => '{{content}}'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $result = $this->Form->control('password');
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'password'],
|
|
|
+ 'Password',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'password',
|
|
|
+ 'name' => 'password',
|
|
|
+ 'type' => 'password',
|
|
|
+ 'value' => '',
|
|
|
+ 'required' => 'required',
|
|
|
+ 'data-message' => 'This field is required',
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $result = $this->Form->control('phone');
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'phone'],
|
|
|
+ 'Phone',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'phone',
|
|
|
+ 'name' => 'phone',
|
|
|
+ 'type' => 'tel',
|
|
|
+ 'value' => '',
|
|
|
+ 'maxlength' => 255,
|
|
|
+ 'required' => 'required',
|
|
|
+ 'data-message' => 'This field cannot be left empty',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+
|
|
|
+ $result = $this->Form->control('email', [
|
|
|
+ 'templateVars' => [
|
|
|
+ 'custom' => 'data-custom="1"'
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ $expected = [
|
|
|
+ 'label' => ['for' => 'email'],
|
|
|
+ 'Email',
|
|
|
+ '/label',
|
|
|
+ 'input' => [
|
|
|
+ 'id' => 'email',
|
|
|
+ 'name' => 'email',
|
|
|
+ 'type' => 'email',
|
|
|
+ 'value' => '',
|
|
|
+ 'maxlength' => 255,
|
|
|
+ 'required' => 'required',
|
|
|
+ 'data-message' => 'Custom error "message" & entities',
|
|
|
+ 'data-custom' => '1',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ $this->assertHtml($expected, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* testRequiredAttribute method
|
|
|
*
|
|
|
* Tests that formhelper sets required attributes.
|