قابلیت validator در ویجت TextFormField کار کنترل مقادیر ثبت شده توسط کاربر را بسیار راحت می کند . برای استفاده از این قابلیت فقط کافیست مطابق کد زیر عمل نمایید :
TextFormField(
// ignore: missing_return
validator: (String value){
if(value.length < 6)
{
return 'طول رمز ورود نباید کمتر از ۶ کاراکتر باشد';
}
},
obscureText: obscureText,
style: TextStyle(color: textColor, fontSize: textFontSize),
decoration: InputDecoration(
icon: Icon(
iconInput,
color: iconColor,
),
border: InputBorder.none,
hintText: hintText,
hintStyle: TextStyle(color: hintColorText, fontSize: hintFontSize),
contentPadding: EdgeInsets.symmetric(vertical: 15, horizontal: 5),
),
)
- TextFormField(
- // ignore: missing_return
- validator: (String value){
- if(value.length < 6)
- {
- return 'طول رمز ورود نباید کمتر از ۶ کاراکتر باشد';
- }
- },
- obscureText: obscureText,
- style: TextStyle(color: textColor, fontSize: textFontSize),
- decoration: InputDecoration(
- icon: Icon(
- iconInput,
- color: iconColor,
- ),
- border: InputBorder.none,
- hintText: hintText,
- hintStyle: TextStyle(color: hintColorText, fontSize: hintFontSize),
- contentPadding: EdgeInsets.symmetric(vertical: 15, horizontal: 5),
- ),
- )
TextFormField(
// ignore: missing_return
validator: (String value){
if(value.length < 6)
{
return 'طول رمز ورود نباید کمتر از ۶ کاراکتر باشد';
}
},
obscureText: obscureText,
style: TextStyle(color: textColor, fontSize: textFontSize),
decoration: InputDecoration(
icon: Icon(
iconInput,
color: iconColor,
),
border: InputBorder.none,
hintText: hintText,
hintStyle: TextStyle(color: hintColorText, fontSize: hintFontSize),
contentPadding: EdgeInsets.symmetric(vertical: 15, horizontal: 5),
),
)
همانطور که در خط ۳ کد فوق مشخص است ، توسط خصوصیت validator مقدار value دریافت و نسبت به آن تصمیم گیری می شود . مطالعه مطالب مربوط به ویجت TextFormField و اعتبارسنجی در فرم ها نیز توصیه می شود .
امیدوارم این مطلب برای شما مفید باشد .