2

This week i've decided to move from CodeIgniter to Laravel. Atm I'm trying to "migrate" my old CI website over to Laravel.

Note: I am very new to Laravel, so if there's something that I'm doing wrong then please say :) I am used to CodeIgniter, I don't even know if this will work in Laravel.

Here is how i want it to be / as i had it in codeigniter --> http://pastebin.com/DRaW0Mzt

        public function add()
        {
                $data['header'] = "Create New User";
                $data['title'] = "Create New User";
                $data['description'] = "Create New User";
                $data['user_groups'] = UserGroup::all();

                return View::make('users.create', $data);
        }

        function create()
        {

                $validator = Validator::make(
                    array(
                        'email' => Input::get('email'),
                        'username' => Input::get('username'),
                    ),
                    array(
                        'email' => 'required|email|unique:users',
                        'username' => 'required|min:3|max:14|unique:users',
                    )
                );

                if ($validator->fails())
                {
                        /* If there's an error then just call the add function again */
                        /* $validator->messages(); should still be available since it is still the same "instance" */
            /* This is how i did it on CodeIgniter */
                        $this->add();
                }else{
                        /* success code... */  
                }

        }

If $validator fails i want it to call $this->add(); again. Note: i have tried doing the following;

return Redirect::to('users/add')->withInput()->withErrors($validator); # redirect to users/add, WITH the old input and WITH the errors from $validator. */

Problem #1: When calling $this->add(); it call the function, but it doesn't execute the code inside the function, the view is not created and all $data is not passed to the view.

And also, IF i create;

protected $layout = "layouts.master"; 

It will use that one instead of;

return View::make('users.create', $data);

But no $data will be passed to it.

Problem #2: The problem with using Redirect::to(); is that IF the user reloads the page then all Input::old(); is gone since withInput() and withErrors(); uses session to store the data and it only lasts for one request, also the errors go away. including the CSS around/on the inputs https://dl.dropboxusercontent.com/u/11204765/SS/SS-026.png

Note: I tried using both the following;

Session::reflash();
Session::keep(array(...));

Anyone knows a solution to the problem? or what I'm possibly doing wrong?

Any help is appreciated!

5
  • #1 use Redirect::to or Redirect::back(). #2 Why is it a problem? If use reloads the page, then it should be reloaded, ain't it? Commented Jul 20, 2014 at 12:38
  • If i Redirect::to()->withInput->()->withErrors(); then 1) i won't be able to dl.dropboxusercontent.com/u/11204765/SS/SS-026.png get CSS around my Inputs without tons of if statements in my view files. 2) if the user reloads the page (for whatever reason) all the old input is gone, and so are the errors, because ->withInput(); and ->withErros(); utilize session, which means the data only lasts 1 request and i want it to last several requests if neccessary, which is why i also tried to use Session::reflash(); and Session::keep(); Commented Jul 20, 2014 at 12:46
  • 1 use single css class, no need for ton of if statements. 2 I understand that, this is how it should work - like I said, if a user reloads the page, then the page should be reloaded. That's how I see it. Commented Jul 20, 2014 at 13:07
  • 1) I would still need an if statement to determine IF i need/want to add the CSS class or not, or am i wrong? 2) yes of course, but i also want the old input to stay and the errors too. Commented Jul 20, 2014 at 13:24
  • @deczo something like this, i would need if statements to determine to determine which css classes i need to add, if the field is approved or contain an error dl.dropboxusercontent.com/u/11204765/SS/SS-027.png (using Bootstrap CSS just fyi). Commented Jul 20, 2014 at 13:35

1 Answer 1

1

Problem #1

Your code is probably executing but you are not returning view form your function.

if ($validator->fails())
{
    return $this->add();
}

Anyway, you should stick to Single Responiblity Principale. Instead of invoking $this->add(); move your bussiness logic to model.

if ($validator->fails())
{
     App::make('User')->addUser(Input::all());

     return View::make('users.create', $data);
}

and inside your user model add function addUser

class User extends \Eloquent
{
     public $fillable = ['email', 'username']; //protected data from assigment by user manually

     public function addUser(array $input)
     {
         return $this->create($input);
     }
}

App:make('User') is almost the same as calling just User::, in future in will allow you to test your code better so it is good practise to write it. You can learn more about IOC here

Problem #2

If user reload the page the form will by cleared, so we want you to have errors displayed?

Sign up to request clarification or add additional context in comments.

14 Comments

Note sure what you're trying to do here, but i did that and i got an error; Illuminate \ Database \ Eloquent \ MassAssignmentException - _token, not entirely sure how to use App::make(); and what it does.
I've set wrong property on User Model, it should be fillable. You can get more info here wiki.laravel.io/FAQ_%28Laravel_4%29#MassAssignmentException
It seems that what you are trying to do with App::make('User')->addUser(Input::all()); is to add the user to the database, which is not what I'm trying to do now is it? if $validator fails i want it do load the form page again and display the old inputs AND error messages. What you were doing now was to add empty users to the DB when the validation failed.
Faux pas. To quick fix just add return statement before $this->add() as I said.
Ahh didn't see that you made that change.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.