Skip to content

Instantly share code, notes, and snippets.

@cydh
Last active December 1, 2022 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cydh/52c6b03ff816297e1fe1d944855e6c05 to your computer and use it in GitHub Desktop.
Save cydh/52c6b03ff816297e1fe1d944855e6c05 to your computer and use it in GitHub Desktop.
Quick guide to add Respect/Validation to FluxCP

Adding Respect/Validation

The simpliest way to add Respect/Validation into FluxCP

  1. Create new direcotry in FLUX_ROOT/lib/ with name respectvalidation
  2. Followed its installation guide by using command line in respectvalidation directory
composer require respect/validation
  1. In any module file you want to add the input validation (example: modules/character/view.php) add the respect/validation autoload
require 'respectvalidation/vendor/autoload.php';

use Respect\Validation\Validator;
use Respect\Validation\Exceptions\NestedValidationException;
  1. As example in this char view module, before $charID = $params->get('id'); add the validation check (wrap it with try { }
try {
    $charID = $params->get('id');
    Validator::numeric()->length(6,7)->setName('Char ID')->assert($charID);
    
/**
 * Put original view.php after $charID = $params->get('id'); line into this 'try' bracket
 **/
 $col  = "ch.char_id, ch.account_id, ch.char_num, ch.name AS char_name, ch.class AS char_class, ch.base_level AS char_base_level, ";
 // ...
 // ...
 	$itemAttributes = Flux::config('Attributes')->toArray();
}

} catch (NestedValidationException $exception) {
    $errorMessage = $exception->getFullMessage();
    $char = null;
}
  1. Then edit the theme file for char view themes/default/character/view.php. Edit to
<?php if (!defined('FLUX_ROOT')) exit; ?>
<h2>Viewing Character</h2>
<?php if (isset($errorMessage)): ?>
<p class="red"><?php echo htmlspecialchars($errorMessage) ?></p>
<?php endif ?>

This screenshot below when I set the validation rule as Validator::numeric()->length->(8,10)->setName('Char ID'); result

Styling Error Message

As the exception message from NestedValidationException class may throws multiple lines for a single validation while getFullMessage() returns the multiple line in markdown style and getMessages() is in array! Here's a quick styling guide.

  1. In module file, in catch block, change
$errorMessage = $exception->getFullMessage();

to

$errorMessage = "<ul class='red validation-error'>";
foreach ($exception->getMessages() as $i => $msg) {
	$errorMessage .= "<li>- ".$msg."</li>";
}
$errorMessage .= "</ul>";
  1. Then because <p> never be friendly with <ul> or <ol> and htmlspecialchars() will remove the HTML chars also you can restyle it later instead recreate the error message as plain text with newline tag such <br />, edit on view file
<p class="red"><?php echo htmlspecialchars($errorMessage) ?></p>

to

<?php if (isset($errorMessage)): ?>
<?php echo $errorMessage ?>
<?php endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment