3iya7w
Last Updated: February 25, 2016
·
7.47K
· rizwaniqbal
605ae30a9f9b68da5ccdd86a5a296f75

Validate Multiple Choice fields in Laravel 4

I don't support the idea of validating data in Controllers against the contrarian argument of doing this using Models. Models are not just merely a map to our data stores but also hold associations, foreign key mappings and other canonical information. Hence, my argument that they should hold validation rules too.

Anyhow, the problem I was facing building a web comic portal was validating a multiple choice selection. I had been working on a clients project to build a social web comics portal. In such a scenario, there exists a many to many relation between the comics added by users and the tags used. For this I had a comics table, a tags table and the customary comic_tag table. The idea was that when users were selecting tags to be added to the comics, there were multiple values passed through a number of checkboxes, all named tags[]. I generally use the static::$rules array set in each model to validate the values submitted using a simple function -

public static function validate($input = null) {
    if (is_null($input)) {
        $input = Input::all();
    }

    return Validator::make($input, static::$rules); 
}

The problem here was that I was submitting an array to the input and I had to check if each value submitted passed three validations:

  1. They were in the tags table
  2. They were integers
  3. And, of course, they were required.

So, I found a nifty little trick to get this done. You iterate through the input array and set rules for each tag type. This is how the modified function looks

public static function validate($input = null) {
    if (is_null($input)) {
        $input = Input::all();
    }
    // We need to do this to not return an error when no tag is selected
    if (array_key_exists('tags', $input)) {
        //Rules for multiple tag selection
        for ($i = 0; $i < count($input['tags']); $i++) {
            static::$rules["tags.{$i}"] = 'required|integer|exists:tags,id';
        }
    } else {
        static::$rules["tags"] = 'required|integer|exists:tags,id';
    }

    return Validator::make($input, static::$rules); 
}

You will have to play with your error messages a little, cause the default messages would be like

Tag.1 is invalid

Say Thanks
Respond

3 Responses
Add your response

13990
F99c0d5767fc68412014ae43799dc267

I'm having a similar situation. I have a text input where the user can enter different tags and when submitting, laravel returns me this string:

Input::get('tags') = 'tag1,tag2,tag3'.

To separate each one

$alltags =  explode(',', Input::get('tags'));

Now:

$alltags = ['tag1','tag2',tag3']

To validate the tags:

for ($i = 0; $i < count($alltags); $i++)
    $rules[$i] = 'required|between:3,15';
$v = Validator::make($alltags, $rules);

Is there any other way to validate them?
When i return the errors it displays:

"The 2 must be between 3 and 15 characters." 

Where 2 is the 3rd tag on the array. Instead it could show like yours

tag.2
over 1 year ago ·
13997
605ae30a9f9b68da5ccdd86a5a296f75

Do it using the validate method in your controller. Should take care of it.

Also, take a look at http://stackoverflow.com/a/17647079/1156254 - might help you out.

over 1 year ago ·
14000
F99c0d5767fc68412014ae43799dc267

Thank you.

over 1 year ago ·
Awesome Job

Bc0a6794 05ee 11e8 902a 778405599bdb
Software Engineer
·
Denver, San Francisco
·
Full Time