Commit 74b91faf authored by JC Brand's avatar JC Brand

Use browser-based "constraint validation"

to validate required fields, instead of doing it manually.

All supported browsers have this feature.
parent f0debc61
...@@ -68,13 +68,16 @@ ...@@ -68,13 +68,16 @@
}, 300) }, 300)
.then(function () { .then(function () {
test_utils.openControlBox();
var cbview = _converse.chatboxviews.get('controlbox'); var cbview = _converse.chatboxviews.get('controlbox');
var registerview = cbview.registerpanel; var registerview = cbview.registerpanel;
spyOn(registerview, 'onProviderChosen').and.callThrough(); spyOn(registerview, 'onProviderChosen').and.callThrough();
registerview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called registerview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
spyOn(_converse.connection, 'connect'); spyOn(_converse.connection, 'connect');
var $tabs = cbview.$('#controlbox-tabs');
$tabs.find('li').last().find('a').click(); // Click the Register tab // Open the register panel
cbview.el.querySelector('.toggle-register-login').click();
// Check the form layout // Check the form layout
var $form = cbview.$('#converse-register'); var $form = cbview.$('#converse-register');
expect($form.find('input').length).toEqual(2); expect($form.find('input').length).toEqual(2);
...@@ -82,8 +85,8 @@ ...@@ -82,8 +85,8 @@
expect($form.find('input').last().attr('type')).toEqual('submit'); expect($form.find('input').last().attr('type')).toEqual('submit');
// Check that the input[type=domain] input is required // Check that the input[type=domain] input is required
$form.find('input[type=submit]').click(); $form.find('input[type=submit]').click();
expect(registerview.onProviderChosen).toHaveBeenCalled(); expect(registerview.onProviderChosen).not.toHaveBeenCalled();
expect($form.find('input[name=domain]').hasClass('error')).toBeTruthy();
// Check that the form is accepted if input[type=domain] has a value // Check that the form is accepted if input[type=domain] has a value
$form.find('input[name=domain]').val('conversejs.org'); $form.find('input[name=domain]').val('conversejs.org');
$form.find('input[type=submit]').click(); $form.find('input[type=submit]').click();
......
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
tpl_login_panel, tpl_login_panel,
tpl_search_contact, tpl_search_contact,
tpl_status_option, tpl_status_option,
tpl_spinner, tpl_spinner
) { ) {
"use strict"; "use strict";
...@@ -507,20 +507,16 @@ ...@@ -507,20 +507,16 @@
validate () { validate () {
const form = this.el.querySelector('form'); const form = this.el.querySelector('form');
const jid = form.querySelector('input[name=jid]').value; const jid_element = form.querySelector('input[name=jid]');
const password = _.get(form.querySelector('input[name=password]'), 'value'); if (jid_element.value &&
const errors = [];
if (!jid || (
!_converse.locked_domain && !_converse.locked_domain &&
!_converse.default_domain && !_converse.default_domain &&
_.filter(jid.split('@')).length < 2)) { _.filter(jid_element.value.split('@')).length < 2) {
errors.push(errors, 'invalid_jid'); jid_element.setCustomValidity(__('Please enter a valid XMPP address'));
return false;
} }
if (!password && _converse.authentication !== _converse.EXTERNAL) { jid_element.setCustomValidity('');
errors.push(errors, 'password_required'); return true;
}
this.model.set('errors', errors);
return errors.length == 0;
}, },
authenticate (ev) { authenticate (ev) {
...@@ -535,15 +531,14 @@ ...@@ -535,15 +531,14 @@
return; return;
} }
let jid = ev.target.querySelector('input[name=jid]').value; let jid = ev.target.querySelector('input[name=jid]').value;
const password = _.get(ev.target.querySelector('input[name=password]'), 'value');
if (_converse.locked_domain) { if (_converse.locked_domain) {
jid = Strophe.escapeNode(jid) + '@' + _converse.locked_domain; jid = Strophe.escapeNode(jid) + '@' + _converse.locked_domain;
} else if (_converse.default_domain && !_.includes(jid, '@')) { } else if (_converse.default_domain && !_.includes(jid, '@')) {
jid = jid + '@' + _converse.default_domain; jid = jid + '@' + _converse.default_domain;
} }
this.connect(jid, password); this.connect(
return false; jid, _.get(ev.target.querySelector('input[name=password]'), 'value')
);
}, },
connect (jid, password) { connect (jid, password) {
......
<form class="pure-form pure-form-stacked converse-form" id="converse-login" method="post"> <form class="pure-form pure-form-stacked converse-form" id="converse-login" method="post">
<legend>{{{__("Login")}}}</legend> <legend>{{{__("Login")}}}</legend>
<div class="conn-feedback fade-in {[ if (!conn_feedback_subject) { ]} hidden {[ } ]} {{{conn_feedback_class}}}"> <div class="conn-feedback fade-in {[ if (!conn_feedback_subject) { ]} hidden {[ } ]} {{{conn_feedback_class}}}">
<p class="feedback-subject">{{{ conn_feedback_subject }}}</p> <p class="feedback-subject">{{{ conn_feedback_subject }}}</p>
<p class="feedback-message {[ if (!conn_feedback_message) { ]} hidden {[ } ]}">{{{conn_feedback_message}}}</p> <p class="feedback-message {[ if (!conn_feedback_message) { ]} hidden {[ } ]}">{{{conn_feedback_message}}}</p>
</div> </div>
{[ if (auto_login || _converse.CONNECTION_STATUS[connection_status] === 'CONNECTING') { ]} {[ if (auto_login || _converse.CONNECTION_STATUS[connection_status] === 'CONNECTING') { ]}
<span class="spinner centered"/> <span class="spinner centered"/>
{[ } else { ]} {[ } else { ]}
{[ if (authentication == LOGIN || authentication == EXTERNAL) { ]} {[ if (authentication == LOGIN || authentication == EXTERNAL) { ]}
<label>{{{__("Jabber ID:")}}}</label> <label>{{{__("Jabber ID:")}}}</label>
<p class="form-help fade-in error {[ if (!_.includes(errors, 'invalid_jid')) { ]} hidden {[ } ]}"> <input autofocus required
{{{__('Please enter a valid XMPP address')}}}
</p>
<input autofocus
type="text" type="text"
name="jid" name="jid"
class="{[ if (_.includes(errors, 'invalid_jid')) { ]} error {[ } ]}"
placeholder="{{{placeholder_username}}}"> placeholder="{{{placeholder_username}}}">
{[ if (authentication !== EXTERNAL) { ]} {[ if (authentication !== EXTERNAL) { ]}
<label>{{{__("Password:")}}}</label> <label>{{{__("Password:")}}}</label>
<p class="form-help fade-in error {[ if (!_.includes(errors, 'password_required')) { ]} hidden {[ } ]}"> <input required
{{{__('Please enter your password')}}} type="password" name="password"
</p>
<input type="password"
name="password"
class="{[ if (_.includes(errors, 'password_required')) { ]} error {[ } ]}"
placeholder="{{{__('password')}}}"> placeholder="{{{__('password')}}}">
{[ } ]} {[ } ]}
<input class="pure-button button-primary" type="submit" value="{{{__('Submit')}}}"> <input class="pure-button button-primary" type="submit" value="{{{__('Submit')}}}">
......
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
<div class="form-errors hidden"></div> <div class="form-errors hidden"></div>
{[ if (default_domain) { ]} {[ if (default_domain) { ]}
{{{default_domain}}} {{{default_domain}}}
{[ } ]} {[ } ]}
{[ if (!default_domain) { ]} {[ if (!default_domain) { ]}
<input autofocus type="text" name="domain" placeholder="{{{domain_placeholder}}}"> <input autofocus required type="text" name="domain" placeholder="{{{domain_placeholder}}}">
<input class="pure-button button-primary" type="submit" value="{{{label_register}}}"> <input class="pure-button button-primary" type="submit" value="{{{label_register}}}">
{[ } ]} {[ } ]}
</form> </form>
......
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