Jump to content

form validation class


Destramic

Recommended Posts

hey guys ive been trying to design this form validation class for my framework but im having a problem with the line

 

public static function is_valid($method = $_POST)

 

if anyone can help please

 

<?php

class Form extends Form_Exception
{
protected $_fields              = array();
protected $_validation_messages = array();
protected $_form_errors         = 0;

public function add_validation($field_name, $valiidation_type, $validation_message = null)
{
	$this->_fields[$field_name]['name']               = $field_name;
	$this->_fields[$field_name]['validation_type']    = $validation_type;
    $this->_fields[$field_name]['validation_message'] = $validation_message;
}

public static function is_valid($method = $_POST)
{
	$fields = $this->_fields;

	foreach ($fields as $field)
	{
		$field_value        = $method[$field['name']];
		$validation_type    = $field['validation_type'];
		$validation_message = $field['validation_message'];

		$validation = $validation_type. '_validation';

		if ($this->$validation($field_value))
		{
			$this->set_validation_message($validation_message);
		}
	}

	$validation_messages_count = count(get_validation_messages());

	$this->set_form_errors($validation_messages_count);
}

public static function display_errors()
{
	$validation_messages = $this->_validation_message;

	foreach ($validation_messages as $validation_message)
	{
		echo $validation_message;
	}
}

protected function set_validation_message($validation_message)
{
	$this->_validation_messages[] = $validation_message
	return $this;
}

protected function set_form_errors($form_errors)
{
	$this->_form_errors = $form_errors
	return $this;
}

protected function get_fields()
{
	return $this->_fields;
}

protected function get_validation_messages()
{
	return $this->_validation_messages;
}
}

Link to comment
https://forums.phpfreaks.com/topic/248691-form-validation-class/
Share on other sites

You can't make a default value of a variable.

 

In order to do what you want you should change it to something like this:

 

public static function is_valid ( $method = NULL )
{
    /* Check if it is NULL, that means just use $_POST */
    if ( $method === NULL )
    {
        $method = $_POST;
    }
}

 

That said the variable name is very misleading.  If I read a variable named $method, I would think it would be something like 'POST' / 'GET' however, you are using it as the array to validate.  So I would change that accordingly to make the code more readable.

 

~juddster

 

~juddster

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.