API16

JFormValidator/validate: Difference between revisions

From Joomla! Documentation

m removing red link to edit, no existant pages
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
Method to validate a group of fields.
Method to validate a group of fields.


<span class="editsection" style="font-size:76%;">
 
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>


<! removed transcluded page call, red link never existed >
<! removed transcluded page call, red link never existed >
Line 83: Line 81:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
<! removed transcluded page call, red link never existed >
<! removed transcluded page call, red link never existed >


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=validate
  category=validate
  category=JFormValidator
  category=JFormValidator
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*

Latest revision as of 01:44, 25 March 2017

Description

Method to validate a group of fields.


<! removed transcluded page call, red link never existed >

Syntax

validate(&$fields, &$data)
Parameter Name Default Value Description
&$fields $fields An array of fields to validate.
&$data $data The data to validate.

Returns

mixed Array on success, on error.

Defined in

libraries/joomla/form/formvalidator.php

Importing

jimport( 'joomla.form.formvalidator' );

Source Body

public function validate(&$fields, &$data)
{
        $results = array();

        foreach ($fields as $name => $field) {
                // Get the data for the field.
                $value = array_key_exists($name, $data) ? $data[$name] : null;

                // Check if the field is required.
                if ((string)$field->attributes()->required == 'true') {
                        // Check if the field value is empty.
                        if ($value === '') {
                                // The required field is empty!
                                if ($message = (string)$field->attributes()->message) {
                                        $results[] = new JException(JText::_($message), 0, E_WARNING);
                                } else {
                                        $results[] = new JException(JText::sprintf('Libraries_Form_Validator_Field_Required', JText::_((string)$field->attributes()->name)), 0, E_WARNING);
                                }

                                // We don't want to display more than one message per field so continue to the next one.
                                continue;
                        }
                }

                // Run the field validator.
                $return = $this->_isValid($field, $data);

                // Check for an error.
                if (JError::isError($return)) {
                        return $return;
                }

                // Check if the field is valid.
                if ($return === false) {
                        // The field failed validation.
                        if ($message = (string)$field->attributes()->message) {
                                $results[] = new JException(JText::_($message), 0, E_WARNING);
                        } else {
                                $results[] = new JException(JText::sprintf('Libraries_Form_Validator_Field_Invalid', (string)$field->attributes()->name), 0, E_WARNING);
                        }
                }
        }

        return $results;
}


<! removed transcluded page call, red link never existed >

Examples

Code Examples