In laravel 5.4 when validation is failed I do like:
if ($validator->fails()) {
$errors_list = $validator->messages()->all();
and I got array like :
[errors_list] => Array
(
[0] => The image has already been taken.
[1] => The is main has already been taken.
)
What I dislike in this output that actuall name of error field is ommitted.
Code
echo '<pre>$validator->messages()::'.print_r($validator->messages(),true).'</pre>';
has output:
$validator->messages()::Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[image] => Array
(
[0] => The image has already been taken.
)
[is_main] => Array
(
[0] => The is main has already been taken.
)
)
[format:protected] => :message
)
And I did not find how access to messages data.
I would like to get array like:
[errors_list] => Array
(
[image] => The image has already been taken.
[is_main] => The is main has already been taken.
)
Is there is a way to make it ?
Thanks!
I hope this will work for you.
$errors->has('image');
$errors->get(image);
$errors->has('is_main');
$errors->get(is_main);
It's a tricky one. The reason it's like that is because there may be multiple validation errors happening at once. However if you only have a single rule per entry:
array_combine($validator->messages()->keys(),$validator->messages()->all())
Related
i have an array, something like
[0] => Array
(
[id] => 1
[name] => Jimmy
[address] => Array
(
[number] => 1
[street] => Astreet
)
)
I need to access [street] using something like helper array_get dot notation:
array.address.street
However, as it can have multiple elements, I need something like
array*
where it can just get the first one.
Coming from cakephp they have a helper so i can do
array.{*}.address.street
Is there something similar in laravel, i cannot find such
If you want to get street from the first element you can do:
array_get(head($array), 'address.street');
// or
array_get($array, '0.address.street')
And if you want to get list of elements that contains street you can use array_pluck:
array_filter(array_pluck($array, 'address.street'));
$addresses = array_map(function($obj) {
if ( array_key_exists('address', $obj) )
if ( count($obj['address']) > 0 )
return $obj['address'][0];
}, $yourArray);
I am trying to create a upload plugin. I am keeping all the logic in behavior. This thing was working all fine by yesterday and from no where this strange problem is surfaced.
/*controller code */
debug($this->request->data);
$this->Model->saveAll($this->request->data);
/* outputs
Array
(
[Ad] => Array
(
[s] => 2
[d] => 2
)
[Upload] => Array
(
[field] => Upload
[table] => Ad
[filename] => Array
(
[name] => index.php
[type] => application/x-php
[tmp_name] => /tmp/php3MbvRh
[error] => 0
[size] => 32
)
)
)
*/
I am developing a plugin. In plugins beforeSave() i debug the same data and it shows
public function beforeSave(Model $Model) {
debug($Model->data);
}
/* outputs
Array
(
[Upload] => Array
(
[1pload] =>
)
)
*/
Everytime there appears a number for the fields property. Sometimes its 1, 8 and/or 9. :(
The plugin is loaded fine from bootstrap.php (CakePlugin::loadAll())
The plugin uses uploads table. The model of which is maintained as model.
The plugin behavior is properly defined and was working perfectly. NOT NOW
I couldn't figure out the problem. But the debug() in beforeSave() was being executed several times. So, I added in the plugin
if( isset ($this->data['preferred']['data'] ) {
}
For first few passes it is still something similar but on the third or fourth time it does show the data.
So, I have come with a theory that the beforeSave in plugin was being asynchronous to that of model's beforeSave and if i put a check then that would solve the issue. In fact it did.
I have the following line in my controller:
$data['faq'] = $this->faqModel->get();
This data print the following using the print_r
Array
(
[faq] => Array
(
[0] => Array
(
[faqid] => 12
[catid] => 122
[question] => How this CMS works
[question_en] => How this CMS works
[answer] => How this CMS works?
[answer_en] => How this CMS works?
[sorder] => 2
[visible] => 1
)
[1] => Array
(
[faqid] => 8
[catid] => 121
[question] => How does the design cost?
[question_en] => How does the design cost?
[answer] => How does the design cost?
[answer_en] => How does the design cost?
[sorder] => 1
[visible] => 1
)
)
)
I want to use the value stored in the [catid] key, and I am trying to do something like:
$data['faq']['catid'] to get that value in the controller (I want to make another select with that value) But I am getting with this error message: Undefined index: catid
Anyone can help me to get the value of ['catid']???
Regards, Zoran
Its 3 dimensional array u look closely there is two elements in faq array. You must wrote something like this: $data['faq'][0]['catid'] or $data['faq'][1]['catid']
The way you are accessing the array is incorrect, you are missing the item index on the second level. The correct way to use it as you are doing would be to do
echo $data['faq'][0]['faqid']; //faqid of the first item
However, this will only show one faqid at a time, and it not so useful when you are iterating. So, a good way would be this way.
foreach($data['faq'] as $value) {
echo $value['faqid'];
}
I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.
[Villa] => Array
(
[0] => SimpleXMLElement Object
(
[VillaID] => 6
[VillaName] => Mary
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Sea
[Value] => 1000 m
)
[1] => SimpleXMLElement Object
(
[Destination] => Market
[Value] => 800 m
)
)
)
)
[1] => SimpleXMLElement Object
(
[VillaID] => 21
[VillaName] => Marion
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Beach
[Value] => 5 min
)
)
)
)
)
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
I can obtain it with:
echo 'Name of Villa: '.$xml->Villa[0]->VillaName.'';
etc. etc ... but in this way have to change manually for every villa (too much) the value in the brackets.
i've tried with $xml->Villa[$value]->VillaName; ($value comes from another page) but it's not working...
Tanks for help!
First of all, your question starts with "i have this xml:" followed by something that is not XML. I'm not saying this to be a smartass, rather because it's important for XML beginners to understand that print_r() is not the right way to inspect SimpleXMLElements. Sometimes it will show you things that aren't in your XML, other times it will not show things that are actually in your XML. In short: do not use print_r() on SimpleXMLElement. Just use ->asXML() and look at the actual XML.
From what I understand, you want to locate and select a node based on some criteria. XML just happens to have a language for that: XPath. The official specs aren't terribly user-friendly but w3schools.com has a pretty good XPath tutorial.
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
Anywhere in your document, you want to select all Villa nodes with an attribute VillaID whose value is "6". In XPath:
//Villa[#VillaID="6"]
Via SimpleXML:
$xml->xpath('//Villa[#VillaID="6"]');
Attention, xpath() always return an array.