Fazal Rasel
Fazal Rasel

Reputation: 4526

Blade Form without action or url

How to generate a form by Blade template engine without generating action attribute? I would like to generate a form just for ajax request.

Upvotes: 1

Views: 2372

Answers (2)

Developer
Developer

Reputation: 21

1) If you dont want the form to submit

{{ Form::open(['onsubmit' => 'return false']) }}

2) If you have a ajax function, you can call it like so

{{ Form::open(['onsubmit' => 'yourAjaxFunction(); return false']) }}

3) If you want to include Angular JS directive to the form

{{ Form::open(['ng-submit' => 'submit()', 'onsubmit' => 'return false']) }}

Upvotes: 2

Quasdunk
Quasdunk

Reputation: 15220

No, it's actually not possible to tell the Form builder to omit the action attribute. Some attributes will be set in any case, and the action-attribute is one of them. Here's the relevant part from the function:

public function open(array $options = array())
{
    //....

    $attributes['method'] = $this->getMethod($method);
    $attributes['action'] = $this->getAction($options);
    $attributes['accept-charset'] = 'UTF-8';

    //....

    return '<form'.$attributes.'>'.$append;
}

Source: https://github.com/illuminate/html/blob/master/FormBuilder.php#L104

But you can easily overwrite it by just passing in a 'url':

Form::open(['url' => '#'])

Note: Overwriting the action like Form::open(['action' => '#']) would throw an error because this specifies the name of a route. url specifies the raw url.

Upvotes: 2

Related Questions