Example #1
0
 public function testForm()
 {
     $form = new Form();
     $form->action('index.php')->method('post');
     $form->add(['name' => Field::text()->maxlength(50)->required()->label('Your name'), 'email' => Field::email()->label('Your email'), 'telephone' => Field::tel()->label('Telephone number'), 'gender' => Field::choose(['m' => Field::radio()->label('Male'), 'f' => Field::radio()->label('Female')]), 'born' => Field::collection(['day' => Field::number()->min(1)->max(31)->label('Day'), 'month' => Field::number()->min(1)->max(12)->label('Month'), 'year' => Field::number()->min(1900)->max(2013)->label('Year')]), 'language' => Field::select()->options(array('gl' => 'Galician', 'es' => 'Spanish', 'en' => 'English'))->label('Language'), 'friends' => Field::duplicable(['name' => Field::text()->label('Name'), 'email' => Field::email()->label('email'), 'age' => Field::number()->label('Age')]), 'action' => Field::choose(['save' => Field::submit()->html('Save changes'), 'duplicate' => Field::submit()->html('Save as new value')])]);
     $data = array('name' => 'Manuel', 'email' => null, 'telephone' => null, 'gender' => 'm', 'born' => array('day' => 23, 'month' => 12, 'year' => 2013), 'language' => 'gl', 'friends' => array(array('name' => 'Luis', 'email' => '*****@*****.**', 'age' => 30)), 'action' => 'save');
     $form->val($data);
     $this->assertTrue($form->isValid());
     $form->addValidator('myValidator', function ($form) {
         $val = $form['name']->val();
         return empty($val) ? true : 'The name value must be empty';
     });
     $this->assertFalse($form->isValid());
     $this->assertEquals('The name value must be empty', $form->error());
 }
Example #2
0
 /**
  * @param string     $id
  * @param Store      $store
  * @param Translator $i18n
  * @param array      $countries
  * @param array      $paymentMethods
  * @param string     $tosUrl
  */
 public function __construct($id, $store, $i18n, $countries = array(), $paymentMethods = array(), $tosUrl = '')
 {
     $this->id = $id;
     $this->store = $store;
     $this->updateLocale($i18n);
     $this->attr(['action' => '', 'method' => 'POST']);
     $this->add(['modeToCart' => Field::submit()->val('toCart')->html($i18n->translate('button.backToCart'))->render(function ($e) {
         // set name to avoid setting name from form
         $e->attr(['name' => 'mode']);
         return '<div class="cartButtonBar">' . $e . '</div>';
     }), 'customer' => Field::group(['email' => Field::email()->required()->label($i18n->translate('checkoutForm.customer.email') . ' *'), 'telephone' => Field::tel()->label($i18n->translate('checkoutForm.customer.tel')), 'paymentMethod' => Field::select()->options($paymentMethods)->label($i18n->translate('checkoutForm.paymentMethod'))])->render(function ($group) {
         return '<div class="group customer"><h3>' . $group->label . '</h3>' . $group->childrenToHtml() . '</div>';
     })->label($i18n->translate('checkoutForm.customer.title')), 'billing' => $this->createAddress($countries, $i18n)->label($i18n->translate('checkoutForm.billingAdr.title')), 'shipping' => $this->createShippingAddress($countries, $i18n)->label($i18n->translate('checkoutForm.shippingAdr.title')), 'extra' => Field::group(['comment' => Field::textarea()->rows(8)])->render(function ($group) {
         return '<div class="group"><h3>' . $group->label . '</h3>' . $group->childrenToHtml() . '</div>';
     })->label($i18n->translate('checkoutForm.customer.comment')), 'accept_tos' => Field::checkbox()->required()->label(str_replace('%s', $tosUrl, $i18n->translate('checkoutForm.tosAccept')))->render(function ($field) {
         return "<label class='checkbox small'>{$field->input} " . $field->label() . '</label>';
     }), 'modePayment' => Field::submit()->val('payment')->addClass('primary')->html($i18n->translate('button.doCheckout'))->render(function ($e) {
         // set name to avoid setting name from form
         $e->attr(['name' => 'mode']);
         return '<div class="cartButtonBar">' . $e . '</div>';
     })]);
 }