/** * Add a new product * @return void */ public function addAction() { $validator = null; $form = new ProductForm(); $form->action = 'add'; $input = Input::make(); //Check is form posted if ($input->hasPost('btnSubmit') == true) { $product = new Product(); //Run validation if ($product->validate($input->post())) { // get post array value except the submit button $postArray = $input->except('btnSubmit')->post(); $product->product_name = $postArray["product_name"]; $product->category = $postArray["category"]; $product->description = $postArray["description"]; $product->validity = $postArray["validity"]; $product->price = $postArray["price"]; $product->created_at = $postArray["created_at"]; $product->updated_at = $postArray["updated_at"]; // Save form details if ($product->save()) { $this->setFlash('success', 'Product added successfully!')->redirectTo('product/index/'); } else { $this->setFlash('error', 'Error occured while saving Product!')->redirectTo('product/index/'); } } else { //Set validation error into form builder $form->errors = $product->validationErrors(); } $form->validator = $product->getValidator(); } // We can also use same view page for create and update $content = View::create('Apps.Views.product.create', ['form' => $form->render(), 'validation_errors' => $form->errors, 'title' => 'Add a new Product']); return Response::make($content); }
/** * Add a new product * @return void */ public function addAction() { $validator = null; $form = new ProductForm(); $form->action = 'add'; $input = Input::make(); //Check is form posted if ($input->hasPost('btnSubmit') == true) { $validator = $this->setValidationRules($input); //Run validation if ($validator->run()) { $product = new Product(); // get post array value except the submit button $postArray = $input->except('btnSubmit')->post(); $product->product_name = $postArray["product_name"]; $product->category = $postArray["category"]; $product->description = $postArray["description"]; $product->validity = $postArray["validity"]; $product->price = $postArray["price"]; $product->created_at = $postArray["created_at"]; $product->updated_at = $postArray["updated_at"]; // Save form details if ($product->save()) { $this->setFlash('success', 'Product added successfully!')->redirectTo('product/index/' . Url::segment(3)); } else { $this->setFlash('error', 'Error occured while adding Product!')->redirectTo('product/index/' . Url::segment(3)); } } else { //validation error here $form->errors = $validator->getErrors(); } $form->validation = $validator; } // We can also use same view page for create and update $this->render('create', array('form' => $form->buildForm()->render(), 'validation_errors' => $form->errors, 'title' => 'Add a new Product')); }