Q:

Implementing standard PHP form validation within a Slim application

So I’m trying to implement php server side validation for a registration form.  I’m using the slim framework along with twig templates to display the html.  I’ve been trying to use this example to achieve this but I think the way I have set up my slim routing is interfering with error messages displaying properly on the registration form. 
The way my application works currently is that a user navigates to the registration form page which presents the registration form.  This is is shown here:
$app->get('/register', function(Request $request, Response $response)
{
    return $this->view->render($response,
        'register.html.twig',
        [
            'css_path' => CSS_PATH,
            'landing_page' => $_SERVER["SCRIPT_NAME"],
            'action_register' =>  './register/success',
            //'initial_input_box_value' => null,
            'page_title' => 'Login App - Register',
        ]);
})->setName('register');

The 'action_register' =>  './register/success', represents the form ‘action’ attribute in the html twig form and indicates where and how the form data is processed and the response to the submitted form.  This can be seen here: (note that the first few lines of the register/success page is the example used from the guide i’ve been using for php validation)
$app->post('/register/success', function(Request $request, Response $response) use ($app)
{
    $nameError = " "; 

    if(isset($_POST['submit']))
    {
        if (empty($_POST["username"])) {
            $nameError = "Name is required";
        } else {
            $name = test_input($_POST["username"]);
// check name only contains letters and whitespace
            if (! preg_match("/^[a-zA-Z ]*$/",$name)) {
                $nameError = "Only letters and white space allowed";
            }
        }
    }

    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $arr_tainted_params = $request->getParsedBody();

    $sanitizer_validator = $this->get('validate_sanitize');
    $password_hasher = $this->get('hash_password');

    $tainted_email = $arr_tainted_params['email'];
    $tainted_username = $arr_tainted_params['username'];
    $tainted_password = $arr_tainted_params['password'];

    $model = $this->get('model');
    $sql_wrapper = $this->get('sql_wrapper');
    $sql_queries = $this->get('sql_queries');
    $db_handle = $this->get('dbase');

    $cleaned_email = $sanitizer_validator->validate_email($tainted_email);
    $cleaned_username = $sanitizer_validator->validate_username($tainted_username);
    $cleaned_password  = $sanitizer_validator->validate_password($tainted_password);
    $hashed_cleaned_password = $password_hasher->hash_password($cleaned_password);

    $model->set_user_values($cleaned_username, $cleaned_email, $hashed_cleaned_password);
    $model->set_sql_wrapper($sql_wrapper);
    $model->set_sql_queries($sql_queries);
    $model->set_db_handle($db_handle);

    if ($sanitizer_validator->get_validate_messages() == ' ')
    {
        $model->store_user_details();

        $_SESSION["loggedin"] = true;
        $_SESSION["username"] = $cleaned_username;

        $arr_storage_result_message = '';

        echo $sanitizer_validator->get_validate_messages(); //this will be turned into a proper alert prompt at a later date
    }

    if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true)
    {
        return $this->view->render($response,
            'display_user.html.twig',
            [
                'css_path' => CSS_PATH,
                'landing_page' => $_SERVER["SCRIPT_NAME"],
                //'action_register' => 'index.php/register',
                //'initial_input_box_value' => null,
                'page_title' => 'Login App - Display',
                'username' => $cleaned_username,
                'hashed_password' => $cleaned_password,
            ]);
    }
});

The idea is that the user creates an account and is then logged in whereby the   'display_user.html.twig', is displayed.  Otherwise error messages regarding form validation should be displayed on the registration form itself.  However the registration form is only displayed on the /register page.  But the registration form posts the data and processes it on the registration/success page. 
So I’m not sure if the php validation guide code is in the wrong place or that my slim routing is incorrect for this purpose of form validation.  I think the problem lies in the fact that when the form is submitted it redirects trhe user to a new page namely register/success. 
In summary i’m trying to implement php form validation such as this within a slim application using twig templates.  
I’ll also include the register.html.twig page of the registration form if that helps:
{% extends 'header_footer.html.twig'%}
{% block content %}
    <h3>Register A New Account</h3>

    <form method = "post" action = " {{ action_register }} ">
        <p>Email: <input type="text" name="email" ><br></p>

        <p>Username: <input type="text" name="username" ><br></p>
        <span class="error">* <? php echo $nameError;?></span> //code used in the php validation guide

        <p>Password: <input type="text" name="password" ><br></p>
        <! --<p>Password Confirm: <input type="text" name="password_confirm"><br></p>  THIS WILL BE IMPLEMENTED LATER-->
        <input type="submit" value="Create Account">
    </form>
{% endblock %}

A:

You can't use PHP in Twig like that. 
Take a look at these:

Slim-Flash
slim-twig-flash
Slim-Validation

With Flash and Slim-Validation you can achieve your desired error messages. 
If you don't want to use the above components then you could simply pass your $nameError variable to the template: 
PHP:
return $this->view->render($response,
  'display_user.html.twig',
  [
    'nameError' => $nameError, // HERE
    'css_path' => CSS_PATH,
    'landing_page' => $_SERVER["SCRIPT_NAME"],
    //'action_register' => 'index.php/register',
    //'initial_input_box_value' => null,
    'page_title' => 'Login App - Display',
    'username' => $cleaned_username,
    'hashed_password' => $cleaned_password,
  ]
);

Twig:
{% if nameError %}<span class="error">* {{ nameError }}</span>{% endif %}

