CSteipp has submitted this change and it was merged.

Change subject: Make use of field-specific HTMLForm errors
......................................................................


Make use of field-specific HTMLForm errors

These already use CSS styling and show an error right next to the bad input 
element instead of just at the top of the form

Change-Id: Ic2dea483c37b227ae0c38635007737bac965f983
---
M control/MWOAuthSubmitControl.php
M frontend/language/MWOAuth.i18n.php
M frontend/specialpages/SpecialMWOAuthConsumerRegistration.php
3 files changed, 68 insertions(+), 11 deletions(-)

Approvals:
  CSteipp: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/control/MWOAuthSubmitControl.php b/control/MWOAuthSubmitControl.php
index 4a5d6ae..d998beb 100644
--- a/control/MWOAuthSubmitControl.php
+++ b/control/MWOAuthSubmitControl.php
@@ -37,6 +37,13 @@
        }
 
        /**
+        * @param array $params
+        */
+       public function setInputParameters( array $params ) {
+               $this->vals = $params;
+       }
+
+       /**
         * Attempt to validate and submit this data
         *
         * This will check basic permissions, validate the action and paramters
@@ -72,8 +79,52 @@
        }
 
        /**
+        * Given an HTMLForm descriptor array, register the field validation 
callbacks
+        *
+        * @param array $descriptors
+        * @return array
+        */
+       public function registerValidators( array $descriptors ) {
+               foreach ( $descriptors as $field => &$description ) {
+                       if ( array_key_exists( 'validation-callback', 
$description ) ) {
+                               continue; // already set to something
+                       }
+                       $control = $this;
+                       $description['validation-callback'] =
+                               function( $value, $allValues ) use ( $control, 
$field ) {
+                                       return $control->validateFieldInternal( 
$field, $value, $allValues );
+                               };
+               }
+               return $descriptors;
+       }
+
+       /**
+        * This method should not be called outside MWOAuthSubmitControl
+        *
+        * @param string $field
+        * @param string $value
+        * @param array $allValues
+        * @return boolean|string
+        */
+       public function validateFieldInternal( $field, $value, $allValues ) {
+               $validators = $this->getRequiredFields();
+               if ( !isset( $validators[$allValues['action']][$field] ) ) {
+                       return true; // nothing to check
+               }
+               $validator = $validators[$allValues['action']][$field];
+               $isValid = is_string( $validator ) // regex
+                       ? preg_match( $validator, $value )
+                       : $validator( $value, $allValues );
+               if ( !$isValid ) {
+                       return wfMessage( 'mwoauth-invalid-field-generic' 
)->text();
+               }
+               return true;
+       }
+
+       /**
         * Get the field names and their validation regexes or functions
-        * (which return a boolean) for each action that this controller handles
+        * (which return a boolean) for each action that this controller 
handles.
+        * When functions are used, they take (field value, field/value map) as 
params.
         *
         * @return Array (action => (field name => validation regex or 
function))
         */
@@ -106,7 +157,7 @@
                        }
                        $valid = is_string( $validator ) // regex
                                ? preg_match( $validator, $this->vals[$field] )
-                               : $validator( $this->vals[$field] );
+                               : $validator( $this->vals[$field], $this->vals 
);
                        if ( !$valid ) {
                                // @TODO: check for field-specific message first
                                return $this->failure( "invalid_field_$field", 
'mwoauth-invalid-field', $field );
diff --git a/frontend/language/MWOAuth.i18n.php 
b/frontend/language/MWOAuth.i18n.php
index b08b127..699c5a7 100644
--- a/frontend/language/MWOAuth.i18n.php
+++ b/frontend/language/MWOAuth.i18n.php
@@ -18,6 +18,7 @@
 
        'mwoauth-missing-field' => 'Missing value for "$1" field',
        'mwoauth-invalid-field' => 'Invalid value provided for "$1" field',
+       'mwoauth-invalid-field-generic' => 'Invalid value provided',
 
        'mwoauth-field-hidden' => '(this information is hidden)',
        'mwoauth-field-private' => '(this information is private)',
@@ -273,6 +274,7 @@
        'mwoauth-verified' => 'Displayed to the user when the consumer does not 
have a callback URL, to provide the verification token that the consumer needs 
to complete the authorization process. Parameters:
 * $1 - Verificiation token
 * $2 - Request token (the app should already have this)',
+       'mwoauth-invalid-field-generic' => 'Used as error message',
        'mwoauth-missing-field' => 'Parameters:
 * $1 - field name
 See also:
diff --git a/frontend/specialpages/SpecialMWOAuthConsumerRegistration.php 
b/frontend/specialpages/SpecialMWOAuthConsumerRegistration.php
index c5ca26f..ce30d3c 100644
--- a/frontend/specialpages/SpecialMWOAuthConsumerRegistration.php
+++ b/frontend/specialpages/SpecialMWOAuthConsumerRegistration.php
@@ -73,8 +73,10 @@
                                throw new PermissionsError( 
'mwoauthproposeconsumer' );
                        }
 
+                       $dbw = MWOAuthUtils::getCentralDB( DB_MASTER ); // 
@TODO: lazy handle
+                       $control = new MWOAuthConsumerSubmitControl( 
$this->getContext(), array(), $dbw );
                        $form = new HTMLForm(
-                               array(
+                               $control->registerValidators( array(
                                        'name' => array(
                                                'type' => 'text',
                                                'label-message' => 
'mwoauth-consumer-name',
@@ -130,6 +132,7 @@
                                                                
MWOAuthUtils::getRightsByGrant()
                                                        )
                                                ),
+                                               'validation-callback' => null 
// different format
                                        ),
                                        'restrictions' => array(
                                                'type' => 'textarea',
@@ -149,17 +152,18 @@
                                                'type'    => 'hidden',
                                                'default' => 'propose'
                                        )
-                               ),
+                               ) ),
                                $this->getContext()
                        );
-                       $form->setSubmitCallback( function( array $data, 
IContextSource $context ) {
-                               $data['grants'] = FormatJSON::encode( // adapt 
form to controller
-                                       preg_replace( '/^grant-/', '', 
$data['grants'] ) );
+                       $form->setSubmitCallback(
+                               function( array $data, IContextSource $context 
) use ( $control ) {
+                                       $data['grants'] = FormatJSON::encode( 
// adapt form to controller
+                                               preg_replace( '/^grant-/', '', 
$data['grants'] ) );
 
-                               $dbw = MWOAuthUtils::getCentralDB( DB_MASTER );
-                               $controller = new MWOAuthConsumerSubmitControl( 
$context, $data, $dbw );
-                               return $controller->submit();
-                       } );
+                                       $control->setInputParameters( $data );
+                                       return $control->submit();
+                               }
+                       );
                        $form->setWrapperLegendMsg( 
'mwoauthconsumerregistration-propose-legend' );
                        $form->setSubmitTextMsg( 
'mwoauthconsumerregistration-propose-submit' );
                        $form->addPreText(

-- 
To view, visit https://gerrit.wikimedia.org/r/80150
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic2dea483c37b227ae0c38635007737bac965f983
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/OAuth
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to