我正在渲染下面的簡單形式使用redux形式,它的工作很好。現在,我希望在另外一種情況下禁用提交按鈕:如果任何Field
的錯誤(即設置了meta.error
)。redux-form:如果至少有一個字段無效,如何禁用提交按鈕?
從lokking進入文檔,我想周圍的<form>
不可能知道它的<Field>
組件是否有錯誤。也許任何人有一個想法,如何解決它一樣容易使用disabled={hasErrors || submitting || pristine}
const EditBlogEntryForm = ({ onSubmit, reset, handleSubmit,
pristine, submitting, ...rest }) => {
console.log('rest: ', rest);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="form-group">
<Field name="title"
type="text"
component={renderField}
label="Titel"
className="form-control"
placeholder="Titel eingeben..." />
</div>
<div className="form-group">
<Field name="text"
component={renderTextArea}
label="Text"
className="form-control"
placeholder="Textinhalt eingeben..." />
</div>
<div className="form-group">
<Field name="image"
type="text"
component={renderField}
label="Bild-URL:"
className="form-control"
placeholder="Bildadresse eingeben..." />
</div>
<div>
<button type="submit" className="btn btn-default"
disabled={submitting || pristine}>
Blogeintrag speichern
</button>
<button type="button" className="btn btn-default"
disabled={pristine || submitting}
onClick={reset}>
Formular leeren
</button>
</div>
</form>
);
};
你能做的僅僅是添加您自己的變量,並把它放在什麼狀態像'錯誤'。一旦這個值爲false,那麼你可以按照@ masoud-soroush提到的提交按鈕 – Alastair