/** * @param object $object * @param string $fieldName * @param BaseControl $control * @param IClassMetadata $classMetadata * @throws \NForms\Exceptions\UnexpectedTypeException * @return bool */ public function save($object, $fieldName, BaseControl $control, IClassMetadata $classMetadata) { if ($control->isOmitted() || $control->isDisabled()) { return TRUE; } $value = $control->getValue(); if ($classMetadata->hasAssociation($fieldName) && $value !== NULL) { if ($classMetadata->isSingleValuedAssociation($fieldName)) { $value = $this->objectManager->find($classMetadata->getAssociationTargetClass($fieldName), $value); } else { if (!is_array($value) && (!$value instanceof \ArrayAccess || !$value instanceof \Iterator)) { throw new UnexpectedTypeException("In mapping association {$classMetadata->getClass()}::\${$fieldName} - expected array or ArrayAccess and Iterator instance, given " . get_class($value) . "."); } $collection = array(); foreach ($value as $id) { $collection[] = $this->objectManager->find($classMetadata->getAssociationTargetClass($fieldName), $id); } $value = $collection; } } if ($classMetadata->hasAssociation($fieldName)) { $classMetadata->setAssociationValue($object, $fieldName, $value); } else { if ($control instanceof Nette\Forms\Controls\TextBase && $value === '') { $value = NULL; } $classMetadata->setFieldValue($object, $fieldName, $value); } return TRUE; }
public function toPropertyValue(\Nette\Forms\Controls\BaseControl $control, Builder\Metadata $metadata) { $value = $control->getValue(); if ($value !== null) { $value = $metadata->type === 'float' ? (double) $value : (int) $value; } return $value; }
/** * Returns control's value. * * @return int|\DateTime */ public function getValue() { if ($this->type === self::DATETIME) { return parent::getValue(); } else { return parent::getValue()->getTimestamp(); } }
private function addNominalDiscountControl(BaseControl $priceControl) { $errorMessage = 'Nominal discount must be between 0 and original price.'; $control = $this->addText('nominalDiscount', 'Nominal discount'); $control->setType('number')->setAttribute('step', 'any')->setDefaultValue(0)->addRule(self::FLOAT, $errorMessage)->addRule(function (TextInput $input) use($priceControl) { return $input->getValue() >= 0 && $input->getValue() < $priceControl->getValue(); }, $errorMessage); if ($this->editedProduct !== null) { $control->setDefaultValue($this->editedProduct->getNominalDiscount()); } }
/** * @return mixed */ public function getRawValue() { return parent::getValue(); }
public function toPropertyValue(\Nette\Forms\Controls\BaseControl $control, Builder\Metadata $metadata) { return $control->getValue(); }