Skip to content

Instantly share code, notes, and snippets.

@adumskis
Created November 18, 2015 14:38
Show Gist options
  • Save adumskis/39f9b3789c859a21436c to your computer and use it in GitHub Desktop.
Save adumskis/39f9b3789c859a21436c to your computer and use it in GitHub Desktop.
Example of Laravel Request to rules by locales
<?php
namespace App\Http\Requests;
use App\CustomPage;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Config;
class CustomPageRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$available_locales = Config::get('translatable.locales');
$fallback_locale = Config::get('translatable.fallback_locale');
$rules = [];
foreach($available_locales as $available_locale){
if($fallback_locale == $available_locale){
$rules[$available_locale.'.menu_name'] = 'required|max:50';
$rules[$available_locale.'.title'] = 'required|max:150';
$rules[$available_locale.'.content'] = 'required';
} else {
$rules[$available_locale.'.menu_name'] = 'max:50';
$rules[$available_locale.'.title'] = 'max:150';
$rules[$available_locale.'.content'] = '';
}
}
return $rules;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment