我正在处理一个页面,该页面上有许多输入验证和逻辑绑定,每次迭代都会增加页面大小。所以,我必须找到一个漂亮且可扩展的解决方案。
想象一下,当用户从下拉列表中选择一个值为“A'”时,必须禁用某些字段,必须清除某些字段,并使用默认值启用某些字段。我可以用一些像
这样的小代码更改一个相关字段(没有像regexp或lenght constrait这样的验证规则)值 this.props.dispatch(change('xForm','xField','xValue' ))
我的问题是,当我需要清除多个字段时,
它总是被我的验证方法阻止,并且清除操作失败(注意:我应该是那样但不是那样)
。
我尝试了一些策略,但是y,z,w字段有一些文本,它触发了验证规则和错误。因此,输入仍然是旧值,而不是清除值。
//Clear
this.props.dispatch(change('xForm','yField','' ))
this.props.dispatch(change('xForm','zField','' ))
this.props.dispatch(change('xForm','wField','' ))
对于具有高度依赖性输入的页面,清除输入或为redux-form的输入分配一些值的最佳做法是什么。
我已经研究了2天,但我找不到任何最佳解决方案。 (redux normalizer,redux form utils等)
感谢。
答案 0 :(得分:15)
这对我有用:
resetAdvancedFilters(){
const fields = ['my','fields','do','reset','properly']
for (var i = 0; i < fields.length; i++) {
this.props.dispatch(change('formName',fields[i],null))
this.props.dispatch(untouch('formName',fields[i]))
}
}
答案 1 :(得分:9)
使用下面的内容,它会清除相应的多个字段,并清除相应字段的错误。
重置多个字段的常用功能。
import {change, untouch} from 'redux-form';
//reset the respective fields's value with the error if any after
resetFields = (formName, fieldsObj) => {
Object.keys(fieldsObj).forEach(fieldKey => {
//reset the field's value
this.props.dispatch(change(formName, fieldKey, fieldsObj[fieldKey]));
//reset the field's error
this.props.dispatch(untouch(formName, fieldKey));
});
}
使用上述常用功能,
this.resetFields('userDetails', {
firstName: '',
lastName: '',
dateOfBirth: ''
});
答案 2 :(得分:3)
plugin()
API更改reducer中的值。它是一个复杂的API,因为你可以完全控制在reducer中搞乱,但是你有一个复杂的要求。
然而,像您说的那样调度三个change()
操作也应该可以正常工作。如果字段中包含您不想显示的untouch()
验证消息,则可能需要/ Required
这些字段。
它总是被我的验证方法阻止,并且清除操作失败。
您能详细说明这意味着什么,显示堆栈跟踪或控制台错误输出吗?
答案 3 :(得分:1)
那里。
让我们看一下http://redux-form.com/6.0.0-alpha.4/docs/faq/HowToClear.md/
对于整个表格,我们可以使用4种方法:
A)您可以使用plugin()API来教会redux-form reducer,以响应提交成功时调度的操作。
B)只需卸载表单组件
即可C)提交成功后,您可以在表单内调用this.props.resetForm()。
D)您可以从任何连接的组件发送reset()
答案 4 :(得分:1)
尝试使用以下有效负载的元对象中的调度函数:
inherit_id
这应该可以解决你想要的任何领域。
答案 5 :(得分:1)
我找到了更好的方法,
我们可以使用RF的initialize动作创建者。
import pygame
import Constants
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()
class player:
def __init__(self,velocity):
self.velocity = velocity
def location(self,x,y):
self.x = x
self.y = y
self.xVel = 0
def keys(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.xVel = - self.velocity
elif event.key == pygame.K_RIGHT:
self.xVel = self.velocity
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self.xVel = 0
def move(self):
self.x = + self.xVel
def draw(self):
display = pygame.display.get_surface()
pygame.draw.rect(window,(255,255,255),[self.x,self.y,20,40])
def do(self):
self.keys()
self.move()
self.draw()
P = player(10)
P.location(Constants.SCREEN_WIDTH/2,0)
#Map
def draw_map():
window.fill(Constants.BLUE)
#sprites
def draw_player():
player = Player(50,50)
all_sprite_list.add(player)
#Game loop
def game_loop():
GameQuit = False
while not GameQuit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameQuit = True
draw_map()
P.do()
pygame.display.update()
clock.tick(30)
#game initialisation
game_loop()
pygame.quit()
quit()
如果keepDirty参数为true,则将保留当前脏字段的值,以避免覆盖用户编辑。
答案 6 :(得分:0)
清除多个字段
import {change} from 'redux-form';
const fields = {name: '', address: ''};
const formName = 'myform';
const resetForm = (fields, form) => {
Object.keys(fields).forEach(field => dispatch(change(form, field, fields[field])));
}
resetForm(fields,formName);
答案 7 :(得分:0)
如果我们正在谈论在表单中输入字段,然后在提交之前进行导航,那么这是最好的解决方案。将其放在您要清除的字段的componentDidMount()中。
kv