bietkul / react-reactive-form

Angular like reactive forms in React.
MIT License
309 stars 32 forks source link

isRequired flag in FormGenerator controls to show asterisk (*) in label #35

Closed arahmanali closed 5 years ago

arahmanali commented 5 years ago

Hi i am using FormGenerator API for my form i want to show the asterisk (*) on label if the control is required but could not find a flag in render args, could anyone please guide?

bietkul commented 5 years ago

You can access the control in your render component, A control has these properties which can be used to render your UI.

I'm not sure but what I understand that you want to show an asterik on label if a required validator is set to the control. You can check this by finding out the required validator in the validators list of a control.

arahmanali commented 5 years ago

@bietkul yeah i found out there was a function validator() in control args but when i call without any param it throws an error evaluating control.value but when i pass empty string it returns an object { required: true }

could you please clarify what argument it expects ?

bietkul commented 5 years ago

Control config has a meta key which accepts an object (Whatever you define here can be accessed in component by using control.meta), I think you can use it to pass a flag to tell if the component is required or not. For e.g

first_name: {
      render: TextInput,
      meta: {
        label: "First Name", // Or solve it by passing a label with asterik like `First Name *`
        placeholder: "Enter first name",
        required: true
     },
     options: {
        validators: Validators.required
     }
}
arahmanali commented 5 years ago

great thanks