laravel-form-builder icon indicating copy to clipboard operation
laravel-form-builder copied to clipboard

Named forms appears as invalid

Open uovidiu opened this issue 7 years ago • 0 comments

Every named form does not validate.

Hope the code below explains it.


class ClientForm extends Form
{
    public function buildForm()
    {
        // Add fields here...

	    $this
		    ->add('company_name', 'text', [
			    'rules' => 'required|min:5',
		    ])
	        ->add('first_name', 'text', [
		        'rules' => 'required|min:5',
	        ])
		    ->add('last_name', 'text', [
//		    	'rules' => 'required|min:5'
		    ])
		    ->add('service_charge_rate', 'number', [
//		    	'rules' => 'required|between:0,99.99'
		    ])
		    ->add('late_fee_amount', 'number',[
//			    'rules' => 'required'
		    ])
		    ->add('minimum_balance', 'number',[
//			    'rules' => 'required'
		    ])

	    ;

	    $this->add('submit', 'submit', [
		    'attr' => [
			    'class' => 'btn btn-primary'
		    ]
	    ]);

    }
}

ClientController.php


    public function create(FormBuilder $formBuilder)
    {
        //

	    $form = $formBuilder->create(ClientForm::class, [
		    'method' => 'POST',
		    'url' => action('ClientController@store'),
		    'language_name' => 'translations',
	    ])->setName('clients');


	    return view('clients.create', compact('form'));
    }


public function store(Request $request)
{

   dd(\request()->all());

	    $form = $this->form(ClientForm::class);

	    // It will automatically use current request, get the rules, and do the validation
	    if (!$form->isValid()) {

	    	dd($form->getErrors());

		    return redirect()->back()->withErrors($form->getErrors())->withInput();
	    }

}

dd output is this

array:2 [▼
  "_token" => "2jV9gE9SU6RbjnKaOkFyhdk7rv4YYjitSGSRJQTI"
  "clients" => array:6 [▼
    "company_name" => "company name"
    "first_name" => "first name"
    "last_name" => ""
    "service_charge_rate" => ""
    "late_fee_amount" => ""
    "minimum_balance" => ""
  ]
]

$form->getErrors() outputs this

array:2 [▼
  "company_name" => array:1 [▼
    0 => "The Company name field is required."
  ]
  "first_name" => array:1 [▼
    0 => "The First name field is required."
  ]
]

Now everything happens with ->setName() on. If however I leave this out the validation works without a problem.

Maybe I don't use named form properly?

Thanks.

uovidiu avatar Jun 20 '17 21:06 uovidiu