New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set Value for a field via code? #369
Comments
Issue resolved after taking a look at this one: http://erikras.github.io/redux-form/#/faq/custom-component?_k=qnjmi9 |
You can set value for a field by do this: First, in the container component, pass
Then, in
|
@rockallite, thanks, that really helped! I was actually able to get it to work by passing changeFieldValue into bindActionCreators in the more usual redux style - I just imported change from redux-form using the changeFieldValue alias.
|
For future reference, the new way to do this in version 6 is using this.props.change(field_name, value). See the docs. |
Hey, is there a way to change multiple fields? One change event with many fields instead many events with one field. |
this.props.change(field,value) does not work for me. I get a response back that looks alright but the value doesn't change and touch is still false.
returns
My goal is to set the field to undefined if the user wants to set the maxSalary to none |
@StRibbon you ever figure this out? I want to be able to have a button that sets a hidden field value. |
@ericdfields I did! Thank you for following up. |
@StRibbon heh, I originally asked because I wanted a similar solution :) I got something that works though. I wanted a button to set
This worked, but I had an unexpected behavior where the field wouldn't remove itself from view (wasn't getting re-rendered and passing the |
|
@StRibbon Don't mean to bother you, but would be curious to hear how you fed a value of "undefined" into the change action. I'm trying to do the same thing to set a field value, but any time I use "undefined" as the target value, nothing changes. If I set any other value (strings, numbers, etc.) in there it works instantly, but undefined gets me no change. My change statement looks like:
|
@threepears, just like this
you need to send 3 arguments. |
@StRibbon : Unfortunately, that doesn't seem to work for me. I tried both using |
What if you're dealing with sub-components of your form which aren't the HOC itself and thus don't have access to the |
@fcfl you can import these action creators from |
Thanks @timhwang21 o/ |
Hi, is there a way to set initial field value without triggering |
@threepears I think it has to do with this: The function won't set a value if the value is undefined, which makes some sense when you consider that JS function parameters that are not used are undefined. That is a behaviour you might not want. Not to mention that the undefined value is intended only to be used for something that has never been defined (hence the name) and all possible object attributes are undefined. However, it is something that could have been stated in the documentation. |
I tried all the proposed solutions unsuccessfully. It would only change the field values if I refreshed the page, but not when I changed pages through clicking links with React Router. I ended up just hacking the dom in componentDidMount
|
Are there any updates on this? In my app the distinction between "null" and "undefined" is important for validation - some fields will be nullable but we need the user to explicitly "null" out the field to make it valid, and in other circumstances we want to invalidate the field manually by setting it back to undefined through dispatching an action. Using It would be really nice if there was a |
@amized Is there some other value that you can use as "not valid" instead of |
I actually solved the problem with the |
Also struggling with this. Some good examples above but for V6 using this.props.change(), it's not clear how to use this. Where are we even calling this prop? Would like to see a complete example. Working on this problem now, if I find the solution I'll post a complete example. |
Same issue as NathanCH. I would like to invoke props.onChange in response to clicking on a checkBox. How is this done? So far the examples have been unintuitive. |
@degamer106, I actually found this to be an anti-pattern for my use-case. Instead of triggering a change on the form directly, trigger a change in your application state. Your initial values will update to reflect this change assuming your initial values are set from application state. This may mean moving additional state to your store. |
@NathanCH I was able to figure out my use case from the comments in this thread: #1500 To summarize, you need to add the desired form fields as props to your component by calling formValueSelector in your react-redux connect() method:
|
@NickBarry Is there documentation on how to use |
Is there any solution for this issue even for me this doesn't work when I call |
@aqumus your should pass the form name as first argument of change function: |
The For anybody struggling with a similar issue, try setting anything other than |
@aqumus can you help me to change/ mask the value once i tabout/focusout the filed? Example: i need to replace the number entered in the field with ****** after tabout. i tried using this.props.change('fieldname', 'new_value'); it is updating the field value but not reflecting on UI. |
For me, nothing works. I am trying to set the value other than null or undefined, but it doesn't work. |
this is very best but I want to update 4 fields value in such cases, i needed to call this.props.onChange('fieldName', value) for 4 times, resulting 4 time re-rending of the form how I can achieve this in a single action? |
In my experience calling a couple of changes didn't have a big performance impact, but in case you really need it to be one action you can:
P.S. the docs I linked are for version 8, check the docs for the version your using, but if the |
I'm creating a customised component for my form.
What I want: after I click a certain location, in my onClick handler, I can set the corresponding value for the form field.
How to do that?
My current solution:
it doesn't work yet.. but even if it works, I feel this is kind of hack.
Any better solution?
The text was updated successfully, but these errors were encountered: