PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/includes/class.AjaxValidator.php

https://github.com/reshadf/Library
PHP | 174 lines | 131 code | 13 blank | 30 comment | 22 complexity | 47eee7489c0b42396f7801664de17961 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Class AjaxValidator
  4. *
  5. * Uses the validator class to validate FormHandler fields on the fly.
  6. *
  7. * @since 03-12-2008
  8. * @author Johan Wiegel @ PHP-GLOBE
  9. * @package FormHandler
  10. *
  11. */
  12. class AjaxValidator
  13. {
  14. static $bScript = true;
  15. function AjaxValidator( $bScript = true )
  16. {
  17. self::$bScript = $bScript;
  18. }
  19. function CreateObservers( $oForm )
  20. {
  21. $bSetJS = false;
  22. // needed javascript included yet ?
  23. if(!$bSetJS)
  24. {
  25. $bSetJS = true;
  26. // add the needed javascript
  27. // only include library when bScript is treu
  28. if( self::$bScript === true )
  29. {
  30. $oForm->_setJS( FH_FHTML_DIR."js/jquery-1.4.2.min.js", true );
  31. }
  32. $oForm->_setJS( FH_FHTML_DIR."js/ajax_validator.js", true,false );
  33. // create observers for all fields with a validator
  34. $sScript = "";
  35. $sScript2 = "";
  36. /*@var $oForm FormHandler*/
  37. foreach( $oForm->_fields AS $sField => $aField )
  38. {
  39. if( is_object( $aField[1]) && method_exists( $aField[1], 'getValidator' ) )
  40. {
  41. if( isset( $oForm->_customMsg[$sField][0] ) AND $oForm->_customMsg[$sField][0] != '' )
  42. {
  43. $sMsg = addslashes( $oForm->_customMsg[$sField][0] );
  44. }
  45. else
  46. {
  47. $sMsg = $oForm->_text(14);
  48. }
  49. // only when a validator is defined for this field
  50. if( $aField[1]->getValidator() != '' )
  51. {
  52. if( is_a( $aField[1], 'selectField' ) )
  53. {
  54. $sEvent = 'change';
  55. }
  56. elseif( is_a( $aField[1], 'timeField' ))
  57. {
  58. $sField .= "_hour";
  59. $sEvent = 'blur';
  60. }
  61. elseif( is_a( $aField[1], 'checkBox' ) )
  62. {
  63. // no on the fly checking yet
  64. $sEvent = '';
  65. }
  66. elseif( is_a( $aField[1], 'radioButton' ) )
  67. {
  68. // no on the fly checking yet
  69. $sEvent = '';
  70. }
  71. else
  72. {
  73. $sEvent = 'blur';
  74. }
  75. if( $sEvent != '' )
  76. {
  77. $sScript .= " jQuery('#".$sField."').live( '".$sEvent."', function(){FH_VALIDATE( '".$aField[1]->getValidator()."', '".$sField."', '".$sField."', '".FH_FHTML_DIR."', '".FH_INCLUDE_DIR."','".$sMsg."' )});\n";
  78. if( $aField[1]->getValidator() <> 'FH_CHECK_DOMAIN' )
  79. {
  80. $sScript2 .= "FH_VALIDATE( '".$aField[1]->getValidator()."', '".$sField."', '".$sField."', '".FH_FHTML_DIR."', '".FH_INCLUDE_DIR."','".$sMsg."' );";
  81. }
  82. }
  83. }
  84. }
  85. }
  86. $oForm->_setJS( '$(function(){'.$sScript.'})', false, true );
  87. //na een post ook de AJAX validators aanroepen om de classes te switchen
  88. /**
  89. * very alpha, we vinden dat het anders moet, maar weten nog niet hoe, nog niet documenteren en/of publiceren
  90. *
  91. * @author Johan Wiegel
  92. * @since 02-09-2009
  93. */
  94. if( $oForm->isPosted() )
  95. {
  96. $oForm->_setJS( $sScript2,false,false );
  97. }
  98. }
  99. }
  100. function Validate( $aRequest, $oValidator )
  101. {
  102. // determin if there is more than one validator
  103. if( $aRequest['validator'] != '' AND $aRequest['msg'] != '' AND isset( $aRequest['value'] ) )
  104. {
  105. if( strpos( $aRequest['validator'], '|' ) > 0 )
  106. {
  107. $aValidators = explode( '|', $aRequest['validator'] );
  108. }
  109. else
  110. {
  111. $aValidators = array( $aRequest['validator'] );
  112. }
  113. // loop through validators
  114. foreach( $aValidators AS $iKey => $sValidator )
  115. {
  116. if( is_object( $oValidator ) && method_exists( $oValidator, $sValidator ) AND $sValidator != 'FH_CAPTCHA' ) // CAPTCHA can not be validated by AJAX
  117. {
  118. if( $oValidator->$sValidator( $aRequest['value'] ) == false )
  119. {
  120. return "<script type=\"text/javascript\">
  121. <!--//<![CDATA[
  122. jQuery('#".$aRequest['msgbox']."').addClass( 'fh_error' );
  123. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_ok' );
  124. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_mandatory' );
  125. jQuery('#".$aRequest['msgbox']."').prev('input').addClass( 'error' );
  126. //]]>-->
  127. </script>".stripslashes( $aRequest['msg'] );
  128. exit; // stop if one validator fails
  129. }
  130. elseif( empty( $aRequest['value'] ) )
  131. {
  132. return "<script type=\"text/javascript\">
  133. <!--//<![CDATA[
  134. jQuery('#".$aRequest['msgbox']."').prev('input').removeClass( 'error' );
  135. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_error' );
  136. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_ok' );
  137. jQuery('#".$aRequest['msgbox']."').addClass( 'fh_mandatory' );
  138. //]]>-->
  139. </script>";
  140. exit;
  141. }
  142. else
  143. {
  144. return "
  145. <script type=\"text/javascript\">
  146. <!--//<![CDATA[
  147. jQuery('#".$aRequest['msgbox']."').html('&nbsp;');
  148. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_mandatory' );
  149. jQuery('#".$aRequest['msgbox']."').removeClass( 'fh_error' );
  150. jQuery('#".$aRequest['msgbox']."').addClass( 'fh_ok' );
  151. jQuery('#".$aRequest['msgbox']."').prev('input').removeClass( 'error' );
  152. //]]>-->
  153. </script>";
  154. exit;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. ?>