Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation problem
  • Hi guys,I'm trying to create a simple application, but I have a lot of problem with validation.
    With OIL scaffolding feature I created a simple structure named "articles" with just 2 fields - name and count. If I try it all goes well, but if I try to integrate it with a simple autentication (I want name to be required) something goes wrong because I can't be able to:
    - populate form with data I posted before submit.
    - retrieve error information Can someone help me to understand how form with validation works? My VIEW:
    <h2 class="first">New Article</h2>
    
    <?php echo isset($errors) ? $errors : false; ?>
    <?php echo Form::open(); ?>
     <p>
      <?php echo Form::label('Name', 'name'); ?>
      <?php echo Form::input('name', Input::post('name', isset($article) ? $article->name : '')); ?>
     </p>
     <p>
      <?php echo Form::label('Count', 'count'); ?>
      <?php echo Form::input('count', Input::post('count', isset($article) ? $article->count : '')); ?>
     </p>
    
     <div class="actions"><?php echo Form::submit(); ?></div>
    <?php echo Form::close(); ?>
    
    My CONTROLLER function (generated with scaffolding / with validation):
    public function action_create($id = null)
     {
      if (Input::method() == 'POST')
      {
       $val = Validation::factory();
       $val->add_field('name', 'Name', 'required');
       
       if($val->run())
       {
       
        $article = Model_Article::factory(array(
         'name' => Input::post('name'),
         'count' => Input::post('count'),
        ));
     
        if ($article and $article->save())
        {
         Session::set_flash('notice', 'Added article #' . $article->id . '.');
         Response::redirect('articles');
        }
     
        else
        {
         Session::set_flash('notice', 'Could not save article.');
        }
       }
       else
       {
        $this->template->errors = $val->errors();
       }
      }
    
      $this->template->title = "Articles";
      $this->template->content = View::factory('articles/create');
     }
    

    If I don't insert any name (but insert a count), it doesn't display errors and doesn't display my count. Thanks for your help!
  • The create action should look smth like that: http://scrp.at/agR And remember, to use $val->validated() insted od posted data, because if you'll add rules like trim|strip_tags - Input::post() will have not trimmed and not stop_tagged version, while $val->validation('name'); will do. if models are set up correctly you should be able to insert data to database easily
  • Thanks for your help, I forget to change the code generated from scaffoldind ... And I'm finally be able to do a correct validation! Thanks! I decided to use this approach to send errors to the view, is it right?
      else
      &#123;
       $data['errors'] = $val->show_errors();
      }
     
      $this->template->title = "Articles";
      $this->template->content = View::factory('articles/create', $data, false);
    

    Or there exists a simple one? Thanks in advance!
  • I do pretty much the same way :) Glad you have solved your problem!

Howdy, Stranger!

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

In this Discussion