Question

I have a problem with laravel, and my form.

In my form (createBand.blade.php), i made a form with a dropdownlist wich call a database table: musicalGenre.

It should be noted that the dropdownmenu/list (select form) calls to another table in the database called MusicalGenre, and the form where I want the dropdownlist is to register a band in the database table Bands.

The select form works, i could choose in a dropdownlist all musicalGenre_name in my table after seeding them. Here's the blade page code (of course i have open, close the form and the section like laravel requires):

    <p>
    {{Form::label('name','Name of the Band: ')}}
    {{Form::text('name',Input::old('name'))}}
</p>
@if ($errors->has('name'))
<p class='error'> {{ $errors->first('name')}}</p>
@endif
<br>
<br>

<p>
    {{Form::label('email','Band Email: ')}}
    {{Form::text('email',Input::old('email'))}}
</p>
@if ($errors->has('email'))
<p class='error'> {{ $errors->first('email')}}</p>
@endif
<br>
<br>

<p>
    {{Form::label('musicalGenre','your style: ')}}
    {{Form::select('musicalGenre_name', $genre_options = array('' => 'select your style') + musicalGenre::lists('name'), Input::old('musicalGenre_name')) }}
</p>
@if ($errors->has('musicalGenre'))
<p class='error'> {{ $errors->first('musicalGenre_name')}}</p>
@endif
<br>
<br>

I have a controller named createBandController, where i made some rules for validators for the blade form. The poblem is: i can't pass the validators, that is to say, even if i choose a musical genre in my dropdownlist, for laravel there no choice made.

I have the error "musical genre is required". I don't understand the validator rules for a select form in my controller, or i don't know what i'm suposed to input in the rules for musical genre. Here's the controller code:

public function createBand() {
        $result = MusicalGenre::all();
        $genre = $result->lists('name');

        $inputs = Input::all();
        $rules = array(
            'name' => 'required|between:1,64|unique:bands,name',
            'email' => 'between:1,128|email|unique:bands,email',
            'musicalGenre' => 'integer|required|in:musicalGenre'
        );

        $validation = Validator::make($inputs, $rules);
        if ($validation->fails()) {
            return Redirect::to('createBand')->withInput()->withErrors($validation)
                            ->with('alert_error', 'you have some mistakes');

Don't pay attention to the names, (i'm french, i changed them in order to be clear for you), and i'm sure that is the validator of my dropdownlist who make problems when i fill out my form, because i can't pass it. In my original project code, there are no spelling mistakes.

All my validators and variable names work. I think i need to find the correct rules for a select form input and validators in order to laravel knows i made a choice when i choose in my dropdownlist.

At first i thought to specify that the dropdownlist use the database table musicalGenre. Like i specified that some fields are in the database table Bands, like this:

'name' => 'required|between:1,64|unique:bands,name'

Here, "name" is a field of the database table Bands. But it didn't work too.

If anyone have a solutions or wants to help, i'm interested. Thank you (and sorry if my english seems so bad).

Was it helpful?

Solution

The validation rule in: on the field musicalGenre won't work the way that you have implemented it. It expects a comma delimited list of strings which the field value is scrubbed against.

For example if the field was 'Gender' the validation would be:

'gender' => 'in:male,female'

To validate against musicalGenre against a model you will need to write a custom Validator. See http://laravel.com/docs/validation#custom-validation-rules

I am currently writing a custom validator for this 'belongs_to' validation and when I have it working I'll post it here.

UPDATE

I have written a custom validation rule that should help. Firstly create a validators.php file and include it in global.php

require app_path().'/validators.php';

Within validators.php create the custom rule:

Validator::extend('exists_in', function($attribute, $value, $parameters)
{
  $result = $parameters[0]::find($value);
  return ($result == null) ? FALSE : TRUE;
});

In your validations you could now have:

'musicalGenre' => 'integer|required|exists_in:musicalGenre'

The exists_in validation takes a parameter of the Model Class name

And to give this validation an error message, add the following to the array in app/lang/en/validation.php:

"exists_in" => "The selected :attribute is invalid."

OTHER TIPS

Does exists:musicalGenre instead of in:musicalGenre help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top