mardi 27 octobre 2020

Form validation on Laravel 5

I seem to have a problem with this function which is used in a contact form. This form was made by a developper I get this message displaying in a random way : "An Error Occured.." when I click Submit button in my form. Most of time I get this error message but on local I yesterday got it not displayed and bassed to the next step and validated the form. Problem is I was not able to reproduce it.

I found the controler use a PostIndex function as follow

    public function postIndex($origin = '')
{
    $input = Input::only('title','name','country_id','company','company_website','phone_number','email','message');
    $input['origin'] = $origin;
    $input['ip'] = getIP();
    // Create a new validator instance from our validation rules
    $validator = Validator::make($input, ContactUs::$rules );

    // If validation fails, we'll exit the operation now.
    if ($validator->fails())
        return Redirect::route('contact-us')->withErrors($validator);
    try {
        $contact = ContactUs::create($input);
    } catch (Exception $e) {
        return Redirect::back()->withInput()->with('error', 'An Error Occured..');  
    }
    return Redirect::route('contact-us.show',$contact->token)->with('success', 'We have received your message.');

}

I suppose there is an issue with the validator relatd to rules which are defined as follow

public static $rules = array(
        'title'             => 'required',
        'name'              => 'required',
        'email'             => 'required|email',
        'country_id'        => 'required|exists:countries,id',
        'company'           => 'required',
        'company_website'   => 'required',
        'phone_number'      => 'required',
        'message'           => 'required',
    );

This is the form field:

<div class="form-group ">
                    <label class="col-lg-3 " for="title">Title: <sup>*</sup></label>
                    <div class="col-lg-8">
                        

                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="name">Name: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <input type="text" class="form-control required "
                               name="name" id="name" value="" required>
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="email">Email: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <input type="email" class="form-control required "
                               name="email" id="email" value="" required>
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="country_id">Country: <sup>*</sup></label>
                    <div class="col-lg-8">
                        
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="company">Company Name: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <input type="text" class="form-control required " required name="company" id="company" value="">
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="company_website">Company Website: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <input type="url" class="form-control required " required name="company_website" id="company_website" value="">
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="phone_number">Phone Number: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <input type="text" class="form-control hidden " name="phone_number" id="phone_number" placeholder="in internal format e.g : +185523232422" value="">
                        <input type="tel" id="phone" class="form-control">
                        <div id="valid-msg" class="hide text-aqc-green val_msg">✓ Valid</div>
                        <div id="error-msg" class="hide text-aqc-red val_msg">Invalid number</div>
                        
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-lg-3" for="message">Your Message: <sup>*</sup></label>
                    <div class="col-lg-8">
                        <textarea name="message" id="message" class="form-control" style="min-height:150px;max-height:200px;" placeholder="Place Your Message Here" required></textarea>
                    </div>
                </div>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire