Ads 4 You

Yii2 when and whenClient validation

Email Id : phpmk888@gmail.com

using 'whenClient' for complex client side validation 
whenClient validation 

 You have a 'Carousel' model with link, image field's When you create a 'Carousel' you choose a 'Image/gif', and follow this rule:

 ['image', 'image', 'minWidth' => 1000, 'minHeight' => 450, 'maxWidth' => 1000, 'maxHeight' => 450]

 And When you Create a 'Carousel' you choose jpg, png, then now follow this rules:

 ['image', 'image']

 In your Carousel model, use this rule:

 


public function rules() {
    return [          
        ['link', 'url'],
        ['image', 'image', 'minWidth' => 1000, 'minHeight' => 450, 'maxWidth' => 1000, 'maxHeight' => 450,
            'when' => function ($model, $attribute) {
                return (isset($model->image->type) && $model->image->type == 'image/gif');
            },
            'whenClient' => "function (attribute, value) {
                var ext = $('#carousel-image').val().split('.').pop().toLowerCase();
                    return (ext=='gif');
                }",
        ],
        [['image'], 'file', 'extensions' => 'gif,jpeg,jpg,png'],
        [['title', 'text', 'link', 'vimeolink'], 'trim'],
        ['status', 'integer'],
        ['status', 'default', 'value' => self::STATUS_ON],
        ['filebase64', 'safe'],
        [['image', 'title', 'text'], 'required', 'on' => 'create'],
        [['title', 'text'], 'required', 'on' => 'update'],                
    ];
}


'when' is server side validation it's call when you submit button (return true if valiation condition) 
AND 
'whenClient' is client side validation it's call when you choose a image file on client side (return true validation condition true).



Comments

  1. Hi Mahesh,

    Could you please make tutorial on yii2 notification.

    Thank you

    ReplyDelete
  2. Interesting blog. It would be great if you can provide more details about it. Thanks you
    Remote Yii Framework Team

    ReplyDelete

Post a Comment