vendredi 17 avril 2015

Is there a correct way to implement partial validations for different parts of one form in Laravel

I'm working on a project in Laravel that consists of about 100 fields that are distributed over 5 individual pages and forms for those pages. A user is able to save their information even if the required fields aren't filled out yet (although they cannot submit the entire form until all of the required fields are filled out).


What I'd like to do is have something like a "soft" validation per page, that doesn't prevent the model from being saved to the database, but simply has a visual indicator of whether the page is complete.


What I have done so far is implement a $rules array for each page, e.g.



//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 run validation against the model itself, rather than the Input::all() via something like



//also in Application model

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

return !$validator->fails();
}


My instincts tell me this isn't quite the best way to do it, but I'm not sure if there are some additional features of Laravel I'm unaware of that are more primed for something like this.


Aucun commentaire:

Enregistrer un commentaire