Commit 57394037 authored by Avris's avatar Avris

Choice objects

minor fixes
parent 82e20fd0
Pipeline #9351568 passed with stage
in 16 seconds
......@@ -3,9 +3,17 @@ namespace Avris\Micrus\Forms\Assert;
class Choice extends Assert
{
/** @var array */
protected $choices;
/** @var bool */
protected $multiple;
/**
* @param array $choices
* @param bool $multiple
* @param string|false $message
*/
public function __construct($choices, $multiple, $message = false)
{
$this->choices = $choices;
......
......@@ -13,7 +13,7 @@ class MaxLength extends Assert
public function validate($value)
{
return strlen($value) > $this->max ? $this->message : true;
return mb_strlen($value) > $this->max ? $this->message : true;
}
public function getHtmlAttributes()
......
......@@ -13,7 +13,7 @@ class MinLength extends Assert
public function validate($value)
{
return strlen($value) < $this->min ? $this->message : true;
return mb_strlen($value) < $this->min ? $this->message : true;
}
public function getHtmlAttributes()
......
......@@ -11,7 +11,7 @@ class BootstrapNoLabel extends FormStyle
'<label for="{id}" class="{label_class}">{label}</label>' => '',
'{extra}' => $widget->getOption('placeholder')
? ''
: 'placeholder="' . htmlentities($widget->getOption('label') ?: ucfirst($widget->getName())) . '"',
: 'placeholder="' . htmlentities($widget->getLabel()) . '"',
'{wrapper_before}' => '<div class="row"><div class="col-lg-12">',
'{wrapper_after}' => '</div></div>',
'{widget_before}' => '',
......
<?php
namespace Avris\Micrus\Forms\Widget;
use Avris\Bag\BagHelper;
use Avris\Micrus\Forms\Assert as Assert;
use Avris\Micrus\Forms\Form;
use Avris\Micrus\Forms\FormObject;
......@@ -30,8 +31,8 @@ class Choice extends Widget implements WhenNotSetInRequest
}
$multiple = (bool) $this->options->get('multiple');
if ($multiple && !is_array($widgetValue)) {
$widgetValue = [];
if ($multiple) {
$widgetValue = BagHelper::toArray($widgetValue);
}
return $this->options->get('expanded')
......@@ -139,6 +140,7 @@ class Choice extends Widget implements WhenNotSetInRequest
if (!$value) {
return $this->options->get('multiple') ? [] : null;
}
if ($this->options->get('multiple')) {
return $this->fetchService('orm')->findBy($model, 'id', $value);
}
......@@ -146,6 +148,27 @@ class Choice extends Widget implements WhenNotSetInRequest
return $this->fetchService('orm')->find($model, $value);
}
if ($this->options->get('objects')) {
if (!$value) {
return $this->options->get('multiple') ? [] : null;
}
$choices = $this->options->get('choices');
if ($this->options->get('multiple')) {
$objects = [];
foreach ($value as $item) {
if (isset($choices[$item])) {
$objects[$item] = $choices[$item];
}
}
return $objects;
}
return isset($choices[$value]) ? $choices[$value] : null;
}
return $value;
}
......@@ -160,8 +183,10 @@ class Choice extends Widget implements WhenNotSetInRequest
foreach ($value as $el) {
$ids[] = FormObject::get($el, 'id');
}
return $ids;
}
return FormObject::get($value, 'id');
}
......
......@@ -4,13 +4,11 @@ namespace Avris\Micrus\Forms;
use App\Model\Testie;
use Avris\Bag\Bag;
use Avris\Micrus\Bootstrap\Container;
use Avris\Micrus\Controller\Http\Request;
use Avris\Micrus\Controller\Http\RequestInterface;
use Avris\Micrus\Exception\InvalidArgumentException;
use Avris\Micrus\Forms\Widget\Text;
use Avris\Micrus\Model\NoORM;
use Avris\Micrus\Test\TestForm;
use Avris\Micrus\Localizator\String\LocalizedString;
use Avris\Micrus\Forms\Style\Bootstrap;
class FormIntegrationTest extends BaseTest
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment