Monthly Archives: October 2011

FuelPHP Input arrays validation

After pulling some hair and digging a lot in FuelPHP core I finally discovered a litle hack that allows validation of input arrays and its re-population using FuelPHP Validation Class.

Here’s how I did it:

View simplified for the sake of the example:

<?php echo \Form::label("Price", "options[0][price]"); ?>
<?php echo \Form::input("options[0][price]", \Validation::instance()->input("options.0.price"); ?>

<?php echo \Form::label("Name", "options[0][name]"); ?>
<?php echo \Form::input("options[0][name]", \Validation::instance()->input("options.0.name"); ?>

<?php echo \Form::label("Price", "options[1][price]"); ?>
<?php echo \Form::input("options[1][price]", \Validation::instance()->input("options.1.price"); ?>

<?php echo \Form::label("Name", "options[1][name]"); ?>
<?php echo \Form::input("options[1][name]", \Validation::instance()->input("options.1.name"); ?>

Mind the options.0.name instead of options[0]name inside the Validation Input.

In my controller I did this:

$val = \Validation::factory();

if ( \Input::post("options") )
{
    $form_options = \Input::post("options");

    foreach ( $form_options as $form_id => $option )
    {
        $val->add("options.{$form_id}.price", "{$option["name"]} price")
                ->add_rule("required")
                ->add_rule("match_pattern", '/[0-9]*(\.|,)?[0-9]+/');

        $val->add("options.{$form_id}.name", "{$option["name"]} name")
                ->add_rule("required");

    }
}

 

And that’s it, this should validate the fields and repopulating them in case of errors. It’s a bit stupid that it only works like this.

FuelPHP should provide a friendlier way and a documented one to validate array input fields, it’s a common feature when dealing with dynamic built forms that have one-to-many objects in it.

 

Tested with FuelPHP 1.0.1

FuelPHP BreadCrumb Class/Library

Morning,

Just finish building a breadcrumb Class for FUELPHP.

This Class has persistent crumbs, meaning that visited crumbs will be saved in session.

GitHub link for FuelPHP-BreadCrumb

Read more »

Jump Tables Explained

A jump table is an array of function pointers or even machine instructions, each of which actually performs the jump. The latter is commonly used in assembly language and can be produced by compilers, but the former is something you’re likely to write yourself, often to help simplify some code.

Read more »

Check if all function arguments are equal in PHP

Question: Could this be written better?

private function compareThree($val_1, $val_2, $val_3)
{
    return (($val_1 == $val_2) && ($val_1 == $val_3) && ($val_2 == $val_1) && ($val_2 == $val_3) && ($val_3 == $val_2) && ($val_3 == $val_1));
}

Short Answer: Yes! That’s horrible and makes my eyes bleed.

Long Answer: Why compare only three? Why not build a function that compares a infitine number of arguments and check if they are all equal. Here’s my solution:

function compareAllTheArguments()
{
    return count(array_unique(func_get_args())) === 1;
}

Explanation:

func_get_args() // Get all function arguments in array list

 array_unique() // Removes duplicate values from an array

count() // Well, yeah, returns the number of elements in a array

Knowing this first we get all the arguments in the function then remove all the duplicates and if it’s only one left that means all elements are equal.