示例#1
0
 /**
  * Helper to build the entity form.
  * 
  * @param Admin      $app
  * @param string     $entityName
  * @param mixed|null $id
  * 
  * return \Folk\Formats\Form
  */
 protected static function createForm(Admin $app, $entityName, $id = null)
 {
     $entity = $app->getEntity($entityName);
     $form = F::form()->method('post')->enctype('multipart/form-data')->add(['entity' => F::hidden()->val($entityName)->class('field-data-entity'), 'data' => $entity->getScheme($app['builder'])]);
     if ($id !== null) {
         $form['id'] = F::hidden()->val($id)->class('field-data-id');
     }
     return $form;
 }
示例#2
0
 /**
  * @depends testValue
  */
 public function testTemplate(Collection $field)
 {
     $form = Builder::form();
     $form['key'] = $field;
     $template = $field->getTemplate();
     $template['name']->id('my-id');
     $template['name']->label->id('my-label-id');
     $this->assertEquals('<label id="my-label-id" for="my-id">Name</label> <input type="text" id="my-id" name="key[::n::][name]" aria-labelledby="my-label-id"> ', (string) $template['name']);
 }
示例#3
0
 public function testFieldsets()
 {
     $form = Builder::form(['submit' => Builder::submit()])->fieldsets(['personal' => ['name' => Builder::text(), 'surname' => Builder::text(), 'age' => Builder::number()]]);
     $this->assertCount(4, $form);
     $this->assertCount(1, $form->fieldsets());
     $this->assertCount(3, $form->fieldsets()['personal']);
     $form->clear()->add(['other' => Builder::text()]);
     $this->assertCount(1, $form);
 }
示例#4
0
 public function testValidation()
 {
     $form = Builder::form(['name' => Builder::text()->maxlength(200)->label('Name'), 'email' => Builder::email()->label('Email'), 'password' => Builder::password()->label('Password'), 'repeat_password' => Builder::password()->label('Repeat password')])->addValidator(function ($form) {
         $password1 = $form['password']->val();
         $password2 = $form['repeat_password']->val();
         if ($password1 != $password2) {
             throw new InvalidValueException('The passwords does not match');
         }
     });
     $form->val(['name' => 'Oscar', 'email' => '*****@*****.**', 'password' => '1234', 'repeat_password' => '12345']);
     $this->assertFalse($form->isValid());
     $this->assertEquals('The passwords does not match', $form->error());
     $this->assertCount(1, $form->getElementsWithErrors());
     $form['repeat_password']->val('1234');
     $this->assertTrue($form->isValid());
     $this->assertNull($form->error());
     $this->assertCount(0, $form->getElementsWithErrors());
 }