Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
When inserting the ORM doesn't validate my data
  • Hi everyone! I'm new on FuelPHP and just started to play with it yesterday. My only issue so far is that I can't get the ORM to validate the data I want to insert.
    This is the function on my controller:
    function post_user()
        {
            $data = array( 
                'first_name' => input::post('first_name'), 
                'last_name'  => input::post('last_name'),
                'gender'     => input::post('gender'),
                'birth'      => input::post('birth'),
                'email'      => input::post('email'),
                'password'   => input::post('password')
                );
            $user = new Model_user($data); 
            if($user->save()){
                $this->response($data,201);
            }else{
                $this->response(null,500);
            }
        }
    
    And this are the propertis on th Model_user:
    protected static $_properties = array(
             'id',
             'first_name' => array(
                 'data_type' => 'varchar',
                 'label' => 'First Name',
                 'validation' => array('required', 'min_length' => array(3), 'max_length' => array(32)),
                 'form' => array('data_type' => 'text')
             ),
             'last_name' => array(
                 'data_type' => 'varchar',
                 'label' => 'Last Name',
                 'validation' => array('required', 'min_length' => array(3), 'max_length' => array(100)),
                 'form' => array('data_type' => 'text')
             ),
             'email' => array(
                 'data_type' => 'varchar',
                 'label' => 'E-mail',
                 'validation' => array('required', 'min_length' => array(3), 'max_length' => array(100)),
                 'form' => array('data_type' => 'text')
             ),
             'gender' => array(
                 'data_type' => 'varchar',
                 'label' => 'Gender',
                 'form' => array('data_type' => 'select', 'options' => array('m' => 'Male', 'f' => 'Female')),
                 'validation' => array('required', 'min_length' => array(1), 'max_length' => array(1)),
             ),
             'birth' => array(
        'data_type' =>'date',
        'label' => 'Birth Date',
        'validation' => array('required')
       ),
       'password' => array(
        'data_type' => 'varchar',
        'label' => 'Password',
        'validation' => array('required')
       )
         );
    
    If I send the 'first_name' empty it stills inserts the record
    Do I have to instantiate some validation object or something like that?
    Thank you very much in advance I haven't been able to figure it out by myself
    Guillermo
  • If you want to use the validation within the model, you'll have to configure the validation observer. For more information, see http://docs.fuelphp.com/packages/orm/observers/included.html#os_validation
  • Thank you very much! I'll give it a try!

Howdy, Stranger!

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

In this Discussion