Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / resources / src / mediawiki.special.changecredentials.js
1 /*!
2 * JavaScript for change credentials form.
3 */
4 ( function () {
5 mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
6 var api = new mw.Api();
7
8 $root.find( '.mw-changecredentials-validate-password.oo-ui-fieldLayout' ).each( function () {
9 var currentApiPromise,
10 self = OO.ui.FieldLayout.static.infuse( $( this ) );
11
12 self.getField().setValidation( function ( password ) {
13 var d;
14
15 if ( currentApiPromise ) {
16 currentApiPromise.abort();
17 currentApiPromise = undefined;
18 }
19
20 password = password.trim();
21
22 if ( password === '' ) {
23 self.setErrors( [] );
24 return true;
25 }
26
27 d = $.Deferred();
28 currentApiPromise = api.post( {
29 action: 'validatepassword',
30 password: password,
31 formatversion: 2,
32 errorformat: 'html',
33 errorsuselocal: true,
34 uselang: mw.config.get( 'wgUserLanguage' )
35 } ).done( function ( resp ) {
36 var pwinfo = resp.validatepassword,
37 good = pwinfo.validity === 'Good',
38 errors = [];
39
40 currentApiPromise = undefined;
41
42 if ( !good ) {
43 pwinfo.validitymessages.map( function ( m ) {
44 errors.push( new OO.ui.HtmlSnippet( m.html ) );
45 } );
46 }
47 self.setErrors( errors );
48 d.resolve( good );
49 } ).fail( d.reject );
50
51 return d.promise( { abort: currentApiPromise.abort } );
52 } );
53 } );
54 } );
55 }() );