// Create a new form $form = $this->createForm(MyFormType::class); // Handle form submission and validation $form->handleRequest($request); // Check if the form is valid if ($form->isSubmitted() && $form->isValid()) { // Do something with the form data } else { // Get list of validation failures $failures = $form->getErrors(true); // Output list of validation failures foreach ($failures as $failure) { echo $failure->getMessage(); } }
// Create a new model instance $user = new User(); // Set some invalid data $user->name = ''; // Validate the data $validator = Validator::make($user->toArray(), [ 'name' => 'required', ]); // Check if validation failed if ($validator->fails()) { // Get list of validation failures $failures = $validator->errors()->all(); // Output list of validation failures foreach ($failures as $failure) { echo $failure; } }Package library: Symfony or Laravel.