Skip to content

Commit

Permalink
Some refactoring to the Form class
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtlambda committed May 20, 2016
1 parent bf3aca0 commit 17c7352
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/fieldwork/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,23 @@ public function getScriptHTML ()
return $openingTag . $this->getScript() . $closingTag;
}

public function renderFormError ($errorMsg)
static public function renderFormError ($errorMsg)
{
return sprintf("<div class=\"form-error card red white\"><div class=\"card-content red-text text-darken-4\">%s</div></div>", $errorMsg);
}

/**
* Get all the error messages for form-wide validators that returned invalid
*/
public function getFormErrors ()
{
return array_map(function (FormValidator $formValidator) {
return $formValidator->getErrorMsg();
}, array_filter($this->validators, function (FormValidator $validator) {
return !$validator->isValid();
}));
}

/**
* Gets the markup that is to be outputted before the actual contents of the form. This method could be used for
* even more manual control over outputting with a custom markup.
Expand All @@ -264,11 +276,9 @@ public function renderFormError ($errorMsg)
public function getWrapBefore ()
{
$dataFields = $this->getDataFieldsHTML();
$errors = '';
foreach ($this->validators as $validator)
/* @var $validator FormValidator */
if (!$validator->isValid())
$errors .= $this->renderFormError($validator->getErrorMsg());
$errors = join(array_map(function ($errorMsg) {
return Form::renderFormError($errorMsg);
}, $this->getFormErrors()));
return "<form " . $this->getAttributesString() . ">" . $errors . $dataFields;
}

Expand Down Expand Up @@ -469,7 +479,7 @@ function describeObject ()
/**
* Gets a complete associated array containing all the data that needs to be stored
*
* @param bool $useName Whether to use the field name (if not, the fields local slug is used)
* @param bool $useName Whether to use the field name (if not, the fields shorter local slug is used)
* @param bool $includeDataFields Whether to include the data fields
*
* @return array
Expand Down Expand Up @@ -521,4 +531,4 @@ public function getInnerHTML ()
{
return parent::getHTML();
}
}
}

0 comments on commit 17c7352

Please sign in to comment.