Пример #1
0
 /**
  * Makes this field to an optional.
  *
  * @param Field $field The field object
  *
  * @return Field
  */
 public function optional(Field $field)
 {
     $disp = $field->getDispatcher();
     $disp->addListener(Events::BEFORE_TRANSFORM, function (Event $event) {
         if (null === $event->getInput()) {
             $event->setData(null);
         }
     });
     return $field;
 }
Пример #2
0
 /**
  * Makes this field to a multiple.
  *
  * @param Field $field The field object
  *
  * @return Field
  */
 public function multiple(Field $proto)
 {
     $resizer = new MultipleResizeListener();
     $name = $proto->getInternalName();
     $disp = new EventDispatcher();
     $field = new Field($name, $disp, $proto->getExtensions());
     $field->setOption('prototype', $proto);
     $disp->addListener(Events::BIND, [$resizer, 'bind']);
     $disp->addListener(Events::FILL, [$resizer, 'fill']);
     return $field;
 }
Пример #3
0
 /**
  * Clones a field.
  *
  * @param Field $field
  *
  * @return Field The cloned field
  */
 protected function cloneField(Field $field)
 {
     $clone = clone $field;
     $clone->setChildren(array_map(function ($child) {
         return $this->cloneField($child);
     }, $field->getChildren()));
     $clone->setConstraints(array_map(function ($constraint) {
         return clone $constraint;
     }, $field->getConstraints()));
     return $clone;
 }
Пример #4
0
 /**
  * Gets the name for form view.
  *
  * @param Field   $field The field object
  * @param boolean $fst   This flag is used to find out which field was 
  *                       called first.
  *
  * @return string
  */
 public function getName(Field $field, $fst = true)
 {
     $parent = $field->getParent();
     if (null !== $parent && '' !== $parent->getName(false)) {
         return $parent->getName(false) . '[' . $field->getInternalName() . ']';
     }
     if ($fst && $field->hasOption('prototype')) {
         return $field->getInternalName() . '[]';
     }
     return $field->getInternalName();
 }
Пример #5
0
 /**
  * Adds a constraint to the field.
  *
  * @param Field    $field   The field object
  * @param string   $message The error message
  * @param callable $check   The check method
  *
  * @return Field
  */
 public function verifying(Field $field, $message, callable $func)
 {
     $field->addConstraint(new \Formz\Constraint\Callback($message, $func));
     return $field;
 }
Пример #6
0
 /**
  * Checks if the field value is boolean.
  *
  * @param Field $field The form field
  * @param string message The error message
  *
  * @return Field
  */
 public function boolean(Field $field, $message = 'formz.error.boolean')
 {
     $field->addConstraint(new \Formz\Constraint\Boolean($message));
     $field->transform(new \Formz\Transformer\Boolean());
     return $field;
 }
Пример #7
0
 /**
  * Allows to bind the form with Symfony's request object.
  *
  * @param Field   $field   The form field
  * @param Request $request The symfony request object
  *
  * @return Field
  */
 public function bindFromRequest(Field $field, Request $request)
 {
     $field->bind($request->request->all());
     return $field;
 }
Пример #8
0
 /**
  * Adds a child field.
  *
  * @param Field $field The field obejct
  */
 public function addChild(Field $child)
 {
     $this->children[$child->getInternalName()] = $child;
 }