r72281 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72280‎ | r72281 | r72282 >
Date:12:54, 3 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - implemented criteria validation error message method
Modified paths:
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/Validator.php
@@ -865,102 +865,85 @@
866866 /**
867867 * Returns an error message for a criteria validation that failed.
868868 *
 869+ * TODO: integrate this further with the hook mechanisms
 870+ * TODO: proper escaping
 871+ *
869872 * @since 0.4
870873 *
871 - * @param string $criteria
872 - * @param string $paramName
873 - * @param string $paramValue
874 - * @param array $args
875 - * @param boolean $isList
876 - * @param array $invalidItems
 874+ * @param string $criteria The name of the criteria
 875+ * @param string $paramName The name of the parameter, as provided by the user
 876+ * @param string $paramValue The value of the parameter, as provided by the user
 877+ * @param array $args The criteria arguments
 878+ * @param boolean $isList Indicates if the parameter is a list type or not
 879+ * @param array $invalidItems Can contain a list of invalid items for list parameters
877880 *
878881 * @return string
879882 */
880883 protected function getCriteriaErrorMessage( $criteria, $paramName, $paramValue, array $args = array(), $isList = false, array $invalidItems = array() ) {
881884 global $wgLang, $egValidatorErrorLevel;
882885
883 - if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $this->validator->hasErrors() ) {
884 - $rawErrors = $this->validator->getErrors();
 886+ if ( $isList ) {
 887+ switch ( $criteria ) {
 888+ case 'not_empty' :
 889+ $message = wfMsgExt( 'validator_list_error_empty_argument', array( 'parsemag' ), $paramName );
 890+ break;
 891+ case 'in_range' :
 892+ $message = wfMsgExt( 'validator_list_error_invalid_range', array( 'parsemag' ),$paramName, '<b>' . $args[0] . '</b>', '<b>' . $args[1] . '</b>' );
 893+ break;
 894+ case 'is_numeric' :
 895+ $message = wfMsgExt( 'validator_list_error_must_be_number', array( 'parsemag' ), $paramName );
 896+ break;
 897+ case 'is_integer' :
 898+ $message = wfMsgExt( 'validator_list_error_must_be_integer', array( 'parsemag' ), $paramName );
 899+ break;
 900+ case 'in_array' :
 901+ $itemsText = $wgLang->listToText( $args );
 902+ $message = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $paramName, $itemsText, count( $args ) );
 903+ break;
 904+ case 'invalid' : default :
 905+ $message = wfMsgExt( 'validator_list_error_invalid_argument', array( 'parsemag' ), $paramName );
 906+ break;
 907+ }
885908
886 - $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br /><i>';
887 -
888 - $errors = array();
889 -
890 - foreach ( $rawErrors as $error ) {
891 - $error['name'] = '<b>' . Sanitizer::escapeId( $error['name'] ) . '</b>';
892 -
893 - if ( $error['type'] == 'unknown' ) {
894 - $errors[] = wfMsgExt( 'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] );
 909+ if ( count( $invalidItems ) > 0 ) {
 910+ foreach ( $invalidItems as &$item ) {
 911+ $item = Sanitizer::escapeId( $item );
895912 }
896 - elseif ( $error['type'] == 'missing' ) {
897 - $errors[] = wfMsgExt( 'validator_error_required_missing', array( 'parsemag' ), $error['name'] );
898 - }
899 - elseif ( array_key_exists( 'list', $error ) && $error['list'] ) {
900 - switch( $error['type'] ) {
901 - case 'not_empty' :
902 - $msg = wfMsgExt( 'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] );
903 - break;
904 - case 'in_range' :
905 - $msg = wfMsgExt( 'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
906 - break;
907 - case 'is_numeric' :
908 - $msg = wfMsgExt( 'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] );
909 - break;
910 - case 'is_integer' :
911 - $msg = wfMsgExt( 'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] );
912 - break;
913 - case 'in_array' :
914 - $itemsText = $wgLang->listToText( $error['args'] );
915 - $msg = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) );
916 - break;
917 - case 'invalid' : default :
918 - $msg = wfMsgExt( 'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] );
919 - break;
920 - }
921 -
922 - if ( array_key_exists( 'invalid-items', $error ) ) {
923 - $omitted = array();
924 - foreach ( $error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item );
925 - $msg .= ' ' . wfMsgExt( 'validator_list_omitted', array( 'parsemag' ),
926 - $wgLang->listToText( $omitted ), count( $omitted ) );
927 - }
928 -
929 - $errors[] = $msg;
930 - }
931 - else {
932 - switch( $error['type'] ) {
933 - case 'not_empty' :
934 - $errors[] = wfMsgExt( 'validator_error_empty_argument', array( 'parsemag' ), $error['name'] );
935 - break;
936 - case 'in_range' :
937 - $errors[] = wfMsgExt( 'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
938 - break;
939 - case 'is_numeric' :
940 - $errors[] = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
941 - break;
942 - case 'is_integer' :
943 - $errors[] = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
944 - break;
945 - case 'in_array' :
946 - $itemsText = $wgLang->listToText( $error['args'] );
947 - $errors[] = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) );
948 - break;
949 - case 'invalid' : default :
950 - $errors[] = wfMsgExt( 'validator_error_invalid_argument', array( 'parsemag' ), '<b>' . htmlspecialchars( $error['value'] ) . '</b>', $error['name'] );
951 - break;
952 - }
953 - }
954 - }
955 -
956 - return $errorList . implode( $errors, '<br />' ) . '</i><br />';
 913+
 914+ $message .= ' ';
 915+ $message .= wfMsgExt(
 916+ 'validator_list_omitted',
 917+ array( 'parsemag' ),
 918+ $wgLang->listToText( $invalidItems ),
 919+ count( $invalidItems )
 920+ );
 921+ }
957922 }
958 - elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $this->validator->hasErrors() ) {
959 - return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $this->validator->getErrors() ) ) . '</b>';
960 - }
961923 else {
962 - return '';
 924+ switch ( $criteria ) {
 925+ case 'not_empty' :
 926+ $message = wfMsgExt( 'validator_error_empty_argument', array( 'parsemag' ), $paramName );
 927+ break;
 928+ case 'in_range' :
 929+ $message = wfMsgExt( 'validator_error_invalid_range', array( 'parsemag' ), $paramName, '<b>' . $args[0] . '</b>', '<b>' . $args[1] . '</b>' );
 930+ break;
 931+ case 'is_numeric' :
 932+ $message = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $paramName );
 933+ break;
 934+ case 'is_integer' :
 935+ $message = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $paramName );
 936+ break;
 937+ case 'in_array' :
 938+ $itemsText = $wgLang->listToText( $args );
 939+ $message = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $paramName, $itemsText, count( $args ) );
 940+ break;
 941+ case 'invalid' : default :
 942+ $message = wfMsgExt( 'validator_error_invalid_argument', array( 'parsemag' ), '<b>' . htmlspecialchars( $paramValue ) . '</b>', $paramName );
 943+ break;
 944+ }
963945 }
964946
 947+ return $message;
965948 }
966949
967950 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r72282Changes for 0.4 - follow up to r72281jeroendedauw13:00, 3 September 2010

Status & tagging log