Skip to content

Form Options and Validation

Niamul Hasan edited this page Jan 5, 2021 · 5 revisions

Checking if the form is submited, it also works in the Controller. And remember you can directly submit post data to a method in a controller

if (Form::exists()) {
//Do something if the form is submitted
}

Chicking if the validation is passed

The $validation->passed() methods returns a boolean value. If the validation is passed it returns true Usage

   if ($validation->passed()) {
    //Do somthing when the validation is passed
   } else {
    // Show the what didn't match the validation requirement
   }

Showing errors

$validation->errors() returns an array which contain's the info for showing the users what made the validation failed. Usage: We can run a foreach loop for looping through the errors and print them.

               foreach ($validation->errors() as $error) {
                    $response['message'] = $error;
                    $x = $x + 1;
                }

Tip: print the error in the else block if the $validation->passed() check

Form Validation Example

            $form = new Validate();
            $validation = $form->check($_POST, array(
                'username' => array(
                    'required' => true,
                    'min' => 2,
                    'max' => 200,
                    'unique' => 'users' //here 'users' is  the  table name. and field name is same to the HTML form's field
                ),
                'password' => array(
                    'required' => true,
                    'min' => 6
                ),
                'password_again' => array(
                    'required' => true,
                    'matches' => 'password'
                )
            ));

How to use

For using the form validation you have to an object

$form = new Validate();

Validate() is a class from the coreX framework

Then to be able to apply checking on any GET or POST request check(method, array()) Let's put it in a variable so we can check the report of validation

validation = $form->check($_POST, array());

Here, the second parameter array() takes an associative array which contains elements with the key named same as the input field of the HTML form. And every elements contains anoter array which contains ruling of validation.

validation = $form->check($_POST, array(
                'username' => array(),
                'password' => array(),
                'password_again' => array(),
                'name' => array()
            ));

and the ruling array looks like

    array(
      'required' => true,
      'min' => 2,
      'max' => 200,
      'unique' => 'users'
    ),

Validation Rules

This is an associative array as well :D Here's the elements of array which are used to validate (Example are shown above, here's the possible values are listed)

reuired

The required keyed element is used to set the rule if the input field is reqiured. It accepts boolean value, by default it's false. ie: If you don't set 'required' => it is 'required' => false by default.

   'required' => true/false,

min

min checks if the minimum numbers of cherecter is filled. It takes int as value. ie: 5

   'min' => int,

max

max checks if it exceed the maximum numbers of cherecters. It takes int as value. ie: 12

   'max' => int,

matches

matches checks if the the HTML input value matches with another input form's value. It's useful for checking the "Confirm Password" field. It takes String as value, which is the name of an HTML input field.

   'matches' => 'password',

unique

unique is used to check the input value if it's an non existing value in a database table. It takes a String which is the name of the database table Please note that the parent array's key ie field name ie the html form's input name must be same as the column name of the database table.

   'unique' => 'users'

Happy Security. And don't forget to use the token to prevent unauthorized request

Clone this wiki locally