Material-ui: Form how to submit?

Created on 12 Jan 2015  路  13Comments  路  Source: mui-org/material-ui

I'm putting Input components in a form like <form action="/something/create" method="POST">...</form>, but there is no button like <RaisedButton type="submit"/>. How are these elements supposed to be used in a form and how do you trigger a submit?

Most helpful comment

My submit button looks like this and works fine (^v0.5.0)

<RaisedButton type="submit" label="login" className="button-submit" primary={true} />

All 13 comments

For me triggers submit fine

<form action="/something/create" method="POST">
        <Input ref="login" type="text" name="login" placeholder="Login" />
        <Input ref="password" type="password" name="password" placeholder="Password" />
        <RaisedButton label="Signin" />
  </form>

@ButuzGOL How is the button in your example linked to the submit action? Is it implicitly always "the" button in the form? What if I add a second button to select an file upload as part of the form?

By default button has type submit
https://developer.mozilla.org/ru/docs/Web/HTML/Element/button

submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

So if you want to prevent submit event you should add type="button"

My submit button looks like this and works fine (^v0.5.0)

<RaisedButton type="submit" label="login" className="button-submit" primary={true} />

Thanks guys! Sorry for this basic question that didn't have much to do with MUI. As you noticed I don't have a lot of experience with forms :)

Also make sure that containerElement isn't set, or it will break as well.

@ButuzGOL RaisedButton (and probably the others) default to type="button" now in late 2017 (v0.19.2), not the HTML default of "submit", so you have to set submit explicitly where you need it.

@estaub - I'm using Button but can't get the form to submit. I have to either just use button or Input (but then I lose Button styles).

                <Button
                    type="submit"
                    variant="contained"
                    component="span"
                    disabled={this.haveErrors(errors)}
                >
                    Save
                </Button>

@smokinjoe Sorry, I'm very rusty on all this, can't help. I'd suggest using Chrome DevTools or similar to inspect the generated <button> html, for starters.

@estaub - It might be a little lazy (and I realized I'm using an older version of material-ui) buuuut, this worked for me to be able to submit and have button styles:

                    <Button variant="contained">
                        <Input type="submit" disabled={this.haveErrors(errors)}>
                            Save
                        </Input>
                    </Button>

hey. for the Button component https://material-ui.com/api/button/, the type is button, how to change type to submit?

@xiongemi i think you're right. by default, it looks like the component's type is 'button'. have you tried adding type="submit" to the component's props?

<Button type="submit">Submit</Button>

via: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L260
via: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L296
via: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L376

@xiongemi Hopefully, teaching to fish: https://material-ui.com/api/button-base/ .

When scanning the props of something in the api docs, always check for an Inheritance parent, where more may be found.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mb-copart picture mb-copart  路  3Comments

pola88 picture pola88  路  3Comments

ryanflorence picture ryanflorence  路  3Comments

ericraffin picture ericraffin  路  3Comments

revskill10 picture revskill10  路  3Comments