How to custom error messages - respect-validation

Hi everyone and thanks for your help.
I'll like to custom error messages for validation and eventually to create different version for each language, it's possible ?
Here my code in his actually state.
namespace App;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
class Validator {
private $messages = [
'lastname',
'firstname',
'phone',
'email',
'message'
];
//other functions and declarations of my class.
private function validationData() {
$rule = null;
switch ($this->page) {
case 'contact';
$rule = v::key('lastname', v::notEmpty()
->alpha("-'"))
->key('firstname', v::optional(v::alpha("-'")))
->key('phone', v::optional(v::phone()))
->key('email', v::email())
->key('message', v::optional(v::length(5, 500)));
break;
default;
break;
}
try {
$rule->assert($this->data);
} catch (ValidationException $exception) {
var_dump($exception->findMessages($this->messages));
}
}
}
It returns me that:
array(5) {
["lastname"]=> string(51) "lastname must contain only letters (a-z) and ""-'"""
["firstname"]=> string(0) ""
["phone"]=> string(38) "phone must be a valid telephone number"
["email"]=> string(25) "email must be valid email"
["message"]=> string(44) "message must have a length between 5 and 500"
}
I need to keep this structure of my result of validation. Because I need field's names for could to display them at client side.
Thanks again,

You can do it with Respect/Validation.
I'll like to custom error messages for validation
When you define your messages, just make it an associative array with the key that represents the error and the value the message that will be shown:
private $messages = [
'lastname' => 'Error for lastname',
'firstname' => 'Custom error for firstname',
'phone' => 'Custom error for phone',
'email' => 'Custom error for email',
'message' => 'Custom error for message'
];
and eventually to create different version for each language
You can create a custom function that converts the messages in any language you want to:
$translateMessage = function($message){
$language = 'it'; //Retrieve your language here or use 'en' if you want your custom messages
$messages = [
'Key {{name}} must be present' => [
'en' => 'Key {{name}} SHOULD be present',
'it' => 'La chiave {{name}} deve essere presente'
],
'{{name}} must be a string' => [
'en' => '{{name}} SHOULD be a magic custom string',
'it' => '{{name}} deve essere una stringa'
],
'{{name}} must contain only letters (a-z)' => [
'en' => '{{name}} SHOULD contain only my letters (a-z)',
'it' => '{{name}} deve contenere solo lettere (a-z)'
],
'{{name}} must contain only letters (a-z) and "{{additionalChars}}"' => [
'en' => '{{name}} must contain only letters (a-z) and "{{additionalChars}}',
'it' => '{{name}} deve contenere solo lettere (a-z) e "{{additionalChars}}"'
]
];
return $messages[$message][$language];
};
and then use it in your catch block:
catch (ValidationException $exception) {
$exception->setParam('translator', $translateMessage);
var_dump($exception->findMessages($this->messages));
}

Related

Discord JS MessageSelectMenu

I would like to add an option to an existing MessageSelectMenu that I created like this:
button = new MessageActionRow().addComponents(
new MessageSelectMenu()
.setCustomId('selects')
.setPlaceholder('Test')
.addOptions([
{
label: 'test1',
value: '1',
},
])
)
For example, I would like to add this option:
{
label: 'test2',
value: '2',
},
I can't do it.
Can you help me?
Thank you in advance for your answers.
To make Discord Select Menus you can follow:
const button = new MessageActionRow()
.addComponents(
new MessageSelectMenu()
.setCustomId('selects')
.setPlaceholder('Test')
.addOptions([
{
label: 'test1',
description: 'This is a description',
value: '1',
},
// if you want to add another option in THIS menu you can continue below:
label: 'You can select me too',
description: 'This is also a description',
value: 'second_option',
},
]),
);
For receiving Select Menus attach an eventlistner to client you can use Interaction.isSelectMenu()
client.on('interactionCreate', async interaction => {
if (!interaction.isSelectMenu()) return;
if(interaction.CustomId === 'selects'){
await interaction.update('selected')
}
});

Filter an array by category and keyword

So I'm trying to filter an array by the category of an item and by its title.
For example, I have a few jobs, each job is an object, containing a job title and an array of categort, so:
const category = [
'Web designer',
'App developer',
'Cleaning',
'Designer',
];
const [userSelectedCategory, setUserSelectedCategory] = useState([]);
const jobs = [
{
title: 'Job 1',
category: ['Web designer', 'App developer'],
},
{
title: 'Job 2',
category: ['Cleaning', 'Web Designer'],
},
{
title: 'Design',
category: ['Designer', 'Web Developer'],
},
];
const categoryoptions = category.map(el=>{
return <button onClick={()=>setUserSelectedCategory(prev=>{...prev, el})}>{el}</button>
//Now each time a user clicks on a button, we can compare the userSelectedCategory array to the individual job category array.
});
So my approach was to do something like this:
//This value is set whenever a user searches in search bar
const [keyword, setKeyword] = useState('');
const pattern = new RegExp('\\b' + keyword.replace(/[^a-zA-Z0-9 ]/g, ''), 'i');
const filteredjobs = jobs?.filter(
(job) =>
(keyword === '' && category.length === 0) ||
(category.some((el) => job.category.includes(el)) &&
pattern.test(job.title))
);
So the HTML would look like this:
<div>
<input onChange={e=>setKeyword(e.target.value)} />
{categoryoptions}
{filteredjobs}
</div>
But the issue with this is that if no categories are selected, then nothing will get returned as default.
So to explain how I want my filter to work,
I want all of the jobs to be displayed if the keyword is blank (input is empty). But if the input is not empty, I want to filter by that value.
In addition, I want it to filter by category even if the keyword is blank.
Lastly, I want it to filter by keyword and by category and both of them are not empty.
use conditional rendering in place of {filteredjobs}:
<input value={keyword} onChange={e => setKeyword(e.target.value)} />
{keyword ? filteredjobs : jobs}

Yii2 - Bulk checkbox by GET request

I have a checkbox in a gridview Yii2 like this,
[
'class' => 'kartik\grid\CheckboxColumn',
'width' => '20px',
'checkboxOptions' => function ($model, $key, $index, $column) {
return [
'value' => trim($model->vessel),
];
}
],
Then to get all the value checkbox in yii2, I use this button
Html::a('<i class="glyphicon glyphicon-print"></i> Print All',
["print-all-based-date"],
[
"class" => "btn btn-success",
'role' => 'modal-remote-bulk',
])
But when in my controller that handle the the action,
public function actionPrintAllBasedTanggal()
{
$request = Yii::$app->request;
$get = $request->get();
print_r($get);
die();
I get :
Array
(
[r] => iwwi/incoming/print-all-based-tanggal
[KMTC_HOCHIMINH,OOCL_NAGOYA] =>
[_] => 1495123320863
)
What it means [KMTC_HOCHIMINH,OOCL_NAGOYA] =>,
I check in html, the checkbox is named selection[] ?
I need this : KMTC_HOCHIMINH,OOCL_NAGOYA
to get continue my app.
Please advise.
Thanks
may be you can use jquery for the solution.
example :
$(document).on('click','#ceklist_all',function(){
if ($(this).is(':checked')) {
$('.ceklist_child').attr('checked',true);
your_variable = [];
$('.ceklist_child:checked').map(function(key,val) {
if(this.checked) {
your_variable[key] = this.value;
}
}).get();
}
});
so,. you can use the your_variable and use the ajax for submit..
$.ajax({
type: 'get',
url: your_url,
data: {
'your_variabel_to_post' : your_variable
},
success: function(data){
// success function
},
error: function(data){
if(data.responseText)
alert(data.responseText);
},
});
CMIIW,.
just the optional solution. heheh

twig array of objects

I'm working on a symfony2 project.
I send from my controller to twig, an array of arrays of objects.
My array is nicely set, and got the values I want.
But when I try to access to these datas on twig, I can't...
My twig looks like {{ myarray.1.0.getFichier() }}
But, twig didn't call getFichier method of myarray.1.0.
Here is what twig responses to me : Item "getFichier" for "Array" does not exist in CDUserBundle:Prof:edit_session.html.twig at line 74
Edit :
dump(myarray.1.0) shows nothing, dump(myarray) shows nothing.
But dump() show a blank page...
Edit² :
Here is my controller
return $this->render('CDUserBundle:Prof:edit_session.html.twig', array(
'erreur' => $erreur,'message' => $message,
'title' => 'C# | Editer session',
'description' => 'keywords description',
'sessionInfo' => $sessionInfo,
'sessionFull' => $sessionFull,
'documents' => $documents,
'videos' => $videos,
'a' => 'showForm',
'vidName' => $videos[0]->getName(),
'vidDMCId'=>$videos[0]->getDMCId(),
'session' => $form->createView(),
'partPath' => $documents[0]->getFichier()
));
My Array is either $documents either $videos
Here is when I create arrays
$videos=array();
if($sessionFull[0]['sess_vid_id']!=NULL) {
if($em->getRepository('CD\ConfigBundle\Entity\Video')->findOneById($sessionFull[0]['sess_vid_id']))
array_push($videos,$em->getRepository('CD\ConfigBundle\Entity\Video')->findOneById($sessionFull[0]['sess_vid_id']));
else
array_push($videos,new Video());
}
else
array_push($videos,new Video());
for($i=0;$i<4;$i++) {
if($sessionFull[$i]['coursVidId']!=NULL) {
$vids=array();
$vidsId=explode(',',$sessionFull[$i]['coursVidId']);
foreach($vidsId as $vidId) {
if($em->getRepository('CD\ConfigBundle\Entity\Video')->findOneById($vidId))
array_push($vids,$em->getRepository('CD\ConfigBundle\Entity\Video')->findOneById($vidId));
else
array_push($vids,new Video());
}
array_push($videos,$vids);
}
else
array_push($videos,array(new Video()));
}
$documents=array();
if($sessionFull[0]['sess_doc_id']!=NULL) {
if($em->getRepository('CD\ConfigBundle\Entity\Document')->findOneById($sessionFull[0]['sess_doc_id']))
array_push($documents,$em->getRepository('CD\ConfigBundle\Entity\Document')->findOneById($sessionFull[0]['sess_doc_id']));
else
array_push(new Document());
}
else
array_push($documents,new Document());
for($i=0;$i<4;$i++) {
if($sessionFull[$i]['coursDocId']!=NULL) {
$docs=array();
$docsId=explode(',',$sessionFull[$i]['coursDocId']);
foreach($docsId as $docId) {
if($em->getRepository('CD\ConfigBundle\Entity\Document')->findOneById($docId))
array_push($docs,$em->getRepository('CD\ConfigBundle\Entity\Document')->findOneById($docId));
else
array_push($docs,new Document());
}
array_push($documents,$docs);
}
else
array_push($documents,array(new Document()));
}
Use {{ myarray.1.0.Fichier }} directly

netzke FormPanel

I think i stucked here. I have a model (Test) with 3 fields: id, name, name2. So i want to write there something and click on Apply button on bottom and if all fields are filled and passed validation (i guess this i should do in model Test.rb, yeah?) and get to localhost:3000/some/where and if i left some filed (name or name2) unfilled so i get a message like "ERROR".
test_panel.rb
class TestPanel < Netzke::Basepack::FormPanel
js_mixin :actions
def configuration
super.merge(
:name => :test_panel,
:model => 'Test',
:title => "TEST PANEL",
)
end
end
action.js
{
onApply: function() {
var form = this.getForm();
if (form.isValid()) {
this.Apply(form.getFieldValues(), function(success) {
if (success) {
window.location = 'some/where';
} else {
Ext.Msg.show({
title: 'FF',
msg: 'I guess you have an error!!',
buttons: Ext.Msg.OK,
icon: Ext.Msg.WARNING });
}
}, this);
} else {
Ext.Msg.show({
title: 'FF',
msg: 'Fill all fields!!',
buttons: Ext.Msg.OK,
icon: Ext.Msg.WARNING });
}
}
}
If my understanding is correct no need to do any thing with netzke. Just write your validators in the rails model. If any field is failed to pass the validation Netzke will capture the validation message from rails model and display it on top of the grid.

Resources