Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation active_field
  • hi,

    i extended validation class and added a custom validation. I was wondering how can i know what field is being validated. Should i use static::active_field() ? It returns blank...
    Another thing is that my function is not getting called if i left the field blank in the form.
    Thanks in advance

    public static function _validation_requiredLang($val) {
    die("field=".static::active_field());
    }
  • Validation rules are only called if there is data to validate. If you don't want the field to be left blank, add the "required" rule to it.

    Validation rules in the validation class are not static. Other rules use $this->active_field(), like _validation_required_with() or _validation_match_field().
  • Hi Harro,

     I've tried with static and non static method and static call to active_field and using this but it returns nothing...
     I want the fields with this validation rule to be "required" but only if the language that referes to is active. how can i achieve this if the validation isn't called when the field is left blank?



    public function _validation_requiredLang($val) {
    die("field".$this->active_field());

    $field = Validation::active_field();
    $idLanguage = substr($field, strlen($field)-1, strlen($field));

    if( is_int($idLanguage) ){
    $lang = Model_RegistrantLanguage::query()->where('idLanguage', $idLanguage)->where('active', 1)->get_one();
    if( $lang && $this->_empty($val) ) return false;
    }
    return true;
    }

    thanks
  • Where is this method defined? How did you extend? The methods I mentioned use the same, and they do work.

    Validation only works on filled-in fields, this is by design. There is no "required_if" functionality at the moment.

    It's unlikely that such a big change will be incorporated in the current codebase, but it could be looked at for Fuel v2. If you want, you can create a feature request for it at https://github.com/fuelphp/validation.
  • I extended the validation class and added it to the autoloader in the bootstrap files
    Autoloader::add_classes(array(
    // Add classes you want to override here
    // Example: 'View' => APPPATH.'classes/view.php',
    'Validation' => APPPATH.'classes/core/validation.php',
    'Log' => APPPATH.'classes/core/log.php',
    ));

    My class in classes/core/validation.php:
    class Validation extends Fuel\Core\Validation {

    /**
    * Required validation for translatable fields
    */
    public function _validation_requiredLang($val) {
    die("field=".$this->active_field());

    $field = $this->active_field();
    $idLanguage = substr($field, strlen($field)-1, strlen($field));

    if( is_int($idLanguage) ){
    $lang = Model_RegistrantLanguage::query()->where('idLanguage', $idLanguage)->where('active', 1)->get_one();
    if( $lang && $this->_empty($val) ) return false;
    }
    return true;
    }

    }
  • HarroHarro
    Accepted Answer
    Looks ok.

    I think where you go wrong is assume that active_field() is a string, which it isn't, it's a Fieldset_Field object. So you can't output it in a die() like you do.

    Use

    $field = $this->active_field();
    echo $field->name;

    if you want the fieldname.
  • p.s. it's better to use

    \Debug::dump($this->active_field()); die();

    it gives you a lot more information, plus options to navigate complex array's and objects.
  • For anyone with similar question. Bacause Fuelphp Validation doesn't have a required_if i ended up overloading save method in my base controller.
    Tanks for your help Harro.



    example code bellow:

    public function save($cascade = null, $use_transaction = false)
    {
    // get active Languages and check required translatable fields
    $activeLanguages = array();
    foreach( Model_Language::getActiveLanguages() as $lang ) $activeLanguages[] = $lang->idLanguage;

    foreach( self::properties() as $field => $property ){
    if( isset($property['validation']) && in_array('requiredLang', $property['validation']) &&
    in_array(substr($field, strlen($field)-1, strlen($field)), $activeLanguages) && empty($this->$field) ){
    throw new Orm\ValidationFailed('Please fill all mandatory fields');
    }
    }

    return parent::save($cascade, $use_transaction);
    }
  • I ran across this issue today as well.  It seems there's a lack of application for actual required fields.  At least I'm seeing it happen with new record entries (explained below).

    Orm\Observer_Validation, line 226:
    if ( ! in_array($p, $obj->primary_key()) and $obj->is_changed($p))

    is_changed() then goes through several checks between _original and _data.  The problem is, on a new record where neither has been set, it's true nothing been changed.  But in order to add the field to $input (back in Observer_Validation::validate() within that if clause), some change is necessary via the if statement above.

    All that said, https://github.com/fuelphp/validation description isn't totally accurate when it says:

    "required - Checks if the value exists in the data being validated"
  • is_changed() has nothing to do with validation, it's job is to see if there was something changed. If you have a record that is completely empty, it sees it as not changed (which technically it isn't, it was empty and it still is).

    So I don't understand the rest of the message?
  • is_changed() is a part of Observer_Validation::validate() where it creates the $input array.  If $input is empty, Validation::run will never execute.  So if a Model using this observer (before_save), no fields are set and save() is executed, the 'required' validation rule will never be applied.  If the reason that validation rule is applied is because certain columns are 'NOT NULL', the fallback will be an exception upon a SQL insert.

    While it's true that nothing was set on the model, take a hypothetical situation where the intention may be to insert a row to retrieve a PK id and perhaps do updates to that row later.  The way I ran into this was via a RESTful call to create a row - I tested it with no parameters defined.  While I'm not yet finished with my routines for screening input etc, an expected fallback behaviour would be that the 'required' validation rule would at least require a value for that column. 
  • Ok, I get what you mean now. You want to run full validation on a new object, so it should be

    if ( ! in_array($p, $obj->primary_key()) and ($obj->is_new() or $obj->is_changed($p)))

    can you check if that fixes it for you?
  • Yes, that nailed it Harro.  It threw me the Orm\\ValidationFailed which is what I expected.  It also appears that it also allows no value on fields not marked as required, which is correct.

    I'll develop more with this tomorrow.  I'll let you know if anything negative surfaces from that alteration.

    Thanks!
  • Cool, if you can confim it fixes it, I'll push the change.
  • This fixes the issue mentioned above.

Howdy, Stranger!

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

In this Discussion