I am currently using Laravel Excel to import data from an excel file into database. When there are some errors occur, I'd like it to report in a foreign language rather than in English which I know I have to use customValidationMessages() function to define the output messages.
This is an example of regular messages:
There was an error on row 2. The somefield has already been taken.
There was an error on row 3. The somefield has already been taken.
Which consists of 2 parts
There was an error on row {some_row}. is the first part.
and
The somefield has already been taken. is the last part
and when I follow the document provided here https://docs.laravel-excel.com/3.1/imports/validation.html#custom-validation-messages all I can output in the foreign language is just the last part but the first part remains in English like this.
There was an error on row 2. ข้อความในภาษาต่างประเทศ
There was an error on row 3. ข้อความในภาษาต่างประเทศ
But I want it to be like this
ข้อความภาษาต่างประเทศแถวที่ 2. ข้อความในภาษาต่างประเทศ
ข้อความภาษาต่างประเทศแถวที่ 3. ข้อความในภาษาต่างประเทศ
How can I do it???
Thank you all in advance
after inspect the library source, it looks like they use the laravel's localization for that part too: __('There was an error on row :row. :message', ['row' => $this->row, 'message' => $message]);
https://github.com/SpartnerNL/Laravel-Excel/blob/44e165b73eaf182a2f699d905a20684889675b1c/src/Validators/Failure.php#L82
so you can define your own localization message like: "There was an error on row :row. :message" => "ข้อความภาษาต่างประเทศแถวที่ 2 (change to your own message because I don't really know your language)",
Related
I want user to provide postal code using input text field and despite of the white spaces he/she uses when providing the postal code I want it to pass validation if it is available in my database so I used the following code in my validator:
'postal_code' =>['required','string','max:7',Rule::exists('zipcodes','POSTAL_CODE')->where(function ($query) use($postalcode) {
return $query->whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postalcode)]);
})],
]);
but when I test the form I used it for it show error if I input aaa7fr but no error if I used aaa 7fr which is exactly in my database which I do not want and instead I want both to show no error.
Would you please kindly tell me where is the wrong in my code and how to fix it?
The update to Laravel 5.5 seems to be creating a few oddities for me. Probably something I've screwed up, but these issues only broke with the new version - this same code has not failed in 5.4, 5.3, etc. The bigger problem is that the error is not consistent on the same model - it fails on update, but works on store.
I have a date field called 'decom_date' on a 'prog' model with the $dates field on the model overridden to include 'decom_date'. A user can fill out a form for a new 'prog', and skip the 'decom_date' field. The model saves with no error. If the user edits the same prog model with the exact same form, and leaves the 'decom_date' field blank, the following error occurs in Laravel 5.5 only:
message "Data missing"
exception "InvalidArgumentException"
file "/var/www/ipfast/vendor/nesbot/carbon/src/Carbon/Carbon.php"
line 582
IE Carbon is now expecting a format instead of an empty string upon updates only. I can work around this with a mutator on the model like so:
public function setDecomDateAttribute($value)
{
$this->attributes['decom_date'] = $value ?: null;
}
No problem - this works, and I think will stop the new breaking 100%... but I worry when things suddenly break, especially as it doesn't seem consistent across the saves. This pattern fails consistently across every model I have with dates, and these were not broken before update.
Anyone able to shed some light on this - or maybe just something dumb I've done?
I can't confirm this behaviour. If your date is an empty string it always fails with your posted error, no matter if it is during update or create.
Normally in Laravel 5.4 and 5.5 the global middleware stack contains:
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
You can check this in app\Http\Kernel.php
With this middleware emptry strings shoul automatically be converted to a NULL value which Carbons handles properly.
What I can imagine is, that your decom_date field is not even present in the request data when you 'skip' this field during create ( Just check it with dd($request) all on top of your store method.
And if you leave the field 'blank' during your update method, the result is an empty string ( In case the middleware mentioned above is missing ... )
I am trying to make a joomla plugin, but I have few questions that I haven't found any answer.
What the plugin has to do: add a new field in register form(let's say Cell Number), and on form submit insert that cell number in database.
My documentation is this tutorial.
Questions:
How do you add a new field in register form? xml file is done, but I am not sure how to write the php code...(please help). What this code do?
$form->setFieldAttribute('something', 'required', $this->params->get('profile-require_something') == 2, 'profile5');
How do I get the cell number variable from that form? $jinput = JFactory::getApplication()->input; ?
Pleas help me with few tips. Thanks!
Just copy the code that is there. What will happen is that the php will automatically loop through all the fields in your xml.
$form->setFieldAttribute('something', 'required', $this->params->get('profile-require_something') == 2, 'profile5');
Is taking the field with the name something and changing it to be required if the parameter called profile-require_something is set to 2 . profile5 is the name of the xml file and the php file for the example plugin. It's the actual name of the plugin. You can have many profile plugins if you want but each needs its own name.
To get the a value you would do something like
$jinput->getString('cell_number', '');
This is not working as we discussed previously, for some reason and I have done is create some $vars for the uploaded file.
Code available here (Pastebin).
But never actually inserts anything
When doing a var_dump($csv_row) i get: bool(false)
var_dump($fh) shows: resource(89) of type (stream)
var_dump($insert_str) shows all 1700 records from the csv file (obviously too big to post on here)
So I’m guessing the while statement or the whole from if statement is wrong somewhere. Really really would appreciate some help on this, I need to get it working by tomorrow (monday)
You seem to be using the codeigniter database library wrong. I can see a
$this->db->set($insert_str);
but no $this->db->insert() to be found. The set() method only useful with an update() or insert() following it, See the docs, search for $this->db->set().
You either want to use the $this->db->insert_batch() (on the same doc page, unfortunately no direct links for sections) form and build up an array of arrays with your records (so you won't have to create a long sql string either).
Or you can use the $this->db->query() and just feed the $insert_str to it where you now call $this->db->set().
According to your question:
var_dump($insert_str) shows all 1700 records from the csv file
So i would rule out the possibility of the while loop or the if not working.
Put this code at the top of the controller or before reading csv file
ini_set('auto_detect_line_endings', true);
Check if this will work for you
A related problem that is specific to single field has been solved here. But how to customize a collection field's conversion error message?
Here is an example:
On a jsp page, I have a field in Collection type:
...
<s:iterator value="items" status="m">
<s:hidden name="selitmems[%{#m.index}].id" value="%{id}"/>
<s:textfield name="selitmems[%{#m.index}].quant" size="10"/>
</s:iterator>
The items' type is: List<Item>; the selitems' type is List<SelItem>.
I want selitmems[].quant property to be an integer type. If a string like "abc" is filled in for the first item by an end user, the default error message is:
Invalid field value for field "selitmems[0].quant".
The above message is not what i want. In my case, I would prefer to generalize the error message as follows regardless of the specific selected item:
Please input integers for the items.
Of course it would be great if the error message can vary according to the specific item:
Please input an integer for the first item.
I have tried to add some keys like "selitmems[0].quant" or "selitmems" in the properties file, but can't get the result. Is there way to customize the error message for a collection field in struts2 when I can still reuse the built-in type conversion functions?
Try using the "label" attribute. You can play some... interesting games with this, such as (untested, but close):
label="%{getText('selitem.quantity', { #m.index })}"
The property file would contain:
selitem.quantity=Item #{0}
You can change the conversion error message as described here, although this may not be precisely what you want to do.
(I've been known to remove the conversion interceptor altogether and let either the default converters or custom converters handle conversion errors when a bad conversion also fails the field's "real" validation.)
Mm hmm... you can play some crazy games with OGNL and substituion.
Put something like this in your properties file.
invalid.fieldvalue.selitmems.quant = Please input integers for the items.
Then selitmems[0].quant, selitmems[1].quant, selitmems[2].quant conversion errors all return the same message.