Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation which depends on other values
  • Lets say we have a model Model_Event with 2 fields: date_start and date_end.
    The start-date of an event shouldn't be before the end-date. Now I am trying to implement a validation for this situation:

    class Model_Event extends \Orm\Model {
    protected static $_properties = array(
    'date_start' => array(
    'data_type' => 'string',
    'label' => 'Start-Datum',
    'validation' => array(
    'date_start'
    ),
    'form' => array( 'type' => 'date'),
    ),
    'date_end' => array(
    'data_type' => 'string',
    'label' => 'End-Datum',
    'validation' => array(
    'date_start'
    ),
    'form' => array('type' => 'date'),
    ),
    );
    public static function _validation_date_start($value) {

    }

    public static function _validation_date_end($value) {
    } }
    The problem is, that in the _validation_date_end/_validation_date_start methods are static methods, so I have no access to the other value (and it wouldn't help since the other value could be modified as well).
    How can I write a validation for this situation?
  • You can access the current validation instance using

    $validation = \Validation::active();

    And using that you can retrieve a validation field object using

    $field = $validation->field('fieldname');

    You can then retrieve it's value using

    $value = $field->value;

  • That doesn't work.

    $validation = \Validation::active();
    $start = $validation->field('date_start');
    $end = $validation->field('date_end');
    print_r($start->value);
    print_r($end->value);

    Both values are null.

    But i think

    $validation->input("date_start")

    can do the job.

  • HarroHarro
    Accepted Answer
    I've never had this at hand, so I had to look in the class for possible solutions.

    In hindsight it seems logical that the fieldset doesn't contain the posted data.

Howdy, Stranger!

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

In this Discussion