How to make validation on TextField on button click
Hello. I have very simple scenario: I want to validate text field if has value when someone click on button.
export default class SpfxReactControls extends React.Component<ISpfxReactControlsProps, any> {
constructor(props: ISpfxReactControlsProps) { super(props); this._getErrorMessage = this._getErrorMessage.bind(this);
this.state = {
titleValue: ""
};
}
public render(): React.ReactElement
const { titleValue } = this.state;
return (
<div>
<div className="docs-TextFieldExample">
<TextField id='titleField' value={titleValue} required={true} label={escape(this.props.description)} **onGetErrorMessage={this._getErrorMessage} validateOnLoad={false}** />
</div>
<br />
<div className='ms-BasicButtonsExample'>
<Button
data-automation-id='test'>Normal button</Button>
<Button
data-automation-id='test'
buttonType={ButtonType.primary} onClick={this._buttonOnClickHandler.bind(this)}>Primary button</Button>
</div>
</div>
);
}
private _getErrorMessage(value: string): string {
return value === "" ? The length of the input value should less than 3, actual is ${value.length}. : "";
}
private _buttonOnClickHandler() {
**this._getErrorMessage = this._getErrorMessage.bind(this);**
alert(this.state.titleValue);
return false;
} }
This validation start to work when someone start to typing text to textfield and delete all chars. But The problem is I don't know how call validation this._getErrorMessage when someone click on button. Thank you
It doesn't sound like you're reporting an issue against OUFR. We kindly request that all questions be directed to StackOverflow. We'd like to keep issues limited to feature requests and bugs. Thank you!
If you are reporting an issue, please fill out our template including a CodePen.
I am sorry but this was a simple question....and I think this is a bug because this is not possible to do....So you are not able to use errorMessage property if you want to make validation onclick....
Could you make a CodePen from your example? That will make it easier to review as a bug vs. feature. Thanks!
Hello. I have prepared stackowerflow:
https://stackoverflow.com/questions/54721721/how-to-set-errormessage-on-textfield-dynamically-on-button-onclick-method
Thanks
It looks like you already have a couple of answers on Stack Overflow. I'll go ahead and treat this as a non-issue for now, but please feel free to respond if you feel there is an underlying issue that needs to be solved.
This issue has been automatically marked as stale because it has marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your contributions to Fabric React!