Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Regarding closure in FuelPHP 1.8
  • Hello there,

    I am just wondering if the closure is not implemented in FuelPHP 1.8.

    I used to implement closure in FuelPHP 1.7.3.

    But, it seems to me that its gone on new version.
  • Moreover, to implement own validation, do we have to define own validation like bottom of following page?

    https://fuelphp.com/docs/classes/validation/validation.html

    Or, is there any way to make validation?


  • What do you mean with "the closure"? A closure is a PHP language construct, it has nothing to do with Fuel. Perhaps you can post a code snippit so we can see what you are trying to do?

    As to validation, I assume you mean "define your own validation rules"?


    1. adding them to the core Validation class by extending the class in your app.

    Has the advantage that your own rules are automatically available everywhere in your app. Has the disadvantage that you have to re-check compatibility when you upgrade Fuel.

    2. adding them to a seperate and dedicated class in your app.

    Has the advantage that your rules are separated from the framework rules, that you can create several rulesets by creating multiple classes. Has the disadvantage that you have to add it to a validation object in your code using the "add_callable" method.

    3. adding them to a (ORM) model class.

    This option is only useful if you do form validation via a model, so if you have a form that represents a record. The advantage is that you can define specific rules for specific fields in your model, the disadvantage is that you may have to duplicate methods if multiple models require the same validation rule (unless you use a base model), and your validation code is all over the place, making maintenance of the app  possibly more complex.
  • Hello there,

    Here is sample code and bold word is the closure part.

    $val->add_field('group', 'group', 'required|valid_string[numeric]')
    ->add_rule(['closure_group'=>
    function($group)
    {
    if($group === '0' || $group === '100')
    {
    return true;
    }
    Validation::active()->set_message('closure_group','wrong group is selected');
    return false;
    }
    ]);

    Possibly, I misunderstand something.
    The source code is working in FuelPHP 1.7.4.
    However, it seems to me that it is not working in FuelPHP 1.8.0.4.

  • In fact, the environment is different.

    I have 2 apps.

    First one is PHP 5.6 and FuelPHP 1.7.4 is installed.

    The other one is PHP 5.3 and FuelPHP 1.8.0.4 is installed.
  • Closures should work in PHP 5.3+. They are used inside the framework as well, so I don't think that is the problem.

    What is the definition of "doesn't work"? Any error messages? Anything in the (php) logs? Any specific behaviour that is different?
  • That is what I expect.

    I have following simple closure and dose not work in fuelPHP 1.8.0.4.

    $val->add_field('is_target_host','Target host', 'numeric_max[1]|numeric_min[0]') 
            ->add_rule(['closure_is_target_host'=>                                                                                  
                          function ($is_target_host)                                                                                
                          {                                                                                                         
                            return false;                                                                                                                 }                                                                                                         
                        ]);  
  • The error message is as following.

    ErrorException [ Parsing Error ]:
    syntax error, unexpected '[', expecting ')'
  • HarroHarro
    Accepted Answer
    You are using short array notation? That is not supported in PHP 5.3, only in 5.4+.
  • Yes....You are right.

    I just realise that that is not supported in PHP 5.3.

    I just replace that to array.

    Thank you :)
  • Closures in PHP 5.3 is as following.

    $val->add_field('is_target_host','Target host', 'numeric_max[1]|numeric_min[0]') 
            ->add_rule(array('closure_is_target_host'=>                                                                                  
                          function ($is_target_host) use ($id)                                                                               
                          {                                                                                                         
                            return false;                                                           
                        ));  
  • And what is the problem with it? Or does it work now?
  • sorry for late reply.
    I was sick since last week.
    Now, it is working and so no issue.
    Thank you for your help.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion