如何在不同的动作上向redux-form字段添加不同的错误

时间:2017-09-26 14:17:36

标签: reactjs redux redux-form

我的redux表单有两个动作,一个是onclick我只需要验证一个字段,在第二个动作中我需要提交动作。我怎样才能实现这个目标

# create a server
# join server
# move Player
# communicate to server that player moved
# update players position for server?

require 'gosu'
require 'socket'

class Player
  def initialize
    @image = Gosu::Image.new("media/starfighter.bmp")
    @x = @y = @vel_x = @vel_y = @angle = 0.0
    @score = 0
  end

  def warp(x, y)
    @x, @y = x, y
  end

  def turn_left
    @angle -= 4.5
  end

  def turn_right
    @angle += 4.5
  end

  def accelerate
    @vel_x += Gosu.offset_x(@angle, 0.5)
    @vel_y += Gosu.offset_y(@angle, 0.5)
  end

  def move
    @x += @vel_x
    @y += @vel_y
    @x %= 640
    @y %= 480

    @vel_x *= 0.95
    @vel_y *= 0.95
  end

  def draw
    @image.draw_rot(@x, @y, 1, @angle)
  end
end

class Client

  def initialize(host, port)
    @client = TCPSocket.open(host, port)
    send_data("321|123")
  end

  def send_data(data)
    @client.write(data)
  end
  # puts "enter your name:"
  # client.write gets
  #
  # while line = client.gets
  #   puts line.chop
  # end
end

class Tutorial < Gosu::Window
  def initialize(server, port)
    super 640, 480
    self.caption = "Tutorial Game"

    @client = Client.new(server, port)

    @background_image = Gosu::Image.new("media/space.png", :tileable => true)

    @player = Player.new
    @player.warp(320, 240)
  end

  def update
    if Gosu.button_down? Gosu::KB_LEFT or Gosu::button_down? Gosu::GP_LEFT
      @player.turn_left
      @client.send_data("left")
    end
    if Gosu.button_down? Gosu::KB_RIGHT or Gosu::button_down? Gosu::GP_RIGHT
      @player.turn_right
    end
    if Gosu.button_down? Gosu::KB_UP or Gosu::button_down? Gosu::GP_BUTTON_0
      @player.accelerate
    end
    @player.move
  end

  def draw
    @player.draw
    @background_image.draw(0, 0, 0)
  end

  def button_down(id)
    if id == Gosu::KB_ESCAPE
      close
    else
      super
    end
  end
end

Tutorial.new('localhost', 2000).show

}

1 个答案:

答案 0 :(得分:0)

要知道,我有3种不同形式。

function submitForms() {
  dispatch(submit('FirstOne'))  // First form
  dispatch(submit('secondOne')) // Second form
  dispatch(submit('thirdOne'))  // Thrid form
}
<Button type="button" onClick={ submitForms } />

使用此功能,您可以向所有表单触发提交功能。

您可以将每个提交实施为分派多项操作

const mapstateToProps = (state,ownProps) => {
  {... do some stuff ... }

return {
  submit : (values, dispatch, props) => {
         dispatch(YOUR ACTIONS) // So to validate one field ?
         dispatch(YOUR ACTIONS) // dispatch your second actions.
    }
 }


export default Form = connect(mapStateToProps)(reduxForm({
  form:'YoURFORMNAME'
})(YoURFORMCOMP))

我可以补充一点,如果您只想验证表单中的少数字段,可以创建一个简单的验证

const required = value => (value ? undefined : 'Required')

并将其添加到您的字段

<Field {...props}
    validate={required}
</Field>