How to Set Raw, html text for checkbox – radio labels when used symfony form builder

As default, Symfony 4 does not allow the set raw label text for form elements. I needed this when I want to show terms of use checkbox in a form. I found a workaround solution to accomplish this.

 

FormType Source Code:

/* ... */

class RegistrationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('termsOfUse', CheckboxType::class, [
            'mapped' => false,
            'label' => 'I agree and accept #TERMS_OF_USE_LINK#',
            'constraints' => new IsTrue(),
        ]);

        /* ... */
    }
}


Twig Template Source Code

{{ form_start(form) }}

{% set termsOfUseFormGroup %}
    {{ form_row(form.termsOfUse) }}
{% endset %}

{{ termsOfUseFormGroup|replace({'#TERMS_OF_USE_LINK#':'<a href="#">Terms of Use</a>'})|raw }}

{{ form_end(form) }}

To explain briefly, I set rendered terms of use form group content (which is {{ form_row(form.termsOfUse) }}) to a variable (which is termsOfUseFormGroup) and replace #TERM_OF_USE_LINK# string to a link.

How to create a cookie which never expire using Codeigniter

Maximum value of cookie expiration time is 2147483647 (2^31 – 1 = 2147483647 = 2038-01-19 04:14:07) [see: RFC 2616, 14.6 Age]

So the example code snippet:

// substracting current time from last exp. time because CI adds current time to the value
$unexpired_cookie_exp_time = 2147483647 - time();

$cookie = array(
	'name' => 'cookie_name',
	'value' => 'cookie_value',
	'expire'=> $unexpired_cookie_exp_time
);

$CI->input->set_cookie($cookie);

 

Form validation example using CodeIgniter

Example is about simply user creation.

Controller file(user.php)

<?php

/**
 * User controller class
 */
class User extends CI_Controller
{

	public function create()
	{
		$data=['title'=>'Create New User'];
		$this->load->library('form_validation');

		// validation rules
		$rules=array(
			array(
				'field'=>'username',
				'label'=>'Username',
				'rules'=>'required|min_length[6]',
			),
			array(
				'field'=>'password',
				'label'=>'Password',
				// user-defined validation rule via method
				'rules'=>'required|callback_checkPasswordStrength',
			),
			array(
				'field'=>'passwordConfirm',
				'label'=>'Password Confirm',
				'rules'=>'required|matches[password]',
			),
		);
		$this->form_validation->set_rules($rules);

		// failed
		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('user/create',$data);
		}
		// successfull
		else
		{
			$attrs=$this->input->post(null,true);
			$this->load->view('user/success');
		}
	}

	// user-defined validation rule via method
	public function checkPasswordStrength($password)
	{
		if (strlen($password)<5) {
			$this->form_validation->set_message('checkPasswordStrength','Password length cant be less than 5 characters');
			return false;
		}
		else if (!preg_match('/[[:punct:]]/',$password)) {
			$this->form_validation->set_message('checkPasswordStrength','Password must contain at least 1 special character');
			return false;
		}
		return true;
	}
}

View file(create.php)

<ul>
	<?php echo validation_errors('<li class="error">','</li>'); ?>
</ul>

<form action="/index.php/user/create" method="post">
	<label>Username:</label>
	<input type="text" name="username" value="<?php echo set_value('username'); ?>" />
	<?php echo form_error('username','<div class="error">','</div>'); ?>
	<br />

	<label for="">Password:</label>
	<input type="password" name="password" value="<?php echo set_value('password'); ?>" />
	<?php echo form_error('password','<div class="error">','</div>'); ?>
	<br />

	<label for="">Password Confirm:</label>
	<input type="password" name="passwordConfirm" value="<?php echo set_value('passwordConfirm'); ?>" />
	<?php echo form_error('passwordConfirm','<div class="error">','</div>'); ?>
	<br />

	<input type="submit" name="submit" value="Create" />
</form>

Yii: Eksik Tercüme Mesajlarını Tespit Eden Betik (how to detect missing translation messages)

Nedir?

Bu betik, Yii framework ile geliştirilmiş projelerde, belli dile ait eksik tercüme mesajlarını tespit eder ve eksik olanları @@@ olarak işaretler.

Parametreler

Parametre
Zorunlu mu?
Örnek
Açıklama
yii-msg-dir-path Evet –yii-msg-dir-path=”/var/www/html/panel/protected/messages” Projede “messages” dizin yolu
correct-lang-dir-name Evet –correct-lang-dir-name=”tr” Tam tercüme mesajlarını içeren dilin dizin adı
missing-lang-dir-name Evet –missing-lang-dir-name=”de” Eksik tercüme mesajlarını içeren dilin dizin adı
overwrite Hayır –overwrite

Eksik tercüme mesajları içeren dosyanın üzerine yazmasını sağlar

*** varsayılanda üzerine yazmaz; .new uzantılı dosyaya yazar

 

Örnek Kullanım

$ php missing-yii-translation-detector.php --yii-msg-dir-path="/var/www/html/panel/protected/messages" --correct-lang-dir-name="tr" --missing-lang-dir-name="de" --overwrite

Betik Dosyasını İndir

İndir: missing-yii-translation-detector.php

Betik Kodları

<?php

/**
 * Detects missing Yii translation messages based on language.
 *
 * - Needs write permission for apache to create target language-spec. directory OR
 * 	create yourself.
 *
 * @author Erman Gülhan
 * @date 2014-06-03
 */

$args=getopt('',array('yii-msg-dir-path:','correct-lang-dir-name:','missing-lang-dir-name:','overwrite'));

if(empty($args['yii-msg-dir-path']) || empty($args['correct-lang-dir-name']) || empty($args['missing-lang-dir-name']))
	showUsage();

$yiiMsgsDir=$args['yii-msg-dir-path'];
$correctLangDir=$args['correct-lang-dir-name'];
$missingLangDir=$args['missing-lang-dir-name'];
$overwrite=isset($args['overwrite']) ? true : false;
$targetFileSuffix='new';

$msgLinePatt="/^\s*['\"](.*)['\"]\s*=>/";
$_sourceDir=$yiiMsgsDir.'/'.$correctLangDir;
$_targetDir=$yiiMsgsDir.'/'.$missingLangDir;

// Needs write permission for apache
if(!file_exists($_targetDir))
	mkdir($_targetDir);

foreach(glob($_sourceDir.'/*.php') as $file)
{
	$curFileName=pathinfo($file);
	$curFileName=$curFileName['basename'];
	$_targetFile=$_targetDir.'/'.$curFileName;
	$isExist=file_exists($_targetFile);
	$_targetMsgArr=$isExist ? include($_targetFile) : array();
	$handle=fopen($file,'r');
	$targetFileContent='';
	while(($line=fgets($handle,4096))!==false)
	{
		if(preg_match($msgLinePatt,$line,$m))
		{
			$msgKey=$m[1];
			$targetContent=array_key_exists($msgKey,$_targetMsgArr)
				? "'$msgKey'=>'".$_targetMsgArr[$msgKey]."',"
				: "'$msgKey'=>'@@@',";
			$targetContent="\t".$targetContent.PHP_EOL;
		}
		else
			$targetContent=$line;

		$targetFileContent.=$targetContent;
	}

	$newFile=$isExist && !$overwrite ? $_targetFile.'.'.$targetFileSuffix : $_targetFile;
	file_put_contents($newFile,$targetFileContent);
}


function showUsage()
{
	echo PHP_EOL.'USAGE: '.PHP_EOL;
	echo basename(__FILE__).' --yii-msg-dir-path="/var/www/html/panel/protected/messages" --correct-lang-dir-name="tr" --missing-lang-dir-name="de" --overwrite'.PHP_EOL.PHP_EOL;
	echo '*** overwrite parameter is optional.'.PHP_EOL.PHP_EOL;
	exit;
}

?>

Php: Highcharts Grafiklerini Yerelden Export Yaptırma (Export Highcharts Graphics From Local)

Highchart grafiklerinin sunduğu export özelliği varsayılan olarak grafiklerin highchart’ın kendi sitesinden yapılmasını sağlıyor.  Export özelliğini yerelden çalışır hale getirebiliriz.

 

Gereksinimler:

 

Dosya yapısı:

/webroot

index.php

js/highcharts/

exportGraph/

export.php

temp/

batik/

batik-rasterizer.jar

Yapılacaklar:

  1. export.php’deki batik-rasterizer.jar dosyasının yolu tanımlanlamı:
    define ('BATIK_PATH', 'batik/batik-rasterizer.jar')

     

  2. highcharts’ın exporting.js dosyasında aşağıdaki değişiklik yapılmalı:window.location.hostname yi bul ve şu şekilde değiştir: window.location.hostname+”exportGraph/export.php”
  3. Export işleminde temp dizini olarak kullanılacak dizine apache için yazma izni verilmeli:
     chgrp apache -R exportGrph/temp/
     chmod g+w -R exportGrph/temp/