fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
813 stars 345 forks source link

'valid_string' Not Working in ORM\Model #555

Closed knudpotente closed 13 years ago

knudpotente commented 13 years ago

Issue as reported on the Forum (http://fuelphp.com/forums/topics/view/5217).

Validation using 'valid_string' does not behave as expected when defined inside ORM\Model $_properties. Example: we want to establish validation for a 'surname' field in the model, where we will only accept alpha characters, spaces and dashes. This is defined in the model as:

<?php

class Model_Client extends Orm\Model {

 protected static $_properties = array(
    'id',
    'created_at',
    'updated_at',
    'given_names',
    'surname' => array(
       'type' => 'string',
       'label' => 'Surname',
       'validation' => array(
           'trim',
           'strip_tags',
           'required', 
           'valid_string'=>array('alpha','spaces','dashes')
       ),
    ),
 );

 protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array('before_insert'),
    'Orm\Observer_UpdatedAt' => array('before_save'),
    'Orm\Observer_Validation' => array('before_save'),
 );

}

The way the 'valid_string' validation in the surname field behaves - using different configurations - is like this:

(1) Using 'valid_string' => array('alpha') only: works as expected - allows only alpha characters, fails if anything else is typed.

(2) Using 'valid_string' => array('alpha', 'whatever'): if anything else is added after 'alpha' (such as 'spaces', or 'dashes'), then it ignores whatever has been added, and validation works exactly as in (1) - that is, if I add a space or a dash, even if I specified that this is be allowed by using 'spaces' or 'dashes', then validation fails

(3) Using 'valid_string' => array('whatever','alpha'): if anything is added before 'alpha', then validation always fails, even if the field contains only alpha characters.

jschreuder commented 13 years ago

That's because only the first param is actually used, as described in docs the valid_string takes either a string or an array as input. The array you add here are the parameters, thus each value is a separate param, if you want it to work you have to call it like this:

'valid_string' => array(array('alpha', 'spaces', 'dashes'))

Then the first param passed is the array of options.

knudpotente commented 13 years ago

Thank you for the explanation! This does not seem clear to me from the documentation - perhaps adding an example there using 'valid_string' would help clear things up.

This usage, however, does seem a bit odd. Why have an 'array' encasing everything: if the content of this array is only ever going to be one single item- ie., one string, or one other array - isn't this encasing array redundant? There may be a technical reason why it is needed, but the logical usage - which is what I had understood from the docs - perhaps should be:

1) Either a (list) string: 'valid_string' => 'alpha,spaces,dashes' 2) Or an array: 'valid_string' => array('alpha','spaces','dashes')

If this makes more sense, then perhaps this issue could be changed to a 'feature request'?