Is there a correct way to do partial validations for different parts of the same form in Laravel

I am working on a project in Laravel that has about 100 fields that are spread across 5 separate pages and forms for those pages. The user can save their information even if the required fields have not been completed yet (although they cannot submit the entire form until all the required fields are filled in).

I would like to do something like "soft" validation on the page that doesn't prevent the model from being saved to the database, but just has a visual indicator of whether the page is complete.

What I have done so far is to implement an array $rules

for each page, eg.

//in Application model
public static $page_1_rules = array(
            'has_previously_attended' => 'required',
            'degree_status' => 'required',
            'date_of_birth' => 'size:10',
            ...

public static $page_2_rules = array(
            'high_school_start_date' => 'required_if:degree_status,new|size:10',
            'previous_college_1_start_date' => 'required_if:degree_status,transfer|size:10',
            ...

      

Is it possible (or even a good idea) to perform the validation on the model itself and not Input::all()

through something like

//also in Application model

public static function page1IsComplete($application) 
{
    $validator = Validator::make(/*[get all of the $application values]*/, Application::$page_1_rules);

    return !$validator->fails();
}

      

My instincts tell me this isn't exactly the best way to do it, but I'm not sure if there are any additional Laravel features that I am not aware of that are more loaded for something like this.

+3


source to share


1 answer


I would do something like this:



public function store(Request $request) {

$step_first = $this->validate_step_1($request);
if($step_first == "true")
// do soft save


$step_second = $this->validate_step_2($request);
if($step_second == "true")
// do actual save

}

    public function validate_step_1(Request $request) {

        // soft validations
        $v = Validator::make($request->all(), [
            'email' => 'email',
            'company_name' => 'min:2'

        ]);

        if ($v->fails()) {

        return false;

        } else {
        return true;
        }
   }

    public function validate_step_2(Request $request) {

        // some serious validations
        $v = Validator::make($request->all(), [
            'email' => 'email',
            'company_name' => 'required',
            'legal_name' => 'required',
            'short_description' => 'required|min:20|max:2500',
            'country' => 'required|not_in:0',
            'city' => 'required|not_in:0',
            'company_logo' => 'mimes:jpeg,png|max:1024|min:5',
            'telephone_1' => 'required|min:4',
            'zipcode' => 'required',
            'address_line_1' => 'required|min:5'
        ]);

        if ($v->fails()) {

        return false;

        } else {
        return true;
        }
   }

      

+2


source







All Articles