\$\begingroup\$
\$\endgroup\$
2
I have sample controller InboxController
in my Inbox app. I want control with field name from my controller. If I will control with fields name from controller will not be destroyed MVC
rules?
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class InboxController extends Controller
{
/*
|--------------------------------------------------------------------------
| Field names in HTML form
|--------------------------------------------------------------------------
*/
/**
* Recipient field name.
*
* @var string
*/
public $recipient = 'recipient';
/**
* Sender field name.
*
* @var string
*/
public $sender = 'sender';
/**
* Subject field name.
*
* @var string
*/
}
Html code:
<input type='text' name="{{ $sender }}">
asked Jan 9, 2018 at 6:17
-
\$\begingroup\$ what are we supposed to be reviewing here? the two controller properties? yes, those are very nice. i have no suggestions. if you want to do something with them and you're not sure how, you should try asking on stackoverflow. \$\endgroup\$I wrestled a bear once.– I wrestled a bear once.2018年01月09日 17:15:34 +00:00Commented Jan 9, 2018 at 17:15
-
\$\begingroup\$ I just wanted to ask if it would be bad if the field names on the HTML form will be defined on the controller @I wrestled a bear once. \$\endgroup\$Andreas Hunter– Andreas Hunter2018年01月10日 02:52:41 +00:00Commented Jan 10, 2018 at 2:52
1 Answer 1
\$\begingroup\$
\$\endgroup\$
I recomend you to use Symfony like form builder. There you can create one form and reuse it anywhere.
$form = FormFactory::create(FormType::class, $user)
->add('name', TextType::class)
->add('email', EmailType::class, [
'rules' => 'unique:users,email',
])
->add('save', SubmitType::class, ['label' => 'Save user']);
What is also good is with this form you can also validate your input at the same time.
$form->handleRequest();
if ($form->isSubmitted() && $form->isValid()) {
//Your code
}
Here is link for repository
answered Jan 10, 2018 at 7:49
lang-php