How to react to changes in SimpleForm Inputs? - redux-form

I'm making a custom input component that needs to change when other input values are changed in the form.
<ReferenceInput source="foo">
<SelectInput />
</ReferenceInput>
<NumberInput onChange={(e, newValue) => {this.setState({ newValue })} />
<CustomInput numberValue={this.state.newValue} />
When NumberInput changes, I can pass CustomInput the new value. But ReferenceInput / SelectInput doesn't have an onChange. How do I get the new value of SelectInput to the CustomInput? (Note that inside CustomInput, props.record.foo doesn't update when ReferenceInput changes.)

Check out aor dependent input
https://github.com/marmelab/aor-dependent-input

Related

Formik - Is there a way to get fresh values object on Form change?

I have a Formik form with onChange handler:
<Formik initialValues={values}>
{({ values }) => (
<Form onChange={() => passUpdatedValues(values)}>
// ...
</Form>
)}
</Formik>
values is an object with multiple properties, and Form's inputs are responsible for each of those properties. Parent component of this one uses those values to compute other stuff, and I want to pass the whole updated values up there on each field change.
.
With current approach it will pass an outdated obviously (the values right before onChange event fired).
I can manually create an object to pass, taking e.target.value, but it's kind of messy and grows the code base a lot, especially when there are many fields in the form.
My question is if there is a simple way to get updated values after onChange event, from that handler?
This is the solution I've found so far, looks like there are no any updatedValues prop in Formik.
<Form
onChange={e => {
values[e.target.name] = e.target.value
passUpdatedValues(values)
}}
>

Admin-on-rest: Input validation based on field value

In a SimpleForm of an Edit view I would like to set the minimum value of the group size based on the current number of members inside.
The entity is:
group = {members: 3, size: 10}
What I'm trying to achieve looks like:
export const UserCreate = (props) => (
<Edit {...props}>
<SimpleForm>
<DisabledInput label="Members number"
source="members" />
<NumberInput label="Group size"
source="size"
validation={{min: members, required: true}} />
</SimpleForm>
</Edit>
);
This is explained in the documentation, in the Per Field Validation: Function Validator part.
In a nutshell, you define a function which will take 2 parameters: the value to validate and values which contains all the form values

Update a value on redux form

I have a react-bootstrap class of a form. The code is something like that:
class MyReactClass extends Component {
// ....
render() {
return (
<div>
<Field name="combobox1" component={ComboBox} options={options} ></Field>
<Field name="textField2" component={My_TextField}></Field>
<Field name="textField3" component={My_TextField}></Field>
<Field name="textField4" component={My_TextField}></Field>
</div>
}
}
export default reduxForm({
form: 'myReactClass ',
}
)(MyReactClass );
I added an initialization to initialize some values:
componentDidMount() {
this.props.initialize({textField2:'blabla'});
}
Now I want that when the user selects a value from the combobox, it'll automatically change the value of textfield3. I have a WORKING onChange function for the combobox, but I can't find a way to change textfield3's value. I tried to use "initialize" again -
this.props.initialize({textField3: this.getUpdatedValue()});
but it initialize the whole form and the form gets cleared and have only the value for textfield3.
I tried to access this.props.textField3 but I got undefined.
How can I update the form's value?
this.props.change('textField3', this.getUpdatedValue()) would work.
Optionally, if you need the user to understand that the value was autofilled but can be manually changed, you can use:
this.props.autofill('textField3', this.getUpdatedValue())
These are described in the docs here.

Alfresco Share forms: how to define a custom validation-handler for a non-mandatory field?

I have already read the Alfresco documentation as well as the wiki, but still it does not get clear: what's the proper way to define a custom validation-handler for a certain field in an Alfresco Share form?
The problem is that the approach that I found everywhere in the forum always overrides the MANDATORY type, so the validation handler only gets called if the field is set to mandatory="true".
<config evaluator="aspect" condition="my:aspect">
<forms>
<form>
<field-visibility>
<show id="my:property" />
</field-visibility>
<appearance>
<field id="my:property" mandatory="true">
<constraint-handlers>
<constraint type="MANDATORY" validation-handler="Alfresco.forms.validation.myValidator" event="keyup" />
</constraint-handlers>
</field>
</appearance>
</form>
</forms>
</config>
But how to define a custom validation-handler for a field that is not mandatory? Or else, which is the correct type to choose/override? I tried type="LENGTH" instead, but this also does not get called when the user enters something in the form field and the field.
Update:
I have also tried Andreas' suggestion from the comment below, however, the validation-handler does not get called. This is my xml and function:
<field id="my:field" mandatory="false">
<constraint-handlers>
<constraint message-id="mandatory.field.empty.suffix" event="keyup,propertychange" validation-handler="Alfresco.forms.validation.metahead"/>
</constraint-handlers>
</field>
Validation handler function:
Alfresco.forms.validation.metahead = function FormEditMetadataWebWorkflowExt_metahead(field, args, event, form, silent, message)
{
console.log('metahead!');
return false;
}
The above validation function does not get called at any time, neither when I change the value of my field nor any other form field.
Next, I tried to register the validation handler manually as I read in the Alfresco forum somewhere:
YAHOO.Bubbling.fire("registerValidationHandler",{fieldId: 'fieldIdOfMyField',handler: Alfresco.forms.validation.metahead, when: "onkeyup"});
but this only works partly. It does call my validation handler function only when any other (mandatory) form field changes (which is normal since the entire form validation happens when a field changes), but not when I change the value of my own field ("my:field").
(And also, why would the validation handler need to be registered manually; I would think this is what the constraint configuration in xml is for.)
Just omit the type attribute, so it reads:
<constraint validation-handler="YourCompany.forms.validation.yourValidator" event="keyup" />
If your function
YourCompany.forms.validation.yourValidator = function (field, args, event, form, silent, message)
{...}
returns false, the message displayed resolves to the value of YourCompany.forms.validation.yourValidator.message from the message resources (default mapping).

ASP.NET MVC 3 Razor view returning multiple values for a Html.CheckBoxFor

I have a View that contains the following Line of code:
//(DaysOfWeek is a bool[])
#Html.CheckBoxFor(m => m.Data.DaysOfWeek[0])
It starts off as false. When the user "checks" the box and returns, it returns a value for both true and false;
Here is what is being passed back as part of the form data
Data.DaysOfWeek[0]:true
Data.DaysOfWeek[0]:false
Why is it doing that?
This is because standard HTML checkboxes return no value if unchecked. To make this annoying behaviour more intuitive, the CheckBoxFor method creates a checkbox and a hidden control with the same name, with a value of false, something like this:
<input type="checkbox" name="myControl" value="True" /> My control
<input type="hidden" name="myControl" value="False" />
What you will see when the form is posted is either:
False // checkbox unchecked
True,False // checkbox was checked
Therefore, to test if the box was checked you should use Contains('True'):
bool checkboxChecked = formCollection["myControl"].Contains("True");

Resources