custom code not working as expected

i have written a custom code to validate the form data , which is wworking fine when i tested in dartpad.dev , same code not working in custom funcitons.

here is my code snippet:

import 'dart:convert';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/backend/schema/structs/index.dart';
import '/auth/firebase_auth/auth_util.dart';

dynamic validateFormSignUp(dynamic formData) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  print(formData);
  Map<String, dynamic> validationRules = {
    "firstname": [
      (value) =>
          (value == null || value.isEmpty) ? "First name is required" : null
    ],
    "lastname": [
      (value) =>
          (value == null || value.isEmpty) ? "Last name is required" : null
    ],
    "email": [
      (value) => (value == null || value.isEmpty) ? "Email is required" : null,
      (value) => !RegExp(r'^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$')
              .hasMatch(value)
          ? "Invalid email address"
          : null,
    ],
    "password": [
      (value) =>
          (value == null || value.isEmpty) ? "Password is required" : null
    ],
    "confirm_password": [
      (value) => (value == null || value.isEmpty)
          ? "Confirm password is required"
          : null,
      (value) =>
          (value != formData["password"]) ? "Passwords do not match" : null,
    ],
    "dropdown_role": [
      (value) => (value == null || value.isEmpty) ? "Role is required" : null
    ],
  };

  Map<String, dynamic> validationResults = {};

  formData.forEach((fieldName, value) {
    if (validationRules.containsKey(fieldName)) {
      List<Function(dynamic)> rules = validationRules[fieldName]!;
      List<String> errors = [];

      for (var rule in rules) {
        String? error = rule(value);
        if (error != null) {
          errors.add(error);
        }
      }

      if (errors.isNotEmpty) {
        validationResults[fieldName] = errors;
      }
    }
  });

  return validationResults;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

here is the input:

{

"firstname": "John",

"lastname": "",

"email": "[email protected]",

"password": "123456",

"confirm_password": "1234561",

"dropdown_role": "user",

}

2
4 replies