diff --git a/system/Config/View.php b/system/Config/View.php index 6f3697f647a2..db06efb4a288 100644 --- a/system/Config/View.php +++ b/system/Config/View.php @@ -60,11 +60,12 @@ class View extends BaseConfig 'upper' => '\CodeIgniter\View\Filters::upper', ]; protected $corePlugins = [ - 'current_url' => '\CodeIgniter\View\Plugins::currentURL', - 'previous_url' => '\CodeIgniter\View\Plugins::previousURL', + 'current_url' => '\CodeIgniter\View\Plugins::currentURL', + 'previous_url' => '\CodeIgniter\View\Plugins::previousURL', 'mailto' => '\CodeIgniter\View\Plugins::mailto', - 'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto', + 'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto', 'lang' => '\CodeIgniter\View\Plugins::lang', + 'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors', ]; public function __construct() diff --git a/system/View/Plugins.php b/system/View/Plugins.php index ea82216d2d4b..cda34b8315e4 100644 --- a/system/View/Plugins.php +++ b/system/View/Plugins.php @@ -121,5 +121,25 @@ public static function lang(array $params = []) return lang($line, $params); } + + /** + * @param array $params + * + * @return string + */ + public static function ValidationErrors(array $params = []) + { + + $validator = \config\services::validation(); + if(empty($params)) + { + return $validator->listErrors(); + } + + return $validator->showError($params['field']); + + + + } } diff --git a/tests/system/View/ParserPluginTest.php b/tests/system/View/ParserPluginTest.php index 8ce8b1f3fc86..d57d00568ce0 100644 --- a/tests/system/View/ParserPluginTest.php +++ b/tests/system/View/ParserPluginTest.php @@ -5,12 +5,14 @@ class ParserPluginTest extends \CIUnitTestCase { protected $parser; + protected $validator; public function setUp() { parent::setUp(); $this->parser = \Config\Services::parser(); + $this->validator = \Config\Services::validation(); } public function testCurrentURL() @@ -55,4 +57,24 @@ public function testLang() $this->assertEquals('TB', $this->parser->renderString($template)); } + public function testValidationErrors() + { + + $this->validator->setError("email","Invalid email address"); + + $template = '{+ validation_errors field=email +}'; + + $this->assertEquals($this->validator->showError('email'), $this->parser->renderString($template)); + } + + public function testValidationErrorsList() + { + + $this->validator->setError("email","Invalid email address"); + $this->validator->setError("username","User name must be unique"); + $template = '{+ validation_errors +}'; + + $this->assertEquals($this->validator->listErrors(), $this->parser->renderString($template)); + } + } diff --git a/user_guide_src/source/general/view_parser.rst b/user_guide_src/source/general/view_parser.rst index d663f942bee0..9f4c063504da 100644 --- a/user_guide_src/source/general/view_parser.rst +++ b/user_guide_src/source/general/view_parser.rst @@ -459,15 +459,16 @@ Provided Plugins The following plugins are available when using the parser: -==================== ========================== =================================================================== ================================= -Plugin Arguments Description Example -==================== ========================== =================================================================== ================================= -current_url Alias for the current_url helper function. {+ current_url +} -previous_url Alias for the previous_url helper function. {+ previous_url +} -mailto email, title, attributes Alias for the mailto helper function. {+ mailto email=foo@example.com title="Stranger Things" +} -safe_mailto email, title, attributes Alias for the safe_mailto helper function. {+ safe_mailto email=foo@example.com title="Stranger Things" +} -lang language string Alias for the lang helper function. {+ lang number.terabyteAbbr +} -==================== ========================== =================================================================== ================================= +==================== ========================== ================================================================================== ================================================================ +Plugin Arguments Description Example +==================== ========================== ================================================================================== ================================================================ +current_url Alias for the current_url helper function. {+ current_url +} +previous_url Alias for the previous_url helper function. {+ previous_url +} +mailto email, title, attributes Alias for the mailto helper function. {+ mailto email=foo@example.com title="Stranger Things" +} +safe_mailto email, title, attributes Alias for the safe_mailto helper function. {+ safe_mailto email=foo@example.com title="Stranger Things" +} +lang language string Alias for the lang helper function. {+ lang number.terabyteAbbr +} +validation_errors fieldname(optional) Returns either error string for the field (if specified) or all validation errors. {+ validation_errors +} , {+ validation_errors field="email" +} +==================== ========================== ================================================================================== ================================================================ Registering a Plugin