laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.01k forks source link

Optional input field validation issue. Forces to fill the fields. #18211

Closed rajasekar-d closed 7 years ago

rajasekar-d commented 7 years ago

Description:

Optional input field validation issue. I mentioned website & facebook_url, google_plus_url, twitter_url, linkedin_url as optional field even though i'm getting the validation error while submitting via ajax.

Payload:

{
   "full_name":"Rajasekar D",
   "resume_headline":"UI Engineer",
   "profile_summary":"Currently I work in a software company - Cognizant. Every day I encounter new challenges, which enhances my knowledge of JavaScript and popular frameworks like jQuery and Angular.js. I am also absolutely in love with HTML5 and CSS3. Even though we spend most of the time together creating beautiful pieces of different UIs, I enjoy learning other technologies such as Node.js, RubyonRails.",
   "dob":{
      "year":2003,
      "month":2,
      "day":4
   },
   "gender":"Male",
   "year_of_experience":3,
   "current_salary":60000,
   "expected_salary":60000,
   "website":"",
   "country":"India",
   "state_id":"1",
   "city":"Chennai",
   "full_address":"No. 16, Alex Street, Ullagram, Chennai, TamilNadu, India",
   "postal_code":600091,
   "facebook_url":"",
   "google_plus_url":"",
   "twitter_url":"",
   "linkedin_url":"",
   "date_of_birth":"2003-02-04"
}

Rules:

   public function rules()
    {
        return [
             'full_name' => 'required',
            'resume_headline' => 'required',
            'profile_summary' => 'required|min:30',
            //'profile_picture' => 'max:1024|image|mimes:jpeg,jpg,png,gif',
            'date_of_birth' => 'required|before:now-14 year',
            'gender' => 'required|in:Male,Female',
            'year_of_experience' => 'required|numeric|min:0',
            'current_salary' => 'required|numeric|min:60000',
            'expected_salary' => 'required|numeric|min:60000',
            //'resume' => 'max:1024|mimes:pdf,doc,docx',
            'website' => 'url',
            'country' => 'required|in:India',
            'state_id' => 'required|exists:states,id',
            'city' => 'required',
            'full_address' => 'required',
            'postal_code' => 'required|regex:/^[1-9]\d{5}$/',
            'facebook_url' => 'sometimes|regex:/http(s)?:\/\/(www\.)?facebook\.com\/.+/i',
            'google_plus_url' => 'regex:/http(s)?:\/\/(www\.)?plus\.google\.com\/.+/i',
            'twitter_url' => 'regex:/http(s)?:\/\/(www\.)?twitter\.com\/.+/i',
            'linkedin_url' => 'regex:/http(s)?:\/\/([\w]+\.)?linkedin\.com\/.+/i',
        ];
    }

Validation Response:

{
   "website":[
      "The website format is invalid."
   ],
   "facebook_url":[
      "The facebook url format is invalid."
   ],
   "google_plus_url":[
      "The google plus url format is invalid."
   ],
   "twitter_url":[
      "The twitter url format is invalid."
   ],
   "linkedin_url":[
      "The linkedin url format is invalid."
   ]
}
devcircus commented 7 years ago

If the fields are optional, then you need to add the 'nullable' rule. 'Sometimes' means that the other validation rules will be run if the array of parameters includes said field. Since you're passing

  "facebook_url":"",
   "google_plus_url":"",
   "twitter_url":"",
   "linkedin_url":"",

then these fields are indeed present, therefore the other validation rules will be run. If the field is optional, add 'nullable':

'facebook_url' => 'sometimes|regex:/http(s)?:\/\/(www\.)?facebook\.com\/.+/i|nullable'
rajasekar-d commented 7 years ago

@devcircus Works like a charm :+1: