I am working on latest react-native version.
I'm trying to implement a custom validator to check if the password and password confirm are equal. The problem is that the validator function is not working.
Library Link react-reactive-form
Code
MatchPassword(AC: AbstractControl) {
let password = AC.get('new_password').value; // to get value in input tag
let confirmPassword = AC.get('confirm_password').value; // to get value in input tag
if (password !== confirmPassword) {
AC.get('confirm_password').setErrors({ mismatch: true });
} else {
return null;
}
}
changePasswordForm = FormBuilder.group({
new_password: ['', [Validators.required]],
confirm_password: ['', [Validators.required]],
}, {
validator: this.MatchPassword,
});
render() {
return (
<Fragment>
<FieldGroup
control={this.changePasswordForm}
render={() => {
return (
<Fragment>
<FieldControl
name="new_password"
render={({ handler, touched, hasError, ...props }) => (
<Fragment>
<TextInput {...props} label="New Password" placeholder="Password" handler={handler} type="password" secureTextEntry />
{(touched && hasError('required')) && <ErrorText message="This Field Should Not Be Empty." />}
</Fragment>
)}
/>
<FieldControl
name="confirm_password"
render={({ handler, touched, hasError, ...props }) => (
<Fragment>
<TextInput {...props} label="Confirm Password" placeholder="Confirm Password" handler={handler} type="password" secureTextEntry />
{(touched && hasError('required')) && <ErrorText message="This Field Should Not Be Empty." />}
{(touched && hasError('mismatch')) && <ErrorText message="Password No Match!" />}
</Fragment>
)}
/>
<Button onPress={() => this.handleSubmit()} variant="success" block large style={[mb0, mr15, flex1]}>Submit</Button>
</Fragment>
);
}}
/>
</Fragment>
);
}
Please tell me how to do this?
Actually, you have used the react JS packages to validate the form. React JS packages can't be used for react-native projects. You should check this here
Abstract of your problem:
react library actually does not have anything related to Browser DOM HTML. Anything related to it separated into react-dom package. React Native does not and cannot use this library, because you don't have DOM underneath a react native application. However you can use most of the code/functionality you wrote for your mobile app in the browser
You can validate your form using these packages:
https://www.npmjs.com/package/react-native-form-validator
https://www.npmjs.com/package/react-native-validator-form
https://github.com/jeffraux/react-native-validate-form
Related
I'm using React Native 0.63.4 and my goal is to have auto fill working both on Android and iOS:
get email suggestions (for example, when filling the email input on sign up and when logging in for the first time)
get asked to save the password after successfully logged in
get the saved credentials when logging in again
Here's the component I am using for the inputs:
const InputField: React.FC<Props> = ({
placeholder,
onTextChange,
keyboardType = "default",
securedTextEntry,
isMissing,
textContentType = "none",
autoCompleteType = "off"
}) => {
const [thisSecureTextEntry, setThisSecureTextEntry] = useState(false);
const isAutoCapitalizeDisabled =
keyboardType === 'email-address' || 'password' ? 'none' : 'sentences';
const importantForAutofill = autoCompleteType ? 'yes' : 'no';
const updateThisSecureTextEntry = (value: boolean) => {
if(securedTextEntry) {
setThisSecureTextEntry(value);
}
}
return (
Platform.OS === 'ios' ?
<TextInput
style={styles.text}
placeholder={placeholder}
placeholderTextColor={isMissing ? ERROR_TEXT : PLACEHOLDER_GRAY}
onChangeText={onTextChange}
onFocus={() => updateThisSecureTextEntry(true)}
onBlur={() => updateThisSecureTextEntry(true)}
secureTextEntry={thisSecureTextEntry}
keyboardType={keyboardType}
autoCapitalize={isAutoCapitalizeDisabled}
textContentType={textContentType}
/> :
<TextInput
style={styles.text}
placeholder={placeholder}
placeholderTextColor={isMissing ? ERROR_TEXT : PLACEHOLDER_GRAY}
onChangeText={onTextChange}
onFocus={() => updateThisSecureTextEntry(true)}
onBlur={() => updateThisSecureTextEntry(true)}
secureTextEntry={thisSecureTextEntry}
keyboardType={keyboardType}
autoCapitalize={isAutoCapitalizeDisabled}
autoCompleteType={autoCompleteType}
importantForAutofill={importantForAutofill}
/>
);
};
export default InputField;
And here's how I use it for login:
<InputField
placeholder={translations.EMAIL}
onTextChange={setEmail}
keyboardType={'email-address'}
autoCompleteType={'username'}
textContentType={'username'}
/>
<InputField
placeholder={translations.PASSWORD}
onTextChange={setPassword}
securedTextEntry={true}
autoCompleteType={'password'}
textContentType={'password'}
/>
I had to maintain the state of secureTextEntry and change the value onFocus and onBlur to correctly get email suggestions on iOS (link)
iOS problem:
If I set textContentType as username, I get asked to save the password on iOS but I don't get email suggestions the first time I am filling the login. If I set it to emailAddress, I get the suggestions but then I am not asked to save the password.
Android problem:
Even with autoCompleteType and importantForAutofill set, I am never asked to save the password.
I tried to add a permission to AndroidManifest.xml <uses-permission android:name="android.permission.BIND_AUTOFILL_SERVICE" /> but it made no difference.
I have auto fill enabled on the settings of the two devices (android and ios) that I am using.
Does anyone have a suggestion on how to fix this? Thank you.
Version react hook form
^7.27.0
What I tried to follow and without successful
react hook form - Discussions 7818
react hook form - Issues 230
About what I have
I have 4 text field components at my screen, the name of each text field is name, documentation, email, password and I would like to know how can I setup some configuration that it will be pressed the NEXT button at keyboard and will focus the following text fields?
An example that I have inside at my component file, again I would like to press the next button and the next component that I will config, will be focus.
<TextField
name="name"
label={I18n.t('registerPersonal.fullNameLabel')}
placeholder={I18n.t('registerPersonal.fullNameInput')}
icon={<TypographyIcon fill={!!errors.name && theme.colors.attention} />}
error={errors.name?.message}
errors={errors}
control={control}
returnKeyType="next"
/>
<TextField
name="documentation"
label={I18n.t('registerPersonal.documentIdentificationLabel')}
placeholder={I18n.t('registerPersonal.documentIdentificationInput')}
icon={
<DocumentIcon
fill={!!errors.documentation && theme.colors.attention}
/>
}
error={errors.documentation?.message}
control={control}
returnKeyType="next"
/>
Some properties that I get at my personal hook
const {
control,
handleSubmit,
formState: { errors, isValid }
} = useForm({ resolver: yupResolver(schema) })
My component TextField
import { TextInputProps, Text } from 'react-native'
import { Control, useController } from 'react-hook-form'
import { Container, Wrapper, TextInput, Label } from './styles'
import theme from '../../global/styles/theme'
type TextFieldProps = {
placeholder?: string
label?: string
icon?: React.ReactNode
error?: string
errors?: {
[x: string]: any
}
name: string
control: Control
} & TextInputProps
export function TextField(props: TextFieldProps) {
const { placeholder, label, icon, error, errors, name, control, ...rest } =
props
const { field } = useController({
control,
defaultValue: '',
name
})
return (
<>
<Container>
{!!label && <Label>{label}</Label>}
<Wrapper hasLabel={!!label} hasError={!!error}>
{!!icon && icon}
<TextInput
value={field.value}
onChangeText={field.onChange}
placeholder={error ? error : placeholder}
placeholderTextColor={
error ? theme.colors.attention : theme.colors.grayColor
}
{...rest}
/>
</Wrapper>
{errors && errors.name && errors.name.type === 'matches' && (
<Text>{errors.name.message}</Text>
)}
</Container>
</>
)
}
your component must include ref prop like
<TextInput
ref={inputRef} // this one
value={field.value}
onChangeText={field.onChange}
placeholder={error ? error : placeholder}
placeholderTextColor={
error ? theme.colors.attention : theme.colors.grayColor
}
{...rest}
/>
then, you need to create ref from parent for each field
const emailRef = useRef(null);
const passwordRef = useRef(null);
after this, you need to add the props that is
onSubmitEditing={() => passwordRef.current.focus()} // to auto focus password field
finally,
<TextField
onSubmitEditing={() => passwordRef.current.focus()} // here
name="name"
label={I18n.t('registerPersonal.fullNameLabel')}
placeholder={I18n.t('registerPersonal.fullNameInput')}
icon={<TypographyIcon fill={!!errors.name && theme.colors.attention} />}
error={errors.name?.message}
errors={errors}
control={control}
returnKeyType="next"
/>
I'm using react-native-phone-number-input and i want to delete the call code from the number if the user types it.
my solution is :
class PhoneUserInput extends PureComponent {
...
<PhoneInput
ref={this.myRef}
onChangeFormattedText={(value) => {
if (value.substr(`+${this.myRef.current?.getCallingCode()}`.length)
.startsWith(`+${this.myRef.current?.getCallingCode()}`)) {
value = value.substr(`+${this.myRef.current?.getCallingCode()}`.length)
.replace(`+${this.myRef.current?.getCallingCode()}`, ''));
}
onChangeFormattedText(value);
console.log('formated value'+value);
}
}}
/>
}
I tried to the same thing on onChangeText props but it's doesn't give the effect instantly on the component.
Please try to do your work with
onChangeText={ (_text)=>{ //Your code} }
AND add :
value={value}
Here is a working example :
<PhoneInput
ref={phoneInput}
defaultValue={value}
value={value}
defaultCode="FR"
layout="first"
placeholder={label}
containerStyle={styles.phoneContainer}
onChangeText={(text) => {
setValue(text)
}}
onChangeFormattedText={(text) => {
setFormattedValue(text)
}}
withDarkTheme
withShadow
/>
So in the docs it says just to add a prop on the edit component with an onSuccess / onFailure function and I've done that but navigating to the page to edit throws an error like this:
Warning: Unknown event handler property `onSuccess`. It will be ignored.
code here:
export const AffiliateEdit = (props) => {
const notify = useNotify();
const onSuccess = () => {
notify('Affiliate saved successfully');
}
return (
<Edit {...props} onSuccess={onSuccess}>
<SimpleForm redirect="list">
<ColorInput source="color" />
<TextInput source="name" validate={[required()]} />
<ReferenceInput
source="network_id"
reference="networks"
validate={[required()]}
>
<SelectInput optionText="name" />
</ReferenceInput>
<TextInput
source="reference"
validate={[required()]}
helperText="Please use all lower case, no spaces or underscores e.g affiliatename"
/>
</SimpleForm>
</Edit>
)
};
doc ref here: https://marmelab.com/react-admin/CreateEdit.html
Nvm figured it out, just needed to update react-admin lol
How can I set focus to an Input in react native?
I have some inputs in my code:
<Input
value={CompName}
onChangeText={(text) => this.onChangeText(text, 'CompName')}
style={styles.valueText}
/>
<Input
value={Phone}
onChangeText={(text) => this.onChangeText(text, 'Phone')}
style={styles.valueText}
/>
...
And I validate if it is empty. IF empty, I set a Alert message and I would like to focus on the field. How can I do it?
myFunc = () => {
if(CompName === '' || CompName === undefined){
Alert.alert('Company Name is required.');
// set focus here??
return;
}
...
}
Thanks
firstly, in react-native, it is TextInput. and here is two methods exposed via the native element. they are .focus() and .blur() that will focus or blur the TextInput programmatically.
for use it, you have to set a ref for the TextInput.
<TextInput
value={this.state.compName}
onChangeText={(text) => this.onChangeText(text, 'CompName')}
style={styles.valueText}
ref={(input) => { this.compNameInput = input; }}
/>
myFunc = () => {
if(this.state.compName === '' || this.state.comNam === undefined){
Alert.alert('Company Name is required.');
// set focus here??
this.compNameInput.focus();
return;
}
...
}
I suggest use the state for the compName, like this.state.compName