I need an input field for a price field for a german website. Therefore I added a NumberFormat filter and the IsFloat validator. The entered price (e.g. 7,5) gets stored correct to the database double field in the format 7.5. But now the problem is, if I wan't to edit the field, it gets populated in the format 7.5 to the user, which he can't save, because it's not a german format. So he has to replace the "." with a ",".
Is there a way, to populate the number with a "," instead of the "."?
public function getInputFilterSpecification() {
return [
'price' => [
'required' => true,
'filters' => [
[
'name' => 'NumberFormat',
'options' => [
'locale' => 'de_DE'
]
]
],
'validators' => [
[
'name' => 'IsFloat',
'options' => [
'locale' => 'de_DE'
]
]
]
],
];
}
I would suggest using a ViewHelper that renders the float with a , instead of a .. Changing the storage of the data in the database and internally handling floats with a , separator seems not the right way to go and will definitely cause issues.
There is even an inbuilt currency formatter view helper in ZF2 that you can use.
For your German currency notation it will be as simple as:
echo $this->currencyFormat(1234.56, 'EUR', null, 'de_DE');
Related
So I am doing a form using the kartik form builder and I am using the input_widget kartik\date\datepicker. When I use the datepicker only it submits but when I go to the item view the date is set like 30/11/-0001. The solution I found when I first had this problem was using not only the date picker but also the date control (also from kartik). But at that time I was using the default yii form and it was possible to use multiple widgets in a single attribute. Now im using the kartik form builder and i cant set more than one widget and its class. How can I do to use both date picker and date control in a single attribute at kartik's form build or, if anyone knows, how to solve this problem without having to add the date control?
before it was like:
<?= $form->field($model, 'validadeCertificado')
->textInput()
->widget(DatePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
]
])
->widget(DateControl::className(), [
'type'=>DateControl::FORMAT_DATE,
'ajaxConversion'=>false,
'widgetOptions' => [
'pluginOptions' => [
'autoclose' => true,
'language' => 'pt-BR',
]
]
]);
?>
now it is like:
echo Form::widget([ // 3 column layout
'model'=>$model,
'form'=>$form,
'columns'=>12,
'compactGrid'=>true,
'attributes'=>[
'validadeCertificado'=>[
'type'=>Form::INPUT_WIDGET,
'widgetClass'=>'\kartik\date\DatePicker',
'options' => [
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'autoclose'=>true,
'todayHighlight' => true
],
],
'columnOptions'=>['colspan'=>3],
],
<!--(other attributes)-->
]
]);```
I just wanted the date to be correctly inserted in the mysql database but it is inserted into the database as 0000-00-00
Heeeelp
I am working with a Laravel 5.8 version.
I need to modify the output of an error message form, unfortunately, the validation is not working properly.
The documentation says (https://laravel.com/docs/5.8/validation):
Specifying Custom Values In Language Files
Sometimes you may need the :value portion of your validation message to be replaced with a custom representation of the value. For example, consider the following rule that specifies that a credit card number is required if the payment_type has a value of cc:
$request->validate([
'credit_card_number' => 'required_if:payment_type,cc'
]);
If this validation rule fails, it will produce the following error message:
The credit card number field is required when payment type is cc.
Instead of displaying cc as the payment type value, you may specify a custom value representation in your validation language file by defining a values array:
'values' => [
'payment_type' => [
'cc' => 'credit card'
],
],
Now if the validation rule fails it will produce the following message:
The credit card number field is required when payment type is credit card.
I have a validation rule like:
$request->validate([
'foo' => [
'required_if:bar.bizz,1',
'numeric',
]
]);
And in my validation.php.
'values' => [
'bar.biz' => [
'1' => 'test',
],
'bar[biz]' => [
'1' => 'test',
],
'*.*' => [
'1' => 'test',
],
]
But I always get the same output:
The foo field is required when bar is 1.
Thanks.
Solved
If you are working in your form with arrays, you have also use arrays like:
'values' => [
'bar' => [
'biz' => [
'1' => 'test',
]
]
],
Also, note we are not using foo variable in the values array.
PS: The wildcard symbol (*) does not work.
Greetings.
I am using REST API using PHP for fetching data from Elastic search with following code
$params = [
'index' => $search_index,
'type' => $search_type,
'from' => $_POST["from"],
'size' => $_POST["fetch"],
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'is_validated' => false ] ],
[ 'query_string' => [ 'query' => $search_str, 'default_operator' => 'OR' ] ]
]
]
]
]
];
Now, this is working perfectly and giving me my desired results.
The data that is returned from ES, has one column "result_source" and it has predefined values like CNN, BBC or YouTube etc.
What I need is, I want to filter results on "result_source" column in a way that, I can only fetch the results with the option I want. Like I want results that have "result_source" value only "YouTube" or only "BBC & CNN" or only "CNN or YouTube" etc.
I have already tried "Should" option, but it also returns the data with other values that I don't need. Not sure how to skip those values of "result_source" column in fetching results from ES.
Any help on this will be appreciated.
Thanks
Solved!!
I am replying to my own question, because I found a solution for it. May be it can help someone else in future.
If anyone is looking for a solution of searching within the field / column of Elastic search, here is what can be done.
[ 'query_string' => [ 'query' => $search_str.'(result_source:CNN OR result_source:BBC)', 'default_operator' => 'OR' ] ]
"result_source" is actually the field / column name of ES on which filter is applied to return results that have result_source=BBC or result_source=CNN.
This actually solved my issue.
I am using unclead / yii2-multiple-input widget.
I want to generate different number of rows with values from my database.
How can i do this?
I can design my columns in view and edit data manualy after page generated. But miss how to program the number of rows and its values in the view.
My code in view:
<?= $form->field($User, 'User')->widget(MultipleInput::className(), [
'min' => 0,
'max' => 4,
'columns' => [
[
'name' => 'name',
'title' => 'Name',
'type' => 'textInput',
'options' => [
'onchange' => $onchange,
],
],
[
'name' => 'birth',
'type' => \kartik\date\DatePicker::className(),
'title' => 'Birth',
'value' => function($data) {
return $data['day'];
},
'options' => [
'pluginOptions' => [
'format' => 'dd.mm.yyyy',
'todayHighlight' => true
]
]
],
]
])->label(false);
How can I make (for example) 8 rows with different values, and also have the ability to edit/remove/update some of them?
You need to look into the documentation as it says that you need to assign a separate field into the model which will store all the schedule in form of JSON and then provide it back to the field when editing/updating the model.
You have not added the appropriate model to verify how are you creating the field User in your given case above. so, i will try to create a simple example which will help you implement it in your scenario.
For Example.
You have to store a user in the database along with his favorite books.
User
id, name, email
Books
id, name
Create a field/column in you User table with the name schedule of type text, you can write a migration or add manually.
Add it to the rules in the User model as safe.
like below
public function rules() {
return [
....//other rules
[ [ 'schedule'] , 'safe' ]
];
}
Add the widget to the newly created column in ActiveForm
see below code
echo $form->field($model,'schedule')->widget(MultipleInput::class,[
'max' => 4,
'columns' => [
[
'name' => 'book_id',
'type' => 'dropDownList',
'title' => 'Book',
'items' => ArrayHelper::map( Books::find()->asArray()->all (),'id','name'),
],
]
]);
When saving the User model convert the array to JSON string.
like below
if( Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) ){
$model->schedule = \yii\helpers\Json::encode($model->schedule);
$model->save();
}
Override the afterFind() of the User model to covert the json back to the array before loading the form.
like below
public function afterFind() {
parent::afterFind();
$this->schedule = \yii\helpers\Json::decode($this->schedule);
}
Now when saved the schedule field against the current user will have the JSON for the selected rows for the books, as many selected, for example, if I saved three books having ids(1,2,3) then it will have json like below
{
"0": {
"book_id": "1"
},
"2": {
"book_id": "2"
},
"3": {
"book_id": "3"
}
}
The above JSON will be converted to an array in the afterFind() so that the widget loads the saved schedule when you EDIT the record.
Now go to your update page or edit the newly saved model you will see the books loaded automatically.
I'm trying to send a campaign to a dynamic list segment based on a custom numeric merge field (GMT_OFFSET, in this case) but the code below yields the following error from the MailChimp API:
"errors" => [
0 => [
"field" => "recipients.segment_opts.conditions.item:0"
"message" => "Data did not match any of the schemas described in anyOf."
]
]
My code, using drewm/mailchimp-api 2.4:
$campaign = $mc->post('campaigns', [
'recipients' => [
'list_id' => config('services.mailchimp.list_id'),
'segment_opts' => [
'conditions' => [
[
'condition_type' => 'TextMerge',
'field' => 'GMT_OFFSET',
'op' => 'is',
'value' => 2,
],
],
'match' => 'all',
],
],
],
// Cut for brevity
];
If I am to take the field description literally (see below), the TextMerge condition type only works on merge0 or EMAIL fields, which is ridiculous considering the Segment Type title says it is a "Text or Number Merge Field Segment". However, other people have reported the condition does work when applied exclusively to the EMAIL field. (API Reference)
I found this issue posted but unresolved on both DrewM's git repo (here) and SO (here) from January 2017. Hoping somebody has figured this out by now, or found a way around it.
Solved it! I passed an integer value which seemed to make sense given that my GMT_OFFSET merge field was of a Number type. MailChimp support said this probably caused the error and suggested I send a string instead. Works like a charm now.