samedi 26 août 2017

Laravel input missing (blocked by validation rules) but it exist in my form

I am making a CRUD Controller for Patient class like this.

BackendPatientController

<?php

class BackendPatientController extends Controller
{
    //region CONST_DEFINE_AREA
    const ID = 'id';
    const PATIENT_NAME = 'patient_name';
    const ADDRESS = 'address';
    const PHONE = 'phone';
    const DOB = 'dob';
    const DIAGNOSTIC = 'diagnostic';
    const SURGERY_TYPE = 'surgery_type';
    const SURGERY_COST = 'surgery_cost';
    const SURGERY_TIME = 'surgery_time';
    const FAMILY_INCOME = 'family_income';
    const FAMILY_SITUATION = 'family_situation';
    const UNIT = 'unit';
    const AFTER_SURGERY_INFO = 'after_surgery_info';
    const PHOTO = 'photo';
    const GENDER = 'gender';
    const POST_BY = 'post_by';
    const GENDER_ID = 'genderID';
    const POST_TIME = 'postTime';
    const SURGERY_DONE = 'surgery_done';

    const TABLE_PATIENT = 'patients';
    //endregion DEF DE
    const DATE_OF_BIRTH = 'dateOfBirth';

    private $allGender = null;



    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $genders = Gender::all();
        $surgery_dones = Patient::getAllSurgeryDoneStatus();
        return View::make('backend/patient/create', array('genders' => $genders, 'surgery_dones' => $surgery_dones));
        //return View::make('backend/patient/create');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $patient)
    {


        error_reporting(E_ALL);


        ini_set('display_errors',1);
        $myfile = fopen('E:\log.txt', "w") or die("Unable to open file!");
        $txt = "ID is : ".Input::get(self::ID);
        fwrite($myfile, $txt);
        fclose($myfile);

        $attributeNames = array(
            self::ID => 'Code',
            self::PATIENT_NAME => 'Name',
            self::DOB => 'Date of birth',
            self::DIAGNOSTIC => 'Diagnostic',
            self::SURGERY_TYPE => 'Surgery type',
            self::SURGERY_COST => 'Surgery cost',
            self::FAMILY_INCOME => 'Family income'
        );

        $validator = Validator::make($patient->all(), [
            self::ID => 'required|unique:patients|max:255',
            self::PATIENT_NAME => 'required',
            self::DOB => 'required',
            self::DIAGNOSTIC => 'required',
            self::SURGERY_TYPE => 'required',
            self::SURGERY_COST => 'required|numeric|min:1000',
            self::FAMILY_INCOME => 'numeric'
        ]);
        $validator->setAttributeNames($attributeNames);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        } else {
            $now = DateUtil::getTimeNow();
            $id = Input::get(self::ID, '0000');
            $name = Input::get(self::PATIENT_NAME, 'No name');
            $address = Input::get(self::ADDRESS, 'No address');
            $phone = Input::get(self::PHONE, 'No phone');
            $dob = Input::get(self::DOB, $now);
            $diagnostic = Input::get(self::DIAGNOSTIC);
            $surgery_type = Input::get(self::SURGERY_TYPE, 'Unknown');
            $surgery_cost = Input::get(self::SURGERY_COST, 0);
            $surgery_time = Input::get(self::SURGERY_TIME);
            $family_income = Input::get(self::FAMILY_INCOME, 0);
            $family_situation = Input::get(self::FAMILY_SITUATION, 'Unknown situation');
            $unit = Input::get(self::UNIT);
            $after_surgery_info = Input::get(self::AFTER_SURGERY_INFO, 'Unknown status');

            $fileNameToStore = null;
            if (Input::hasFile(self::PHOTO)) {
                $photo = Input::file(self::PHOTO);
                $fileNameWithExtension = $photo->getClientOriginalName();
                //Get file name without extension
                $fileName = pathinfo($fileNameWithExtension, PATHINFO_FILENAME);
                //Get file extension
                $extension = $photo->getClientOriginalExtension();
                //filename to store
                $fileNameToStore = $fileName . '_' . time() . '.' . $extension;
                //Upload file
                $photo->move(Patient::getPatientImageDir(), $fileNameToStore);
                //TinyPNG compress image code

            }
            $gender = Input::get(self::GENDER, 1);
            $postBy = Input::get(self::POST_BY, Session::get('currentLogin'));
            $postTime = $now;
            $surgery_done = Input::get(self::SURGERY_DONE);

            $new_patient[] = ['id' => $id, 'patientName' => $name, 'address' => $address,
                'phone' => $phone, 'dateOfBirth' => $dob, 'diagnostic' => $diagnostic,
                'surgeryType' => $surgery_type, 'surgeryCost' => $surgery_cost, 'surgeryTime' => $surgery_time,
                'income' => $family_income, 'familySituation' => $family_situation,
                'unit' => $unit, 'afterSurgeryInfo' => $after_surgery_info, 'photo' => $fileNameToStore, 'genderID' => $gender,
                'postBy' => $postBy, self::POST_TIME => $postTime, self::SURGERY_DONE => $surgery_done
            ];

            $update = Input::get('update', 0);
//            $new_patient = $this->inputStore($patient);
            $message = "Default message";
            if ($update == 0) {
                $message = "Create";
                DB::table(self::TABLE_PATIENT)->insert($new_patient);
            } else {
                $old_id = $patient->id;
                $old_patient = Patient::find($old_id);

                $old_patient->id =
                    $new_patient->id;
            }
            return redirect()->action('BackendPatientController@index', ['message' => $message]);
        }
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return Response
     */
    public
    function edit($id)
    {
        $genders = $this->getAll();
        $surgery_dones = Patient::getAllSurgeryDoneStatus();
        $patient = Patient::find($id);
        return View::make('/backend/patient/create',
            array('genders' => $genders, 'surgery_dones' => $surgery_dones, 'patient' => $patient));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int $id
     * @return Response
     */
}

I have a view (create.php) for create action, this view is bind to the store method of BackendPatientController class. The creating job is worked. Now I want to reuse the store action and the view create.php for edit action, like this: When I pass a Patient object to the view, it should understand that is editing instead of creating. create.php

...
    <form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data"
    action="" role="form">
    <input type="hidden" name="_token" value="">
    @if(isset($patient))
        <input type="hidden" name="update" value="1">
    @endif
        <?php
            $old_id = isset($patient)? $patient->id :   Request::old('id');
            $old_name = isset($patient)? $patient->patientName : Request::old('patientName');
            // get other properties of editing object ...
        ?>

        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">ID (*)</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input type="text" class="form-control" placeholder="ID" name="id" value="" @if(isset($patient)) disabled @endif>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Name (*)</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input type="text" class="form-control" placeholder="Name" name="patient_name"
                  value=""
                >
            </div>
        </div>
        ...
    </form>

When I edit a patient and click "Edit", it redirect to editing/create view, the ID and the Name is load correctly to the page, so the "id" input value is exist. When I click Submit, it call the store action with parameter update=1, mean editing, but the validation say: The Code is required ('Code' is the title assigned to to ID input field). So the editing job cannot be done, but when I test the creating job, it still work.

I think my problem is: The input value is exist but because it loaded programmatically to the form, so Laravel cannot recognize it.

Any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire