How to Unvalidate Some Fields in Yii2
How to Unvalidate Some Fields in Yii2

Validation can be disabled both on the client side (for yiiActiveForm) and on the server side.

Using a scenario will indicate that server-side validation should be skipped for certain fields.

On the client side, validation for specific fields will be dynamically removed depending on the selected action (in our case — delete).

Our controller:

$NewsletterMailForm = new NewsletterMailForm();

if(Yii::$app -> request -> post($NewsletterMailForm -> formName())['event'] != NewsletterMailForm::EVENT_DELETE)
{
	$NewsletterMailForm -> scenario = NewsletterMailForm::EVENT_SEND;
}

If the "event" field is not equal to "delete", it means we are going to send emails (no other actions are handled at this point).

read more...

How to set default parameters for a widget in Yii2
How to set default parameters for a widget in Yii2

To set default parameters for a widget in Yii2, you need to use the dependency injection container.

Example with DatePicker:

Yii::$container -> set('yiijuiDatePicker', [
	'language' => 'en-US',
]);

The first parameter in set is the class name, and the second is the array of parameters to configure. This way, we can preconfigure our widget properly from the start.

Your own Grid columns in the Yii2 framework
Your own Grid columns in the Yii2 framework

Continuing to expand my knowledge of Yii2.

I had, of course, heard about custom columns in Grid, but hadn’t used them in practice. However, when code duplication reached 5 separate places, I thought — why not give it a try? And it turned out to be quite nice, convenient, and most importantly — flexible!

Below is the full code listing of a sample column class that extends DataColumn:

namespace backendgrid;

use Yii;
use yiihelpersHtml;
use yiigridDataColumn;

class ActiveColumn extends DataColumn
{
	public $headerOptions = [
		'style' => 'width: 80px;'
	];
	
	public $contentOptions = [
		'class' => 'text-center'
	];
	
	public $attribute = 'is_active';
	
    /**
     * @inheritdoc
     */
    protected function renderDataCellContent($a)
    {
		if($a['is_active'] == 1)
		{
			return Html::a('<span class="glyphicon glyphicon-ok-circle"></span>', [
					'active', 'id' => $a['id'], 'is_active' => 0], [
					'title' => Yii::t('app', 'Unactivate this'),
					'data-pjax' => '0',
				]
			);
		}
		return Html::a('<span class="glyphicon glyphicon-lock"></span>', [
				'active', 'id' => $a['id'], 'is_active' => 1], [
				'title' => Yii::t('app', 'Activate this'),
				'data-pjax' => '0',
			]
		);
    }
}

read more...

How to find out tomorrow's or yesterday's date in timestamp format in PHP?
How to find out tomorrow's or yesterday's date in timestamp format in PHP?

To get yesterday’s or tomorrow’s date in timestamp format (PHP), you need to use the standard PHP function "strtotime".

For example, let’s display the time of the current date at 00:00:00 (zero hours, zero minutes, zero seconds):

$t = strtotime('00:00:00');
echo 'Timestamp: '.$t;
echo 'Datetime: '.date('Y-m-d H:i:s',$t);

read more...