I was trying to use the Yii2 ActiveForm encodeErrorSummary property because I wanted to put line-breaks on Yii2 validation error messages:
Example code snippet in MODEL file
public function rules()
{
return [['username', 'required', 'message' => 'long message first line here<br> long message last line here']];
}
Example code snippet in VIEW file
$form = ActiveForm::begin(['id' => 'myform',
'encodeErrorSummary' => false
]);
...
echo $form->field($model, 'username');
...
ActiveForm::end();
Official Yii2 Documentation describes encodeErrorSummary property as:
Whether to perform encoding on the error summary.
but it seemed not suitable for that in my case... Maybe it's me misunderstanding something (...error summary)?
So... what is it intended for, then?
Thank you!
It seems like you need to configure the $fieldConfig property like this:
ActiveForm::begin([
'fieldConfig' => [
'errorOptions' => ['encode' => false],
],
]);
for your requirement. The errorSummary is the summary that you echo with
<?= $form->errorSummary($model) ?>
before or after the form. What you want is a behavior at the field level, while this is an option to disable the encoding at the summary level.
Related
I used the laravel validator for credit card information as per instructed here.
I can finally let it work but my problem is how can I make its result readable by a user. I am also using Laravel validation like this:
public static function validate()
{
$val = [
'card_num' => ['required', 'numeric', new CardNumber],
];
return $val;
}
So, if I clicked the submit button and the field is empty, the result is card numは必須です。 which means card number is required. And if I supply an invalid card number, the result is validation.credit_card.card_length_invalid, how can I make it has the same result as leaving the field empty? I do like this when getting the validation errors in blade php.
<div>
<label>カード番号</label>
<input type="text" name="card_num" id="credit-card" value="{{ old('card_num', $user['card_num']) }}" >
</div>
#if ($errors->has('card_num'))
<p class="alert-error">
{{ $errors->first('card_num') }}
</p>
#endif
UPDATE
I have seen its source file and messages, but I don't want to directly change the vendor files.
I have tried adding the custom in validation.php, but it's not working.
'custom' => [
'validation.card_length_invalid' => '無効なカード長',
'validation.credit_card' => 'クレジットカード',
],
Your array in validation.php is close to correct. Try changing the top-level key to credit_card instead of custom like they have in the source code. I'm not 100% sure, but I don't think this package supports messages in the custom array like the ordinary Laravel rules do. For example:
'credit_card' => [
'card_length_invalid' => '無効なカード長',
'credit_card' => 'クレジットカード',
],
I am working on a custom package for one of my projects and came up with an idea to create a custom blade directive for the elements that I want to loop through.
I made a sample code that does not work:
// WIRES
\Blade::directive('startWire', function($params) {
$wires = [
0 => [
'name' => 'test1',
'image' => 'http://image.jpg'
],
];
return "<?php foreach($wires as $wire): ?>";
});
\Blade::directive('endWire', function() {
return "<?php endforeach; ?> ";
});
And in the Blade file I wan to use it like:
#startWire()
#endWire
But I got the following error:
Array to string conversion
I tried to escape the string but nothin seems to be a good choice. Shall I extend the default Blade #foreach directive or how it supposed to work?
Thank you for your answers!
Gábor
Currently I am using $form->error($user_model,'password') this function to show error message which gives me output as <div class="errorMessage">Please enter current password</div> but I want to add id in same div. What changes I have to do for that?
To customize error message you have to go to user model and change/add your message to function rules() array.
function rules() {
return [
['password', 'required', 'message' => 'Your custom message'],
];
}
Check CRequiredValidator class for more information.
To apply some custom ID:
$form->error($error_model, 'password', ['id' => 'My-super-custom-id']);
just it will break your error messages output when using yii-js methods.
In another Yii2 application I used a package called navatech/yii2-roxymce to replace the textarea with HTMl editable box. In the current application I don't want to use it while I want to keep everything easy reusable. In other words, I want a conditional check says that, if the package is installed call it, if not call the ordinary active form textarea.
I have tried class_exists like the following:
<?php
// _form.php code
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use navatech\roxymce\widgets\RoxyMceWidget;
......
<?php if (class_exists('RoxyMceWidget')): ?>
<?= RoxyMceWidget::widget([
'model' => $model, //your Model, REQUIRED
'attribute' => 'content', //attribute name of your model, REQUIRED if using 'model' section
'name' => 'Post[content]', //default name of textarea which will be auto generated, NOT REQUIRED if using 'model' section
'value' => isset($_POST['Post']['content']) ? $_POST['Post']['content'] : $model->content, //default value of current textarea, NOT REQUIRED
'action' => Url::to(['roxymce/default']), //default roxymce action route, NOT REQUIRED
'options' => [//TinyMce options, NOT REQUIRED, see https://www.tinymce.com/docs/
'title' => 'RoxyMCE',//title of roxymce dialog, NOT REQUIRED
'height' => 450,
],
]);?>
<?php else: ?>
<?= $form->field($model, 'content')->textarea(['rows' => 14]);?>
<?php endif; ?>
.....
However, after the installation of navatech\roxymce\widgets\RoxyMceWidget using composer, the conditional statement gives the same result. i.e printing the ordinary activeform text area, so class_exists seems to always return false inspite off the widget is being installed.
Is there any other right way to check if a package is found or not?
You must provide fully qualified namespace for the class.
class_exists('navatech\roxymce\widgets\RoxyMceWidget')
with prs4 you must include namespace:
class_exists('navatech\roxymce\widgets\RoxyMceWidget')
or:
class_exists(RoxyMceWidget::className()) if it already defined in the use statement.
updated: you should use lastest version of yii2-roxymce, current is 2.0.0.1
I am using Yii Framework 2.0. I have a form with a text input field which is meant for a date. I have read the Yii Framework 2.0 about the Class yii\validators\Validator and known all the validator keys which can be used inside of the rules() method in a model class. When I use the date key as below, it does not validate anything. It means that I still can put some text in that input field and can post the form.
When I changed it into boolean or email, I could see that it validates very well when I put something wrong in the input field. How can I validate a date value inside of an input field with Yii Framework 2.0?
My rules() method:
public function rules()
{
return [
[['inputfield_date'], 'required'],
[['inputfield_date'], 'safe'],
[['inputfield_date'], 'date'],
];
}
My view page:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'inputfield_date')->textInput(); ?>
<?php ActiveForm::end(); ?>
Boy the Yii docs suck. They don't even give an example. Working from O'Connor's answer, this worked for me since I was assigning the value in 2015-09-11 format.
// Rule
[['event_date'], 'date', 'format' => 'php:Y-m-d']
// Assignment
$agkn->event_date = date('Y-m-d');
The docs don't even specify where format or timestampAttribute came from, or how to use them. It doesn't even say what the heck from_date and to_date are. And most of all, no examples!
Working solution. My rules() method:
public function rules()
{
return [
[['inputfield_date'], 'required'],
[['inputfield_date'], 'safe'],
['inputfield_date', 'date', 'format' => 'yyyy-M-d H:m:s'],
];
}
My form in the view page:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'inputfield_date')->textInput(); ?>
<?php ActiveForm::end(); ?>
My method in controller:
if ($model->load(Yii::$app->request->post()) && $model->validate()):
if($model->save()):
// some other code here.....
endif;
endif;
Note that the date format depends on how you define your date format input field. Note once again that this is not an AJAX validator. After clicking on the submit button, you will see the error message if you enter something else which is not a date.
You can validate for date like this from model rules
public function rules(){
return [
[['date_var'],'date', 'format'=>'d-m-yy'],
[['from_date', 'to_date'], 'default', 'value' => null],
[['from_date', 'to_date'], 'date'],
];
}
Not 100% sure how it is in Yii2, but going out from Yii1 that had to look like this...
array('org_datetime', 'date', 'format'=>'yyyy-M-d H:m:s'),
(source: http://www.yiiframework.com/wiki/56/#hh8)
...I'd say i'd have to look something like this in Yii2:
['shopping_date', 'date', 'format' => 'yyyy-M-d H:m:s'],
Firstly, date validation relies on $model->validate(), so does not work inline but is only triggered by clicking Save (i.e. it's typically applied in actionCreate/actionUpdate).
Secondly, it's applied after other rules like 'required', and only if they pass, so even when you click Save, if anything else fails validation, those errors will show, but the date error will not.
In summary, date validation can only be tested by ensuring that all other rules pass and then clicking Save.