Domanda

Is there a simpler way to use custom validation error messages in Laravel rather than listing each field and attribute in an array? I don't mind the typing, I just feel it looks rather dirty.

Here is my two blocks of code now...

public static $rules = array(
    'first_name' => 'required|alpha',
    'last_name' => 'required|alpha',
    'email' => 'required|email',
    'password' => 'required|alpha_num|min:8|confirmed',
    'password_confirmation' => 'required|alpha_num'
);

public static $messages = array(
    'first_name.required' => 'You must enter your First Name!',
    'first_name.alpha' => 'Your first name must contain alphabetical characters only!',
    '...and so on' => 'for the rest...'
);

in the public static $messages block, I'm wondering if there's any way I can clean that up without typing each field name and attribute? Can I, for example, do something like this instead?

public static $messages = array(
    'first_name' => array(
         'required' => 'msg',
         'alpha' => 'msg'
     ),
     'and so on' => array(
         'for the' => 'rest'
     )
);

To me, that seems cleaner. Thanks for any input you can provide.

È stato utile?

Soluzione

You can do pretty much what you're after, by creating a validation.php language file, with the format:

'custom' => array(
    'first_name' => array(
        'required' => 'Please enter your first name',
        'alpha' => 'Only letters for your first name please'
    ),
),

Source: http://laravel.com/docs/validation#custom-error-messages

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top