fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

How to make validation on TextField on button click

Open enti333 opened this issue 7 years ago • 6 comments

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

enti333 avatar Feb 15 '19 21:02 enti333

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.

JasonGore avatar Feb 15 '19 21:02 JasonGore

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....

enti333 avatar Feb 15 '19 22:02 enti333

Could you make a CodePen from your example? That will make it easier to review as a bug vs. feature. Thanks!

JasonGore avatar Feb 15 '19 22:02 JasonGore

Hello. I have prepared stackowerflow:

https://stackoverflow.com/questions/54721721/how-to-set-errormessage-on-textfield-dynamically-on-button-onclick-method

Thanks

enti333 avatar Feb 16 '19 09:02 enti333

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.

JasonGore avatar Feb 19 '19 16:02 JasonGore

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!

msft-github-bot avatar Feb 23 '19 17:02 msft-github-bot