Skip to content

Commit

Permalink
adds validation rules in Ubiquity JsUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheron committed Nov 1, 2022
1 parent 187fb81 commit 8fc168b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Ajax/php/ubiquity/JsUtils.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Ajax\php\ubiquity;

use Ajax\semantic\components\validation\Rule;
use Ubiquity\controllers\Startup;
use Ubiquity\utils\http\URequest;
use Ubiquity\security\csp\ContentSecurityManager;
use Ubiquity\utils\http\UResponse;

class JsUtils extends \Ajax\JsUtils {

Expand Down Expand Up @@ -145,4 +147,32 @@ public static function diBootstrap($controller, $options = [
$jquery->setAjaxLoader("<div class=\"d-flex justify-content-center\"><div class=\"spinner-border\" role=\"status\"><span class=\"sr-only\">Loading...</span></div></div>");
return $jquery;
}

/**
* Adds a new semantic validation ajax rule.
* To use with @checkValidationRule in controllers.
* @param string $ruleName
* @param string $checkUrl
* @param string $params
* @param string $method
* @return void
*/
public function ajaxValidationRule(string $ruleName,string $checkUrl,string $params='{_value:value}',string $method='post'): void {
$this->exec(Rule::ajax($this, $ruleName, $checkUrl, $params, 'result=data.result;', $method), true);
}

/**
* Checks a validation rule declared with @ajaxValidationRule.
* @param $callback a callback using the value to validate : function($value){}
* @return void
*/
public function checkValidationRule($callback): void {
if (URequest::isPost()) {
$result = [];
UResponse::asJSON();
$value = $_POST['_value'];
$result['result'] = $callback($value);
echo \json_encode($result);
}
}
}

0 comments on commit 8fc168b

Please sign in to comment.