Permalink
Browse files

Close #65 - Allow population on a form-basis

  • Loading branch information...
1 parent c0aad8a commit 014cad818134ad0ce117ff866ce4998c3bd81501 @Anahkiasen Anahkiasen committed Mar 20, 2013
Showing with 94 additions and 22 deletions.
  1. +2 −1 CHANGELOG.md
  2. +1 −1 src/Former/Dispatch.php
  3. +52 −13 src/Former/Form/Form.php
  4. +17 −0 src/Former/Former.php
  5. +5 −6 src/Former/Traits/Field.php
  6. +17 −1 tests/FormTest.php
View
@@ -13,7 +13,8 @@
- [add] You can also create a group that contains raw content (not a field) with `Former::group('label')->contents('YourContent')`. This will wrap the content in a control class so that your content is aligned with the fields.
- [add] Checkables now handle being populated from relations
- [add] You can now add classes to the group via the `->addGroupClass` method
-- [add] Former::withRules() no also take a Laravel-formatted string of rules (ie. "rule|rule:parameter|rule")
+- [add] Former::withRules() now also take a Laravel-formatted string of rules (ie. "rule|rule:parameter|rule")
+- [add] You can now populate on a form-basis with the chained method `->populate` on a form opener
2.6.0
-----
@@ -41,7 +41,7 @@ public static function toForm(Former $former, $method, $parameters)
// Disregards if the method doesn't contain 'open'
if (!String::contains($method, 'open')) return false;
- $form = new Form\Form($former, $former->getContainer('url'));
+ $form = new Form\Form($former, $former->getContainer('url'), $former->getPopulator());
return $form->openForm($method, $parameters);
}
@@ -2,6 +2,7 @@
namespace Former\Form;
use Former\Former;
+use Former\Populator;
use Former\Traits\FormerObject;
use Underscore\Methods\ArraysMethods as Arrays;
use Underscore\Methods\StringMethods as String;
@@ -33,6 +34,13 @@ class Form extends FormerObject
protected $url;
/**
+ * The Populator
+ *
+ * @var Populator
+ */
+ protected $populator;
+
+ /**
* The Form type
*
* @var string
@@ -92,11 +100,18 @@ class Form extends FormerObject
/////////////////////////// CORE METHODS ///////////////////////////
////////////////////////////////////////////////////////////////////
- public function __construct(Former $former, $url)
+ /**
+ * Build a new Form instance
+ *
+ * @param Former $former
+ * @param UrlGenerator $url
+ */
+ public function __construct(Former $former, $url, Populator $populator)
{
$this->former = $former;
$this->framework = $former->getFramework();
$this->url = $url;
+ $this->populator = $populator;
}
/**
@@ -165,7 +180,7 @@ public static function hasInstanceOpened()
}
////////////////////////////////////////////////////////////////////
- ////////////////////////// CHAINED METHODS /////////////////////////
+ /////////////////////////////// SETTER /////////////////////////////
////////////////////////////////////////////////////////////////////
/**
@@ -205,16 +220,6 @@ public function secure($secure = true)
}
/**
- * Alias for $this->former->withRules
- */
- public function rules()
- {
- call_user_func_array(array($this->former, 'withRules'), func_get_args());
-
- return $this;
- }
-
- /**
* Outputs the current form opened
*
* @return string A <form> opening tag
@@ -237,13 +242,47 @@ public function __toString()
}
////////////////////////////////////////////////////////////////////
+ ////////////////////////// PUBLIC HELPERS //////////////////////////
+ ////////////////////////////////////////////////////////////////////
+
+ /**
+ * Alias for $this->former->withRules
+ */
+ public function rules()
+ {
+ call_user_func_array(array($this->former, 'withRules'), func_get_args());
+
+ return $this;
+ }
+
+ /**
+ * Populate a form with specific values
+ *
+ * @param array|object $values
+ */
+ public function populate($values)
+ {
+ $this->populator->setValues($values);
+ }
+
+ /**
+ * Get the Populator binded to the Form
+ *
+ * @return Populator
+ */
+ public function getPopulator()
+ {
+ return $this->populator;
+ }
+
+ ////////////////////////////////////////////////////////////////////
////////////////////////////// HELPERS /////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Apply various parameters according to form type
*
- * @param string $type The original form type provided
+ * @param string $type The original form type provided
* @return string The final form type
*/
private function applyType($type)
View
@@ -13,42 +13,49 @@ class Former
{
/**
* The current environment
+ *
* @var Illuminate\Container
*/
protected $app;
/**
* The current field being worked on
+ *
* @var Field
*/
protected $field;
/**
* The current form framework
+ *
* @var FrameworkInterface
*/
protected $formFramework;
/**
* The current form being worked on
+ *
* @var Form
*/
protected $form;
/**
* The Populator instance
+ *
* @var Populator
*/
public $populator;
/**
* The form's errors
+ *
* @var Message
*/
protected $errors;
/**
* An array of rules to use
+ *
* @var array
*/
protected $rules = array();
@@ -178,6 +185,16 @@ public function getPost($name, $fallback = null)
return $this->app['request']->get($name, $oldValue);
}
+ /**
+ * Get the Populator binded to Former
+ *
+ * @return Populator
+ */
+ public function getPopulator()
+ {
+ return $this->populator;
+ }
+
////////////////////////////////////////////////////////////////////
////////////////////////////// TOOLKIT /////////////////////////////
////////////////////////////////////////////////////////////////////
@@ -294,17 +294,16 @@ public function getLabel()
*/
private function repopulate($fallback = null)
{
- if(is_null($fallback)) $fallback = $this->value;
-
// Get values from POST, populated, and manually set value
- $post = $this->former->getPost($this->name);
- $populate = $this->former->getValue($this->name);
+ $post = $this->former->getPost($this->name);
+ $populator = $this->form ? $this->form->getPopulator() : $this->former->getPopulator();
+ $populate = $populator->getValue($this->name);
// Assign a priority to each
- if(!is_null($post)) return $post;
+ if(!is_null($post)) return $post;
if(!is_null($populate)) return $populate;
- return $fallback;
+ return $fallback ?: $this->value;
}
/**
View
@@ -39,7 +39,7 @@ public function testCanOpenAClassicForm()
$this->assertHTML($matcher, $open);
}
-
+
public function testCanOpenAFormWithoutAction()
{
$open = $this->former->open()->__toString();
@@ -178,4 +178,20 @@ public function testCanSetNameOnFormOpeners()
$this->assertHTML($matcher, $form);
}
+
+ public function testDifferentFormsCanPopulateDifferentValues()
+ {
+ $this->former->populate(array('foo' => 'eatshitanddie'));
+
+ $this->former->open('#')->populate(array('foo' => 'bar'));
+ $field = $this->former->text('foo');
+ $this->former->close();
+
+ $this->former->open('#')->populate(array('foo' => 'foo'));
+ $fieldTwo = $this->former->text('foo');
+ $this->former->close();
+
+ $this->assertEquals('bar', $field->getValue());
+ $this->assertEquals('foo', $fieldTwo->getValue());
+ }
}

0 comments on commit 014cad8

Please sign in to comment.