So the first one shows the input form and the second takes its input. The create method validates the input and should redirect back to the new route on errors.
I am not yet sure how to redirect those error Messages to the new route. As far as I know there’s $response->withRedirect(<TARGET>) but no way to add data.
How do I ideally structure those two methods? Should I just Show the form from within the create method? How do I pass the errors to new (to show them inside the form)?
$cat = new Cat;
$cat->name = $request->getParsedBodyParam('name');
$cat->color = $request->getParsedBodyParam('color');
// in my Cat model, I have a validator method I'm calling in this example, but you could put it here instead
$validator = $cat->getValidator($cat);
if (!$validator->validate()) {
$data [
'cat' => $cat,
'errors' => $validator->errors()
];
return $this->view->render($response, 'cat/create.html.twig', $data); // errors available in view as `errors`
}
// All looks good, save the cat and redirect somewhere...
Thanks for you Reply. So you put post and get in one method? Hmm… I was used to put them in different methods when I used Laravel. That’s why I thought I should Redirect.
OK, I’ll try that Approach. I combined get and post in my routes with $app->map(['GET', 'POST'], '/cats/new', '\CatController:new');
Okay, so your code in your first answer was inside the store (POST) method? So you just show the create.html.twig in your create and store method? Like this:
public function create($request, $response)
{
return $this->view->render($response, 'cat/create.html.twig', $data);
}
public function store($request, $response)
{
// your code from your first answer...
}
Right? I wasn’t sure if you called the template in both methods.
I think so. In my create method I call the create view like you showed. In my store method, if validation fails I return the create view with the (valid) old views as well as the error message. If validation passes I will redirect to various places depending on what is appropriate for the app… sometimes the index view, sometimes the show view, etc.
I now do it the same, but I don’t use /cats for the post route. I think it looks a bit weird if you where at /cats/new and suddenly you’re at ``/cats`.
I have still a problem , how can I redirect to same page if validation failed. I am using a main.php which have two sections for login and for signup. I have used pagination for moving 1 to 2 or 2 to 1 section. In this case how can I redirect to same page if validation failed. I have used like this for log in :
But I have single page with two section , how i redirect to same page same section of page if validation failed. it means I am using main.php which includes one section for login and other for signup , now i want to redirect the page to login if login validation failed . Could you please send me sample of files and structure.
You can route to different methods within a class in a couple of different ways, consult the Container Resolution section of the documentation for full info, but here are some examples.
$app->get('/', '\HomeController:home');
// routes to the home method of the HomeController class
$app->get('/', \HomeController::class . ':home');
// same as above, but in a different format
So in your example it might look something like this
$app->get('/', '\MainController:login')->setName('login');
$app->get('/', '\MainController:signup')->setName('signup');
// or maybe you like this better
$app->get('/', \MainController::class . ':login'->setName('login'));
$app->get('/', \MainController::class . ':signup')->setName('signup');
I am using respect/validation in my project , the following fatal error found :
Fatal error: Call to a member function setName() on string in C:\myproject\app\Validation\Validator.php on line 29
with codes
<?php
namespace App\Validation;
use Respect\Validation\Validator as Respect;
use Respect\Validation\Exceptions\NestedValidationException;
class Validator
{
protected $errors;
public function validate($request, array $rules)
{
foreach ($rules as $field => $rule)
{
try {
$rule->setName(ucfirst($field))->assert($request->getParam($field));
} catch(NestedValidationException $e){
$this->errors[$field] = $e->getMessages();
}
}
When I try to remove setName(), it display same error for assert(), how can I Solve this . what happen in line 29? Please, have any solution ?
Thank you .