Skip to content

Commit

Permalink
issue #34
Browse files Browse the repository at this point in the history
By default password field required to fill if model not exists. If you want to change it, you can fill password field otherwise leave field empty.

Or set  `AdminFormElement::password('password', 'Password')->allowEmpty()` to allow check password validation rules by each model updating
  • Loading branch information
butschster committed Mar 14, 2016
1 parent bd32f77 commit 235e292
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Form/Element/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,57 @@

class Password extends NamedFormElement
{
/**
* @var bool
*/
protected $allowEmpty = false;

public function save()
{
$value = $this->getValue();

if (! $this->isAllowedEmptyValue() and $this->getModel()->exists() and empty($value)) {
return;
}

parent::save();
}

/**
* @return array
*/
public function getValidationRules()
{
$data = parent::getValidationRules();

if (! $this->isAllowedEmptyValue() and $this->getModel()->exists()) {
foreach ($data as $field => $rules) {
foreach ($rules as $i => $rule) {
if ($rule == 'required') {
unset($data[$field][$i]);
}
}
}
}

return $data;
}

/**
* @return boolean
*/
public function isAllowedEmptyValue()
{
return $this->allowEmpty;
}

/**
* @return $this
*/
public function allowEmptyValue()
{
$this->allowEmpty = true;

return $this;
}
}

0 comments on commit 235e292

Please sign in to comment.