Laravel - validation localization doesn't work - validation

Whenever I use the trans() function to return a specific translation from the validation.php file, it works just perfectly. I have two languages in my application and the translations get returned for both of them.
However, whenever I use the Laravel validator, it returns messages in the default locale only. Is there something I need to specify in the validator? How do I make it work for both languages?

You need to pass as 3rd parameters your translations. Let's assume you have defined your fields, rules and validator like in the following code:
$data = Input::only('title');
$rules['title'] = 'required|min:20|max:80',
$validator = Validator::make($data, $rules,
Lang::get('forms.validation.entry'));
Now you need to define your translations. Let's assume you need translation for fr lang so you need co create lang/fr/forms.php file and put the following content into it:
<?php
return
array (
'validation' => array (
'entry' => array (
'title.required' => 'Your translation for title required',
'title.min' => 'Your translation for title min',
'title.max' => 'Your translation for title max',
)
)
);
Of course you can create file with simpler array but it's just example - instead of forms.validation.entry it could be for example just forms or validation.

The problem originated from my localization implementation. I've added App::setLocale(Session::get('lang')); to the App::before() method in the filters.php file and it all works now.

in resources/lang/en folder laravel have validation.php. When you create new language so you can copy this file to new created language folder and then edit it to this language.
for example you wanna create fr language
.
/resources/lang/fr/validation.php and translate it to this language

Related

Laravel Validation - Individual rule and individual field problem

i have a field in Laravel with the name "company_url". and its stored like this.
$post['company_url'] = "http://example.org";
and then i have a rule string which i have stored for validation which must be applied on this individual field. which is stored like this
$post['rule'] = "required|max:24";
now i am trying this code to get validation errors. which is not working.
$validator = Validator::make([$post['name']], [$post['rules']]);
tell me what is the way to get errors on this validation?
The data you're passing to make() is in incorrect format. It should be in key-value pair format.
Also I don't know where the $post['name'] coming from. I assume it is company_url not name.
$post['rules'] is also undefined. It should be $post['rule']
The following should work:
$validator = Validator::make(['company_url' => $post['company_url']], ['company_url' => $post['rule']]);

Laravel 5.5, Collections and Localization

This question is a follow-up/attempt to implement the answer from a prior question.
My attempt to pluck values from a collection and apply the current localization is as follows:
$prefix_array = ['' => trans('registration.prefixes.select')] +
$prefixes->pluck('prefix', 'prefix')->map(function($item, $key) {
return trans('messages.fields.prefixes.'.$item);
})->toArray();
However, this produces an array with values like:
"Mrs." => "messages.fields.prefixes.Mrs."
Instead of:
"Mrs." => "Sra." // eg: shortened Senora for Spanish translation
The localization path (messages.fields.prefixes.XYZ) is correct and references to it in other places displays as expected.
It may be trailing dot (period) is confusing the localisation. You may need to have your translation key as just 'mrs' => 'Mrs.'

laravel how to add validation rule based on custom database query

I want the user to input postal code then in register controller I want to validate that postal code against postal code database I have so if the user provided postal code is in my database it will success despite of the formatting he/she used and what I mean by that is I want the validation to success whether he/she use spaces or no spaces at all.
the query I would like to use:
$postalcode=$data['postal_code'];
zipcodes::whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postal_code)
I tried to use the following in my validation:
Rule::exists('zipcodes','POSTAL_CODE')->where(function ($query) use($postalcode) {
// die($postalcode);
$query->whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postalcode)]);
})
but it does not do what I want and it seems to already try to find if postal code exist in zipcodes table before performing mysql replace or php str_replace.
so please any suggestions?
The easiest way to do this is use Laravel's validate() method.
https://laravel.com/docs/5.7/validation
You can use the Exists validation rule. It will check the database you tell it to and if the record exists, if it does then it will show as valid.
If you need to trim the spaces before validation, first just do $request->postal_code = str_replace(' ', '', $request->postal_code); before you call the validate method.
Here is a complete example:
$request->postal_code = str_replace(' ', '', $request->postal_code);
$request->validate([
'postal_code' => 'required|exists:zipcodes',
]);

Generate form with drop down using Gii in Yii

I am new to yii.I want to create model and form using Gii,In my database table i used enum data type for specific values to select by user.than i generate model and crude using Gii but it doesn't create drop-down list in form for that enum field.To create that drop-down using Gii what should i have to do. or change templates of Gii.
please help me. Thanks in advance.
change the _form and put your dropdown after creation by gii
echo $form->dropdownList($model , 'fieldName' , array(
'private' => Yii::T('model' , 'private'), // give data as an array to populate your drop down
'public' => Yii::T('model' , 'public'),
))

Remembering CodeIgniter form_dropdown fields

This works...
form_dropdown('location', $location_options, $this->input->post('location'));
But when I try and use an array to add extra attributes, it stops working... Why is this?
$attributes = array(
'name' => 'location',
'id' => 'location'
);
form_dropdown($attributes, $location_options, $this->input->post('location'));
The name of the dropdown list is included in the array of attributes so i don't see how this is any different to the first example. Whenever the form is posted posted back, it resets to the start.
Can anyone help me out with this?
Thanks
It's just the wrong syntax.
Please have a look at the docu: http://codeigniter.com/user_guide/helpers/form_helper.html
form_dropdown('location', $location_options, $this->input->post('location'), "id='location'");
Your code should look something like the above. And by the way: if you're using the form_validation library you could use set_value instead of $this->input->post ...
$attributes = ' id="bar" class="foo" onChange="some_function();"';
$location_options = array(
'IN' =>'India',
'US' =>'America'
);
form_dropdown('location', $location_options, $this->input->post('location'),$attributes);
Parameters :
1st param will assign to name of the field,
2nd will get your options,
3rd is for default value,
4th one is for extra properties to add like javascript function, id, class ...

Resources