用户输入时验证redux表单输入字段

时间:2018-07-05 12:01:43

标签: javascript reactjs validation redux redux-form

当用户输入值时,是否有任何方法可以验证redux表单中的输入字段?当我单击或按下“提交”按钮时,我所有的验证都有效。我希望我的验证在用户键入时运行。我在该字段的代码是这样的:

$allRequests = [];

while ( !checkIfNeedToEnd() ) {
    $newItems = getItemsFromQueue();
    $allRequests = $allRequests + spawnRequests($newItems);
    GuzzleHttp::processWhatYouCan($allRequests);
    removeProcessedRequests($allRequests);
}

1 个答案:

答案 0 :(得分:0)

您可以从元数据(元键下的props)检查字段是否为active,如果其值为true,则显示错误。

const renderField = ({input, label, type, meta: {active, touched, error, warning}}) => (
    <div>
        <label>{label}</label>
        <div>
            <input
                {...input}
                placeholder={label}
                type={type}
                onFocus={input.onFocus}
            />
            {(active || touched) &&
            ((error && <span>{error}</span>) ||
                (warning && <span>{warning}</span>))}
        </div>
    </div>
);

注意:仅当您将onFocus传递到输入元素时,该方法才有效。

  

Codesandbox demo