framework icon indicating copy to clipboard operation
framework copied to clipboard

POST request with data gives "No route defined"

Open Crinsane opened this issue 12 years ago • 7 comments

I'm trying to create a contact page with a contact form. The idea is to show the form on the GET request (normal page view) the form will POST to the same route, and there I will handle the form, and redirect back to the page.

So I've set up two routes:

Route::get('page', ['contact', 'uses' => 'ContactController@create']);
Route::post('page', ['contact', 'uses' => 'ContactController@store']);

And I have a controller with those two methods:

public function create()
{
    $errors =  $this->contactForm->getErrors();

    return View::make('pages.contact', compact('errors'));
}

public function store()
{
    if( ! $this->contactForm->validateForm(Input::all()))
    {
        $_SESSION['validation_errors'] = $this->contactForm->getErrors()->toArray();

        wp_redirect(get_permalink() . '#contact-form');
        exit;
    }

    $this->contactForm->send(
        $this->contactForm->getData()
    );

    wp_redirect(get_permalink() . '#contact-form');
    exit;
}

But now I run into this problem. When I do a POST of the form without any form data (all fields empty), the store() method fires, validation fails and you get redirected. But as soon as I fill in anything in the form, I get a "Page not found" error and it shows the default "No route defined for this request..." text with green "Back home" button.

What is going wrong? Am I missing something or doing something wrong, or is it a bug?

Crinsane avatar Nov 12 '14 09:11 Crinsane

Hi,

you're using the get_permalink without any post ID as parameter. As we're not in a defined WordPress loop, I will suggest you to pass in the page ID as a first argument to your get_permalink function.

The global $post variable is passed to all route callbacks. So your store method contains the current post as a parameter. Try this code and let me know if it fixes your issue:

// Global $post variable as 1st parameter
public function store($post)
{
    if( ! $this->contactForm->validateForm(Input::all()))
    {
        $_SESSION['validation_errors'] = $this->contactForm->getErrors()->toArray();

        wp_redirect(get_permalink($post->ID) . '#contact-form');
        exit;
    }

    $this->contactForm->send(
        $this->contactForm->getData()
    );

    wp_redirect(get_permalink($post->ID) . '#contact-form');
    exit;
}

Look at the $post->ID code added in the get_permalink functions.

jlambe avatar Nov 12 '14 09:11 jlambe

Thanks for the response.

I actually thought about it, but just to be sure tested it again. But I can even dumb this problem down. If I remove all code and only use this:

public function store()
{
    td('test');
}

Doing a POST with no form data shows "test", doing a POST with form data gives the "Page not found" error.

So sadly, I don't think that's the problem. The app is never even reaching the store() method.

Crinsane avatar Nov 12 '14 09:11 Crinsane

I ran a new installation with a simple contact page and form. Using your code, both methods of your ContactController are called.

The routes.php is returning the first match in the file beginning at the top through the bottom of the file. Check if you don't have another route conflicting with your Route::post(). For example, if you defined a global route for all pages before the one of your contact page, this global route is used:

// This is matched before your contact page route.
Route::any('page', function()
{
    return 'All pages route.';
});

// This route is never triggered
Route::post('page', ['contact', 'uses' => 'ContactController@store']);

Let me know if you fixed your issue.

jlambe avatar Nov 12 '14 10:11 jlambe

I had been trying stuff out myself too, and found out something really strange. It seems like the problem is with a form field with the name "name" <input type="text" name="name">

If I fill anything into that field, I get the "Page not found". Other fields did work. So renaming that field to "user" or something fixes the problem...

Is my installation acting up, or is the same happening on yours?

Crinsane avatar Nov 12 '14 10:11 Crinsane

The same is happening to mine. But it's not a "real issue". WordPress has some reserved query parameters and the name query parameter is used by WordPress to build its query. So you can't use it in any forms. Here is a list of reserved WordPress query vars: http://codex.wordpress.org/WordPress_Query_Vars

This is a WordPress tip, always prefix your name attributes ;)

jlambe avatar Nov 12 '14 10:11 jlambe

Thanks for the tip! My problem is solved!

Keep up the great work! Really love this framework, finally makes Wordpress programming less of a burden.

Crinsane avatar Nov 12 '14 11:11 Crinsane

Could it be possible to see a full validation example? I would love to see that ContactForm.php class. Where are you registering it? Are you adding something like config/forms.config.php ?

santivila avatar Dec 06 '14 14:12 santivila