[form-builder] In form builder can we send error response and exit form?
Created 7 years ago by lckamal

I am trying like this for exit form and send response

class UserLanguageFormBuilder extends FormBuilder
{
...
public function onPost()
    {
        $language = \Request::get('language');
        $userlang = UserLanguageModel::where(['user_id' => auth()->id(), 'language_id' => $language])->count();
        if($userlang > 0){
            return response()->json(['success' => false, 'message' => 'This language is already added.']);
        }
    }
}

Is there some other options instead?

piterden  —  7 years ago
$this->setFormResponse($redirect->to('admin/users/roles'));

return;
class UserLanguageFormBuilder extends FormBuilder
{
...
public function onPost()
    {
        $language = \Request::get('language');

        // The right way is to use UserLaguageRepositoryInterface here
        //$userlang = UserLanguageModel::where(['user_id' => auth()->id(), 'language_id' => $language])->count();

        if($userlang > 0){
            $this->setFormResponse(
                response()->json(['success' => false, 'message' => 'This language is already added.'])
            );
            return;
        }
    }
}
lckamal  —  7 years ago

Hi piterden, It is not working

dali  —  7 years ago

instead of using onPost create a handler class UserLanguageFormHandler and :

    public function handle(UserLanguageFormBuilder $builder, MessageBag $messages, Redirector $redirect)
    {
        $messages->error('Cant save now..');
        $builder->setSave(false);
        $builder->setFormResponse($redirect->back());
    }