Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 733 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Cakephp 3 display true/false for booleans

#1
Using cakephp 3, I have a boolean [tinyint(1)] in a table, and the edit and add templates have a check box on the form, but how do I get the index and view templates to display a string like true/false or yes/no instead of 1/0. Do I map them over in the controller actions, or is there a option I can add to the templates?
Reply

#2
simply:

<?= ($var)?'yes':'no' ?>
Reply

#3
When you go in to display the data you can choose to display a string instead of the int. This is a simplified approach since you didn't provide any code or other information:

In the view, $isTrue being the boolean:

<?php if($isTrue){echo "true";}else{echo "false";} ?>
Reply

#4
The answers given both work fine.

I created a Helper class in `/src/View/Helper/FormatBooleanHelper.php` as below:

<?php
/*
* FormatBooleanHelper
*
* CakePHP Helper created to format boolean values in either Yes/No or True/False text.
* By: Jared Testa
*
*/

namespace App\View\Helper;

use Cake\View\Helper;

class FormatBooleanHelper extends Helper
{

/**
* yesNo method
*
* @param boolean| $value boolean
* @return string
*
*/

public function yesNo($value, $showNo = true) {
return ($value) ? "Yes" : (($showNo) ? "No" : "");
}

/**
* trueFalse method
*
* @param boolean| $value boolean
* @return string
*
*/

public function trueFalse($value, $showFalse = true) {
return ($value) ? "True" : (($showFalse) ? "False" : "");
}

}

?>

The helper is used in the standard convention by including `$this->loadHelper('FormatBoolean');` in your initialize method in the AppView.php file.

You then use the Helper in your view by including `$this->FormatBoolean->yesNo($booleanVariable)` or `$this->FormatBoolean->yesNo($booleanVariable, false)` in your view. The latter example will leave the field blank in a false response.

Overkill? Perhaps...but I think it fits into the CakePHP structure, and it was a good exercise in creating your own helper.

I'm using the helper in CakePHP version 3.3.4. Hope this helps someone in the future.
Reply

#5
I added (for bottom, jtesta answer) method to get graphically representation - "check" or "x" using Foundation Icon Fonts 3:

public function checkX($value, $showFalse = true) {
return ($value) ? '<i class="fi-check"></i>' : (($showFalse) ? '<i class="fi-x"></i>' : '');
}

[Image how it loks like][1]


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through