Posiadam moneyFieldset z 2 polami, kwotami i walutą.Dynamicznie zmieniaj wymagany parametr Field Field
class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(...);
$this->add(array(
'name' => 'currency',
'type' => 'select',
'options' => array(
'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
),
'attributes' => array(
'value' => \Core\Service\Money::DEFAULT_CURRENCY,
),
));
$this->add(array(
'name' => 'amount',
'type' => 'text',
));
}
}
public function getInputFilterSpecification()
{
$default = [
'amount' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => AmountFilter::class]
],
'validators' => [
]
],
'currency' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => StringToUpper::class]
],
'validators' => [
]
]
];
return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
}
Używam moneyFieldset w moich innych fieldsets tak:
// Price Field
$this->add(array(
'name' => 'price',
'type' => 'form.fieldset.moneyFieldset',
'attributes' => array(
'required' => true,
'invalidText' => 'Please type an amount'
),
'options' => array(
...
),
));
Kiedy ustawić filtr tak:
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
To nie działa, ponieważ cena ma 2 pola, więc jak mogę powiedzieć cena [kwota] i cena [curreny] jest wymagana?
Próbowałem, ale nic się nie zmieniło. To jest wynik: '". Nie ma parametru "required =" required''. –
Eray
W przypadku zf2, powiedz w inputFilterSpecification, że wymagane jest pole lub grupa pól, nie oznacza to, że wymagany atrybut jest ustawiony w HTML, to tylko do sprawdzania poprawności, gdy wykonasz $ form-> isValid(), sprawdzi wszystkie wymagane pola w ramach walidacjiGrupa – Hooli
Muszę również ustawić wymagany atrybut. Myślałem, że 'price [attributes] [required] = true' spowoduje wygenerowanie' required = required' HTML attribute – Eray