0

This question already asked but that is not solving my issue.

I have try to use validation in my laravel project. But it not working this is my code

$rules = array(
    'coupon_title'          => 'max:10',
    'coupon_type_id'        => 'numaric',
    'expiry_start_date'     => 'required|before_or_equal:expiry_end_date',
    'expiry_end_date'       => 'required',
);

$messages = [
    'before_or_equal'    => 'The :attribute must be smaller than the End Date',
    'before'    => 'The :attribute must be Greater than the Interview Start Date',
    'after'    => 'The :attribute must be Greater than the Interview End Date',
    'after_or_equal'    => 'The :attribute must be Greater than the Start Time',
    //'max' => 'asdasd'
];

$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) 
{
    $messages = $validator->messages();
    return response()->json(['message'=>$messages],401);
}

In my code only expiry_start_date field validated. But coupon_title and coupon_type_id fields not validated.

5
  • You have a tiny typo near coupon_type_id : it is 'numeric', not 'numaric' ;) Commented Dec 11, 2018 at 8:03
  • Are coupon_title and coupon_type_id allowed to be null? Commented Dec 11, 2018 at 8:04
  • @Virginia:Yes #coupon_title allowed null values Commented Dec 11, 2018 at 8:05
  • Can you add the HTML part / the view where you are using these four variables (coupon_title, coupon_type_id, expiry_start_date, expiry_end_date? I think that might make it easier to spot the problem. Commented Dec 11, 2018 at 8:07
  • @Virginia: I set coupon_title Null yes see this image imgur.com/a/QLtZIW2 Commented Dec 11, 2018 at 8:08

1 Answer 1

1

You need to correct spell numaric to numeric and add nullable keyword in validation rule

'coupon_title'          => 'string|max:10|nullable',
'coupon_type_id'        => 'numeric',

Also you can add custom message for numeric and max

$messages = [
    'before_or_equal'    => 'The :attribute must be smaller than the End Date',
    'before'    => 'The :attribute must be Greater than the Interview Start Date',
    'after'    => 'The :attribute must be Greater than the Interview End Date',
    'after_or_equal'    => 'The :attribute must be Greater than the Start Time',
    'max' => 'The :attribute has crossed the limit',
    'numeric' => 'The :attribute must have numeric value'
];
Sign up to request clarification or add additional context in comments.

2 Comments

Working but i have doubt want to fields change null to yes in database ?
@RameshS Yes you need to make column nullable in mysql

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.