cnbwd
cnbwd

Reputation: 379

Validation in laravel 4

Iam new in laravel.

Iam trying to laravel form validation.

my validation code is

 $rules = array(
 'studname'=>'required',
 'pmobile'=>'required|digits:10',
 'studadno'=>'required|unique:wys_students,studadno',
 'studrollno'=>'required|numeric|unique:wys_students,studrollno',
 'studgender'=>'required|in:male,female',
 'studdob' =>'required',
 'studbldgrp'=>'required|in:O+ve,O-ve,A+ve,A-ve,B+ve,B-ve,AB+ve,AB-ve,Other',
    );  $messages = array(
    'required' => 'The :attribute field is required.',
     'in'      => 'The :attribute must be one of the following types: :values',
);
    $validate=Validator::make(Input::all(), $rules, $messages); `

output is-:

  Student Name :The studname field is required.
  Parent Mobile:
  The pmobile field is required.

but, I want eg: student name field is required.. how to change my validation code?

Upvotes: 2

Views: 79

Answers (4)

lukasgeiter
lukasgeiter

Reputation: 152850

You can provide translations of the attribute names in app/lang/xx/validation.php.

There's an attributes array at the very bottom, just add your attributes:

'attributes' => array(
    'studname' => 'student name',
    'pmobile' => 'parent mobile',
    // and so on
),

This way you don't have to rewrite the message for every field and every rule. And you can even use those field names across multiple forms.

Upvotes: 0

Sameer Shaikh
Sameer Shaikh

Reputation: 7814

$messages = [
    'studname' => 'student name field is required.',
];

$validator = Validator::make($input, $rules, $messages);

You can pass the $messages array as the third parameter and you can define custom messages in the $messages array

Upvotes: 2

cnbwd
cnbwd

Reputation: 379

$rules = array(
      'studname' => 'required',
    'pmobile'=>'required|digits:10',
    'studadno'=>'required|unique:wys_students,studadno',
    'studrollno'=>'required|numeric|unique:wys_students,studrollno',
    'studgender'=>'required|in:male,female',
    'studdob' =>'required',
    'studbldgrp'=>'required|in:O+ve,O-ve,A+ve,A-ve,B+ve,B-ve,AB+ve,AB-ve,Other',
    );


$messages = array(
     'studname.required' =>'The Student Name field is required.',
     'pmobile.required'=>'The Parent Moblie field is required.',
     'studadno.required'=>'The Student Admission Nubmer field is required.',
     'studrollno.required'=>'The Student RollNubmer field is required.',
      'studgender.required'=>'Must be one of the following types: :values',
      'studdob.required'=>'The Student Date of Birth field is required.', 
    'studbldgrp.in'=>'Must be one of the following types: :values',
    );
        $validate=Validator::make(Input::all(), $rules, $messages);

Upvotes: 0

Margus Pala
Margus Pala

Reputation: 8663

There are 2 options.

Firstly you can change the form field name to student_name and then you will see message "The student name field is required"

Secondly you can write custom messages for each of the fields. In you case it will be:

$messages = array(
 'studname.required' => 'The Student Name field is required.'  
);

Upvotes: 0

Related Questions