Class: \Conifer\Form\AbstractBase (abstract)
Abstract form base class, encapsulating the Conifer Form API Example usage:
php use Conifer\Form\AbstractBase; class EmployeeForm extends AbstractBase { public function __construct() { $this->fields = [ 'user_login' => [ 'label' => 'Username', 'validators' => [ [$this, 'validate_required_field'], [$this, 'validate_login'] ], ], 'user_email' => [ 'label' => 'Email', 'validators' => [[$this, 'validate_email']], ], 'first_name' => [ 'label' => 'First Name', 'validators' => [ [$this, 'validate_required_field'], [$this, 'validate_name'] ], ], 'last_name' => [ 'label' => 'Last Name', 'validators' => [ [$this, 'validate_required_field'], [$this, 'validate_name'] ], ], ]; } public function validate_email(array $emailField, string $email) { // We can call other pre-defined validators here $valid = $this->validate_required_field($emailField, $email); if ($valid) { // End-user specified an email, but is it valid? $valid = !empty(filter_var($email, FILTER_VALIDATE_EMAIL)); if (!$valid) { // NOPE. $this->add_error($emailField, 'invalid email!'); } } return $valid; } public function validate_name(array $nameField, string $name) { // We can get as simple as we want to! // Here we're just checking that $name matches a certain value. $valid = $name === 'Bob'; if (!$valid) { $this->add_error($nameField, 'only people name Bob are worthy'); } return $valid; } public function validate_login(array $loginField, string $login, array $submission) { // The validate() method passes all submitted data to each validator. // So, if you need to, you can look at other fields: $leet = $login === $submission['name'].'_x1337x'; // Check if the login is cool enough. if (!$leet) { $this->add_error( $loginField, 'You login is not cool enough. It must be your name plus the string "_x1337x".' ); } return $leet; } }
Note that for a given field, "validators" is a list of callbacks. Validators define their own error messaging; that is, if a validator method finds that a field is invalid for some reason, it is responsible for adding an error message for that field. While this is a little more work, it allows for fine-grained control over the order in which fields are validated, and the messages that are displayed for specific reasons.
Visibility | Function |
---|---|
public | __construct(array $files=null)</strong> : void Constructor |
public | add_error(\string $fieldName, \string $message)</strong> : void Add an error message to a specific field. You can also add errors to the form globally or to some aspect of the form, as long as you use the same $fieldName to refer to it at render time using get_errors_for(). |
public | checked(string $field, string $value=null)</strong> : bool true if the field was checked Whether or not $field was checked in the submission, optionally matching on $value (e.g. for radio buttons). necessary e.g. for radio inputs, where there's more than one possible value. |
public static | create_from_submission(mixed $args)</strong> : \Conifer\Form\AbstractBase the form object Create a new instance from the request array (e.g. $_POST) and return the hydrated form object. Takes a variable number of arguments, but the first argument MUST be the submitted values, as an associative array. The remaining arguments, if any, are passed to the constructor. |
public | get(mixed $field)</strong> : the submitted value, or the existing persisted value if no value has been submitted, or otherwise null. Get the current value for the given form field. |
public | get_error_messages_for(\string $fieldName)</strong> : array an array of error message strings Get error messages for a specific field |
public | get_errors() : array Get the errors collected while processing this form, if any |
public | get_errors_for(\string $fieldName)</strong> : array an array of error arrays Get errors for a specific field |
public | get_field(\string $name)</strong> : array/null the field, or null if it doesn't exist Get a field definition by its name |
public | get_fields() : array an array of form fields. Get the fields configured for this form |
public | get_file(\string $field)</strong> : array An array of data for the given file upload. Will be empty if the field doesn't exist or an error occurred during upload. Get an uploaded file by field name |
public | get_file_upload_error_message(array $field, \integer $errorCode)</strong> : string The error message, based on the specified upload error code Returns an error message for a given file upload field, based on the provided PHP upload error code. |
public | get_files() : array an array of uploaded files. Get the files configured uploaded to this form |
public | get_unique_error_messages() : array Get all unique error messages as a flat array, e.g. for displaying in a list at the top of the |
public | get_whitelisted_fields(array $submission)</strong> : array the whitelisted fields Get the submitted values, filtered down to only fields decared in the constructor |
public | has_errors() : bool Whether this form has any validation errors |
public | has_errors_for(\string $fieldName)</strong> : bool Whether the field $fieldName has any validation errors |
public | hydrate(array $submission, array $options=array())</strong> : \Conifer\Form\AbstractBase this form instance Hydrate this form object with the submitted values typically $_POST , $_GET , or $_REQUEST . - stripslashes or strip_slashes : whether to run stripslashes() on any string values. When true , recursively applies to array options as well. Default: false ; will default to true in a future version of Conifer. |
public | abstract process(array $request)</strong> : void Process the form submission. |
public | require(array $field, \string $value)</strong> : boolean Alias of validate_required_field |
public | require_file(array $field)</strong> : boolean Alias of validate_required_file |
public | selected(\string $field, mixed $optionValue)</strong> : bool Whether $field was selected with the value $optionValue . php if ($form->selected('company_to_contact', 'acme')) { $acmeClient = new AcmeClient(); $acmeClient->sendMessage('Someone chose you!'); } actual submitted value |
public | set_files(array $files=null)</strong> : \Conifer\Form\AbstractBase This form instance Set the uploaded files for this form, generally to the contents of the $_FILES superglobal. |
public | succeeded() : boolean Whether this form has been processed without errors |
public | validate(array $submission)</strong> : boolean whether the submission is valid Check field values according to their respective validators, returning whether all validations pass. NOTE: This doesn't add any errors explicitly; specific validators are responsible for that. |
public | validate_file_mime_type(array $field, \string $value, array $submission, array $validTypes=array())</strong> : boolean Check if the specified file upload field submission matches an array of whitelisted mime types |
public | validate_file_upload(array $field)</strong> : boolean Check whether the specified file field has an upload error, adding an error if so. |
public | validate_required_field(array $field, \string $value)</strong> : boolean Check whether a value was submitted for the given field, adding an error if not. |
public | validate_required_file(array $field)</strong> : boolean Check whether a required file upload was submitted, adding an error if not. |
protected | execute_validator(mixed $validator, array $field, array $submission)</strong> : boolean Execute a single field validator any errors raised |
protected | filter_field(array $field, mixed $value)</strong> : mixed the filtered value Filter a submitted field value using the field's declared filter logic, if any. If a field default is provided, the default is applied after the filter (that is, to the result of the filter callback). |
protected | throw_files_exception_if_not_set() : void Throws an exception if the files property is null |
protected | validate_field(array $field, array $submission)</strong> : boolean Validate a single field MUST include at least a name index. |