Custom ajax validation for text length doesn't work yii2 - yii2

I am trying to check the length of the text entered but without success. It is working with the required rule because i get the field is empty error but not with mine validation. My custom rule works only on form submit. Also tried to enable ajax validation of the form but again nothing.
public function rules()
{
return [
[['author_id', 'title', 'review'], 'required'],
[['author_id'], 'integer'],
[['review'], 'string'],
[['review'], function($attribute, $params){
if(strlen($this->$attribute) < 10){
$this->addError($attribute, 'The review is too short! Minimum 10 symbols!');
}
}],
[['review'], 'trim'],
[['dt'], 'safe'],
[['title'], 'string', 'max' => 255],
[['author_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['author_id' => 'id']],
[['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => News::className(), 'targetAttribute' => ['post_id' => 'id']],
];
}
My form:
<?php $form=\yii\bootstrap\ActiveForm::begin([
'method' => 'post',
'options' => [
'id' => 'textarea_' . $model->id . '',
'class' => "textarea_review"
],
]) ?>
<input type="hidden" name="flag" value="1"/>
<input type="hidden" name="model_id" value="<?= $model->id ?>"/>
<?= $form->field($model, 'review')->textarea(['id'=>'update_text_'.$model->id.''])->label(false) ?>
<?= $form->field($model, 'csrf_token')->hiddenInput(['value' => $session['token']])->label(false) ?>
<?= Html::button('Изпрати', ['onclick'=>'editComment('.$model->id.')', 'class'=>'btn btn-primary send-button']) ?>
<?php \yii\bootstrap\ActiveForm::end() ?>
Thank you in advance!

perhaps skipping the inline validator and defining the string rule as follows is the best solution for you:
[['review'], 'string', 'max' => 10, 'message' => 'The review is too short! Minimum 10 symbols!']
If you absolutelty need a custom validator, the second best option is to
use ajax validation.
If neither of the above suit you, the you won't get away with just writing php validation rules.
you need to provide client side script to implement the same validation logic in browser.
Either define a custom validator class and override clientValidateAttribute()
or you may specify clientValidate property to the inline validator you're using in your custom rule.
Make sure you follow the distinction between yii\validators\InlineValidatorand yii\validators\Validator when reading trough the docs

For client-side validation you have to set whenClient property aswell, where you put javascript validation.
Here docs: Client Side Validation

Related

Remove encoding for popovers on labels

I'm using Yii2 to generate a popover on a label, but having some trouble to remove the default HTML encoding. I'm not sure that the popover can be created for just the label without HTML encoding and what the correct way to do this is, it must be possible though as Gii uses some variant of this code? This is what I've tried:
<?= $form->field($model, 'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
'encodeLabel'=> false]) ?>
Use
['labelOptions' => ['encode' => false]]
.
<?= $form->field($model, ['labelOptions' => ['encode' => false]] ,
'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
) ?>
you can use label option for setting the encode false of label attribute
<?= $form->field($model,
'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
'encode' => false,
) ?>

Yii2 Redactor is not loaded after ajax/pjax request

I use redactor for editing comment. There are multiple redactors since there are multiple comments. Most of the time, redactor is loaded, but sometimes the redactor is not loaded and only generate normal text editor.
These comments are loaded after Pjax Request
My code:
<?= \yii\redactor\widgets\Redactor::widget([
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>
Problem solved:
You have to put ID :)
<?= \yii\redactor\widgets\Redactor::widget([
'id' => 'edit_redactor_' . $comment_id,
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>

Yii2 pjax search save param

I have some filter per-page and gridview.
They placed in pjax.
When i change some pagination and sort and after that use my per-page filter - params from pagination and sort are lost.
I want to save it. How to do that?
My code:
<?php Pjax::begin(['id' => 'pjax']) ?>
<?= Html::dropDownList('per-page', Yii::$app->request->get('per-page') != NULL ? Yii::$app->request->get('per-page') : [10 => 10], [1 => 1, 5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100], [
'onchange' => '
$.pjax.reload({
url: "'.Url::to([Yii::$app->controller->action->id, 'id' => Yii::$app->request->get('id')]).'?StandartSearch%5Bdate%5D=" + $(\'#date\').val() + "&per-page=" + $(this).val(),
container: "#pjax",
});
',
'class' => 'form-control',
'id' => 'per-page'
]) ?>
<?= GridView::widget([
'id' => 'gridview',
'dataProvider' => $dataProvider,
'columns' => [
// columns
],
]); ?>
<?php Pjax::end() ?>
Per-page filter are placed within pjax container. So obviously they will get lost when pjax event is completed.
To make them not to lost their current selection, you can place per page filter in the form outside pjax.
and $formSelector property on pjax which points to that filter form id.
reference: http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html#$formSelector-detail

Yii 2 Editable, how to use editable inside form?

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?
You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.

Customizing ckeditor to accept html and body tags using yii2-ckeditor-widget

I am using 2 amigos ckeditor widget
<?= $form->field($model, 'text')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'basic'
]) ?>
How do I add the configuration settings, I want the editor to accept HTML tags hTML and body which the editor usually stripes off. Where do i specify this setings in the widget.
There is special property called clientOptions for setting plugin options.
For filtering tags use allowedContent option, you can read official docs here.
Here is an example of code:
<?= $form->field($model, 'text')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'basic',
'clientOptions' => [
'allowedContent' => ...,
],
]) ?>
So i added the configuration 'allowedContent' => true and it worked.