Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Dynamic Form Validation in Laravel with prohibited_if

    Dynamic Form Validation in Laravel with prohibited_if

    December 7, 2024

    Dynamic Form Validation in Laravel with prohibited_if

    Form validation in web applications requires careful handling of interdependent fields. Laravel’s prohibited_if validation rule provides an elegant solution for conditionally restricting field inputs based on other field values. Let’s explore how to implement this powerful feature in your applications.

    Understanding prohibited_if

    The prohibited_if validation rule enables you to specify when certain fields must remain empty or absent based on specific conditions. This proves invaluable when building dynamic forms with interdependent fields.

    use IlluminateSupportFacadesValidator;
    
    $validator = Validator::make($request->all(), [
        'freelance_company' => 'prohibited_if:type,individual',
        'type' => 'required|in:individual,business'
    ]);
    

    Real-World example of prohibited_if

    Let’s implement a professional membership registration system with dynamic field requirements:

    <?php
    
    namespace AppHttpControllers;
    
    use AppModelsMembership;
    use IlluminateHttpRequest;
    use IlluminateValidationRule;
    
    class MembershipController extends Controller
    {
        public function store(Request $request)
        {
            $request->validate([
                'membership_type' => 'required|in:personal,corporate',
                'full_name' => 'required|string|max:255',
                'organization_name' => [
                    'prohibited_if:membership_type,personal',
                    'required_if:membership_type,corporate',
                    'string',
                    'max:255',
                ],
                'organization_size' => [
                    'prohibited_if:membership_type,personal',
                    'required_if:membership_type,corporate',
                    'integer',
                    'min:1',
                ],
                'tax_id' => [
                    Rule::prohibitedIf(fn() => 
                        $request->membership_type === 'personal' || 
                        $request->country !== 'US'
                    ),
                    'string',
                    'size:9',
                ],
            ], [
                'organization_name.prohibited_if' => 'Organization details should not be provided for personal memberships.',
                'tax_id.prohibited_if' => 'Tax ID is only required for US-based corporate memberships.',
            ]);
            Membership::create($request->validated());
            return response()->json([
                'message' => 'Membership created successfully'
            ]);
        }
    }
    

    This implementation demonstrates:

    • Dynamic field requirements based on membership type
    • Conditional tax ID validation for US corporate members
    • Custom error messages for clarity
    • Combined usage with other validation rules

    You can extend this further with multiple conditions:

    'business_license' => [
        Rule::prohibitedIf(fn() => 
            $request->membership_type === 'personal' || 
            !in_array($request->business_type, ['retail', 'franchise'])
        ),
        'required_if:business_type,retail,franchise',
        'string',
    ]
    

    The prohibited_if rule enables the creation of sophisticated form validation logic while maintaining clean, readable code. This approach ensures your forms handle user input appropriately based on context.


    The post Dynamic Form Validation in Laravel with prohibited_if appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleStreamlining Route Parameters in Laravel Using URL Defaults
    Next Article Managing Large Datasets in Laravel with LazyCollection

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 15, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4695 – PHPGurukul Cyber Cafe Management System SQL Injection

    May 15, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Selenium-JavaMail api-Unable to perform email verification. It throws error as :- Please log in via your web browser:

    Development

    GPT-4.5 or GPT-5? Unveiling the Mystery Behind the ‘gpt2-chatbot’: The New X Trend for AI

    Development

    Experience the Future of AI at Agentforce World Tour Atlanta

    Development

    Unveiling WolfsBane: Gelsemium’s Linux counterpart to Gelsevirine

    Development

    Highlights

    Linux

    Huawei’s New Laptops May Run Linux, not HarmonyOS Next

    March 6, 2025

    Is Huawei planning to ship Linux on its upcoming MateBook laptops instead of HarmonyOS NEXT?…

    Threat Actors USDoD and SXUL Claim 70 Million Rows of Sensitive Data in Alleged Prison Data Breach

    May 20, 2024

    Automating Zero Trust in Healthcare: From Risk Scoring to Dynamic Policy Enforcement Without Network Redesign

    April 24, 2025

    15+ Best Photoshop Action Sets for Cinema & Movie Photo Effects

    July 26, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.